3using System.Runtime.ExceptionServices;
6using System.Threading.Tasks;
25 private const int BufferSize = 65536;
27 private readonly
byte[] buffer =
new byte[BufferSize];
28 private readonly
byte[] signature;
29 private readonly
string collectionName;
33 private readonly ulong bytes;
34 private long bytesLeft;
35 private int bufPos = 0;
36 private int bufLen = 0;
41 this.bytesLeft = InputLength;
56 ICryptoTransform Aes =
null;
60 fs = File.OpenRead(FileName);
61 Aes = Provider.GetAes(FileName,
false);
62 return new CryptoStream(fs, Aes, CryptoStreamMode.Read);
69 ExceptionDispatchInfo.Capture(ex).Throw();
82 ICryptoTransform Aes =
null;
83 CryptoStream cs =
null;
88 fs = File.OpenRead(FileName);
89 Aes = Provider.GetAes(FileName,
false);
90 cs =
new CryptoStream(fs, Aes, CryptoStreamMode.Read);
100 ExceptionDispatchInfo.Capture(ex).Throw();
113 int CollectionLen = cs.ReadByte();
114 if (CollectionLen == 0)
117 byte[] CollectionBin = await cs.ReadAllAsync(CollectionLen);
121 int Bytes = 1 + CollectionLen;
123 int SigLen = cs.ReadByte();
127 byte[]
Signature = await cs.ReadAllAsync(SigLen);
133 byte[] ContentLen = await cs.ReadAllAsync(8);
135 long InputLength = BitConverter.ToInt64(ContentLen, 0);
146 int Len = (int)Math.Min(1024, Result.bytesLeft);
147 byte[] Bin = await Result.ReadBytesAsync(Len);
152 Result.bufPos -= Bin.Length - Result.reader.
Position;
157 ExceptionDispatchInfo.Capture(ex).Throw();
168 private async Task<byte[]> ReadBytesAsync(
int Bytes)
170 byte[] Result =
new byte[
Bytes];
176 if (this.bufPos >= this.bufLen)
177 await this.ReadMore();
179 i = Math.Min(
Bytes - Pos, this.bufLen - this.bufPos);
180 System.Buffer.BlockCopy(this.buffer, this.bufPos, Result, Pos, i);
188 private async Task ReadMore()
191 this.bufLen = await this.input.TryReadAllAsync(this.buffer, 0, BufferSize);
192 if (this.bufLen <= 0)
193 throw new IOException(
"Unable to read from block.");
195 this.bytesLeft -= this.bufLen;
196 if (this.bytesLeft < 0)
198 this.bufLen += (int)this.bytesLeft;
203 private async Task<byte> ReadByteAsync()
205 if (this.bufPos >= this.bufLen)
206 await this.ReadMore();
208 return this.buffer[this.bufPos++];
218 return (this.bytesLeft <= 0 && this.bufPos >= this.bufLen);
252 this.input?.Dispose();
271 byte[] Buf = await this.ReadBytesAsync(10);
274 long Ticks = BitConverter.ToInt64(Buf, 1);
275 DateTime Timestamp =
new DateTime(Ticks, DateTimeKind.Utc);
280 while ((b & 128) != 0)
282 b = await this.ReadByteAsync();
283 Len |= (b & 127) << Offset;
287 Buf = await this.ReadBytesAsync(Len);
Reads objects in a block.
byte[] Buffer
Internal buffer.
static async Task< BlockReader > CreateAsync(string FileName, NeuroLedgerProvider Provider)
Creates a block reader.
static async Task< BlockReader > CreateAsync(Stream cs, NeuroLedgerProvider Provider)
Creates a block reader.
string CollectionName
Collection name.
async Task< Entry > ReadEntryAsync()
Reads an entry from the block.
Entry ReadEntry()
Reads an entry from the block.
static Stream GetStream(string FileName, NeuroLedgerProvider Provider)
Gets a stream to the contents of the block.
bool EOF
If the current position is at the end of the file.
ulong Bytes
Size of block, in bytes
int BufferPosition
Position in internal buffer.
void Dispose()
IDisposable.Dispose
byte[] Signature
Signature of block.
BlockHeader Header
Block Header
Represents an entry in a block or bucket file.
Task< ObjectSerializer > GetObjectSerializerEx(object Object)
Gets the object serializer corresponding to a specific object.
Manages binary deserialization of data.
int Position
Current position.
Serializes a class, taking into account attributes defined in Attributes.
virtual async Task< object > Deserialize(IDeserializer Reader, uint? DataType, bool Embedded)
Deserializes a value.
const uint TYPE_OBJECT
Represents an object.
delegate Task BinaryDataReadCallback(byte[] Data, bool More, object State)
Delegate for binary data callback methods.
EntryType
Ledger entry type.