public override async ValueTask <BucketBytes> ReadAsync(int requested = int.MaxValue) { while (true) { BucketBytes bb; if (!_readable.IsEmpty) { if (requested > _readable.Length) { bb = _readable; _readable = BucketBytes.Empty; _position += bb.Length; return(bb); } bb = _readable.Slice(0, requested); _readable = _readable.Slice(requested); _position += bb.Length; return(bb); } if (!await RefillAsync(true).ConfigureAwait(false)) { return(BucketBytes.Eof); } } }
private async ValueTask ReadFromStream() { int length; #if NETFRAMEWORK length = await _stream.ReadAsync(_inputBuffer, 0, _inputBuffer.Length).ConfigureAwait(false); #else length = await _stream.ReadAsync(new Memory <byte>(_inputBuffer)).ConfigureAwait(false); #endif _bytesRead += length; _unread = new BucketBytes(_inputBuffer, 0, length); }
async Task <BucketBytes> DoRead(int requested) { if (_unread.Length == 0 && !_readEof) { await ReadFromStream().ConfigureAwait(false); } if (_unread.Length > 0) { var bb = _unread.Slice(0, Math.Min(requested, _unread.Length)); _unread = _unread.Slice(bb.Length); return(bb); } else { _readEof = true; return(BucketBytes.Eof); } }
//public ValueTask<BucketBytes> PollAsync(int minRequested = 1) //{ // return Peek(); //} public override BucketBytes Peek() { if (!_remaining.IsEmpty) { return(_remaining); } while (!_readLeft.IsEmpty) { int origLeft = _readLeft.Length; _remaining = ConvertData(ref _readLeft, false); if (!_remaining.IsEmpty || _readLeft.Length == origLeft) { return(_remaining); } } var bb = Inner.Peek(); if (!bb.IsEmpty && _skipFirst > 0) { bb = bb.Slice(_skipFirst); } if (bb.IsEmpty) { return(bb); } int len = bb.Length; _remaining = ConvertData(ref bb, false); _skipFirst += (len - bb.Length); return(_remaining); }
public void BytesEmptyEof() { Assert.IsTrue(BucketBytes.Eof.IsEof, "BucketBytes.Eof.IsEof = true"); Assert.IsFalse(BucketBytes.Empty.IsEof, "BucketBytes.Eof.IsEof = false"); Assert.IsFalse(BucketBytes.Eof.Equals(BucketBytes.Empty), "BucketBytes.Eof != BucketBytes.Empty"); //Assert.IsFalse(BucketBytes.Empty.IsEof, "BucketBytes.Eof.IsEof = false"); Assert.IsTrue(BucketBytes.Eof.Trim().IsEof, "Trimmed still eof"); Assert.IsFalse(BucketBytes.Empty.Trim().IsEof, "Trimmed not eof"); BucketBytes bEmpty = Array.Empty <byte>(); Assert.IsTrue(bEmpty.IsEmpty, "Empty"); Assert.IsFalse(bEmpty.IsEof, "Not EOF"); Assert.IsTrue(BucketBytes.Empty.Slice(0, 0).IsEmpty, "Empty"); Assert.IsTrue(BucketBytes.Eof.Slice(0, 0).IsEmpty, "Empty"); Assert.IsFalse(BucketBytes.Empty.Slice(0, 0).IsEof, "Eof"); Assert.IsTrue(BucketBytes.Eof.Slice(0, 0).IsEof, "Eof"); Assert.IsTrue(bEmpty.Slice(0, 0).IsEmpty, "Empty"); Assert.IsFalse(bEmpty.IsEof, "Not EOF"); }
public override async ValueTask <BucketBytes> ReadAsync(int requested = int.MaxValue) { if (!_remaining.IsEmpty) { var r = _remaining.Slice(0, Math.Min(requested, _remaining.Length)); _remaining = _remaining.Slice(r.Length); _position += r.Length; return(r); } while (_skipFirst > 0) { var skipped = await Inner.ReadSkipAsync(_skipFirst).ConfigureAwait(false); _skipFirst -= (int)skipped; } do { if (_readLeft.IsEmpty) { _readLeft = await Inner.ReadAsync(ConvertRequested(requested)).ConfigureAwait(false); } _remaining = ConvertData(ref _readLeft, _readLeft.IsEof); if (!_remaining.IsEmpty) { var r = _remaining.Slice(0, Math.Min(requested, _remaining.Length)); _remaining = _remaining.Slice(r.Length); _position += r.Length; return(r); } }while (!_readLeft.IsEof); return(BucketBytes.Eof); }
public override async ValueTask <BucketBytes> ReadAsync(int requested = int.MaxValue) { if (_remaining.Length == 0) { #if !NETFRAMEWORK int n = await _stream.ReadAsync(_buffer).ConfigureAwait(false); #else int n = await _stream.ReadAsync(_buffer, 0, _buffer.Length).ConfigureAwait(false); #endif _remaining = new BucketBytes(_buffer, 0, n); } if (_remaining.Length > 0) { var r = _remaining.Slice(0, Math.Min(requested, _remaining.Length)); _remaining = _remaining.Slice(r.Length); return(r); } else { return(BucketBytes.Eof); } }
protected abstract BucketBytes ConvertData(ref BucketBytes sourceData, bool final);
private async ValueTask <bool> RefillAsync(bool allowWait) { if (_state <= ewah_state.start && !allowWait && Inner.Peek().IsEmpty) { return(false); } if (_lengthBits is null) { var bb = await Inner.ReadFullAsync(4 + 4).ConfigureAwait(false); _lengthBits = NetBitConverter.ToUInt32(bb, 0); _compressedSize = NetBitConverter.ToInt32(bb, 4); _left = _compressedSize; _state = ewah_state.start; } int peekLength = Inner.Peek().Length / sizeof(ulong); _wpos = 0; switch (_state) { case ewah_state.start: ulong curOp = await Inner.ReadNetworkUInt64Async().ConfigureAwait(false); _repBit = (curOp & 1UL) != 0; _repCount = (uint)(curOp >> 1); _rawCount = (int)(curOp >> 33); _left--; peekLength--; _state = ewah_state.same; goto case ewah_state.same; case ewah_state.same: byte val = _repBit ? (byte)0xFF : (byte)0; while (_repCount > 0 && _wpos + 8 < _buffer.Length) { _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _buffer[_wpos++] = val; _repCount--; } if (_repCount > 0) { _readable = new BucketBytes(_buffer, 0, _wpos); return(true); } _state = ewah_state.raw; goto case ewah_state.raw; case ewah_state.raw: while (_rawCount > 0) { if ((_wpos > 8 && peekLength < 8) || (_wpos + 8 >= _buffer.Length)) { // Avoid new reads if we already have something. Return result _readable = new BucketBytes(_buffer, 0, _wpos); return(true); } var bb = await Inner.ReadFullAsync(sizeof(ulong)).ConfigureAwait(false); if (bb.Length != sizeof(ulong)) { throw new BucketEofException(Inner); } peekLength--; _left--; _rawCount--; for (int i = bb.Length - 1; i >= 0; i--) { _buffer[_wpos++] = bb[i]; } } if (_left == 0) { _state = ewah_state.footer; _readable = new BucketBytes(_buffer, 0, _wpos); return(true); } _state = ewah_state.start; goto case ewah_state.start; case ewah_state.footer: await Inner.ReadNetworkUInt32Async().ConfigureAwait(false); _state = ewah_state.done; goto case ewah_state.done; case ewah_state.done: default: return(false); } }
internal static bool TryReadRefFile(string path, string?prefix, [NotNullWhen(true)] out string?result) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } #pragma warning disable CA1031 // Do not catch general exception types try { using var f = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 512); byte[] buf = new byte[512]; int n = f.Read(buf, 0, buf.Length); if (buf.Length > 0 && n < buf.Length && n >= (prefix?.Length ?? 0)) { BucketEol eol = BucketEol.None; if (buf.Length > 2 && buf[buf.Length - 1] == '\n') { if (buf[buf.Length - 2] == '\r') { eol = BucketEol.CRLF; } else { eol = BucketEol.LF; } } else if (buf[buf.Length - 1] == '\r') { eol = BucketEol.CR; } else if (buf[buf.Length - 1] == '\n') { eol = BucketEol.LF; } else if (buf[buf.Length - 1] == '\0') { eol = BucketEol.Zero; } BucketBytes bb = new BucketBytes(buf, 0, n); if (prefix != null) { var p = bb.Slice(0, prefix.Length).ToUTF8String(); if (prefix != p) { result = null; return(false); } bb = bb.Slice(p.Length); } result = bb.ToUTF8String(eol); return(true); } } catch (IOException) { } catch (NotSupportedException) { } catch (SystemException) { } #pragma warning restore CA1031 // Do not catch general exception types result = null; return(false); }