Exemplo n.º 1
0
 public override void Reset()
 {
     Inner.Reset();
     _state    = ewah_state.init;
     _wpos     = 0;
     _position = 0;
 }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
 public GitEwahBitmapBucket(Bucket inner)
     : base(inner)
 {
     _state  = ewah_state.init;
     _buffer = new byte[512];
 }