Repository navigation
Expand file tree
/
Copy pathBytesCodec.cs
More file actions
113 lines (88 loc) · 3.36 KB
/
Copy pathBytesCodec.cs
File metadata and controls
113 lines (88 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace ZarrNET;
/// <summary>
/// The "bytes" array-to-bytes codec (Zarr v3 spec section 3.3.2).
/// Handles endianness conversion when translating between the in-memory
/// array representation and a flat byte sequence.
/// This is always the first codec in a Zarr v3 pipeline.
/// </summary>
public sealed class BytesCodec : IZarrCodec
{
public string Name => "bytes";
private readonly ByteOrder _byteOrder;
public BytesCodec(ByteOrder byteOrder = ByteOrder.LittleEndian)
{
_byteOrder = byteOrder;
}
// -------------------------------------------------------------------------
// IZarrCodec
// -------------------------------------------------------------------------
public Task<byte[]> DecodeAsync(byte[] input, CancellationToken ct = default)
{
ct.ThrowIfCancellationRequested();
var result = NeedsSwap()
? SwapByteOrder(input)
: input;
return Task.FromResult(result);
}
public Task<byte[]> EncodeAsync(byte[] input, CancellationToken ct = default)
{
ct.ThrowIfCancellationRequested();
// Swap is its own inverse
var result = NeedsSwap()
? SwapByteOrder(input)
: input;
return Task.FromResult(result);
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
private bool NeedsSwap()
=> _byteOrder == ByteOrder.BigEndian && BitConverter.IsLittleEndian
|| _byteOrder == ByteOrder.LittleEndian && !BitConverter.IsLittleEndian;
internal bool IsNoOpForHostByteOrder => !NeedsSwap();
/// <summary>
/// Reverses byte order for each element. We rely on the element size being
/// provided by the zarr data type — but BytesCodec at this layer operates on
/// raw bytes, so we receive already-flat bytes and treat them as pairs/quads/etc.
/// The element size is resolved by CodecPipeline before invoking this codec.
/// </summary>
private static byte[] SwapByteOrder(byte[] input)
{
// Default only applies if this codec is used directly. CodecPipeline
// supplies the real dtype element size through the overload below.
return SwapByteOrder(input, elementSize: 2);
}
internal Task<byte[]> DecodeWithElementSizeAsync(byte[] input, int elementSize, CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
var result = NeedsSwap()
? SwapByteOrder(input, elementSize)
: input;
return Task.FromResult(result);
}
internal Task<byte[]> EncodeWithElementSizeAsync(byte[] input, int elementSize, CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
var result = NeedsSwap()
? SwapByteOrder(input, elementSize)
: input;
return Task.FromResult(result);
}
internal static byte[] SwapByteOrder(byte[] input, int elementSize)
{
if (elementSize <= 1)
return input;
var output = new byte[input.Length];
for (int i = 0; i < input.Length; i += elementSize)
{
for (int j = 0; j < elementSize; j++)
output[i + j] = input[i + elementSize - 1 - j];
}
return output;
}
}
public enum ByteOrder
{
LittleEndian,
BigEndian
}