3using System.Runtime.ExceptionServices;
4using System.Security.Cryptography;
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 =
new byte[CollectionLen];
118 await cs.ReadAllAsync(CollectionBin, 0, CollectionLen);
122 int Bytes = 1 + CollectionLen;
124 int SigLen = cs.ReadByte();
129 await cs.ReadAllAsync(
Signature, 0, SigLen);
135 byte[] ContentLen =
new byte[8];
136 await cs.ReadAllAsync(ContentLen, 0, 8);
138 long InputLength = BitConverter.ToInt64(ContentLen, 0);
149 int Len = (int)Math.Min(1024, Result.bytesLeft);
150 byte[] Bin = await Result.ReadBytesAsync(Len);
155 Result.bufPos -= Bin.Length - Result.reader.
Position;
160 ExceptionDispatchInfo.Capture(ex).Throw();
171 private async Task<byte[]> ReadBytesAsync(
int Bytes)
173 byte[] Result =
new byte[
Bytes];
179 if (this.bufPos >= this.bufLen)
180 await this.ReadMore();
182 i = Math.Min(
Bytes - Pos, this.bufLen - this.bufPos);
183 Array.Copy(this.buffer, this.bufPos, Result, Pos, i);
191 private async Task ReadMore()
194 this.bufLen = await this.input.TryReadAllAsync(this.buffer, 0, BufferSize);
195 if (this.bufLen <= 0)
196 throw new IOException(
"Unable to read from block.");
198 this.bytesLeft -= this.bufLen;
199 if (this.bytesLeft < 0)
201 this.bufLen += (int)this.bytesLeft;
206 private async Task<byte> ReadByteAsync()
208 if (this.bufPos >= this.bufLen)
209 await this.ReadMore();
211 return this.buffer[this.bufPos++];
221 return (this.bytesLeft <= 0 && this.bufPos >= this.bufLen);
255 this.input?.Dispose();
274 byte[] Buf = await this.ReadBytesAsync(10);
277 long Ticks = BitConverter.ToInt64(Buf, 1);
278 DateTime Timestamp =
new DateTime(Ticks, DateTimeKind.Utc);
283 while ((b & 128) != 0)
285 b = await this.ReadByteAsync();
286 Len |= (b & 127) << Offset;
290 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.