예제 #1
0
        public void WriteSerializedBulk <T>(ISerializer <T> serializer, T obj, int count)
        {
            SAssert.ArgumentNotNull(() => serializer);
            SAssert.ArgumentNonNegative(() => count);

            serializer.Write(obj, _stream, count);
            writeCrLf();
        }
예제 #2
0
        public byte[] ReadBulk(int length)
        {
            SAssert.ArgumentNonNegative(() => length);

            var buffer = new byte[length];

            ReadBulk(buffer, 0, length);

            return(buffer);
        }
예제 #3
0
        public LimitingStream(Stream inner, int bytesLimit)
        {
            SAssert.ArgumentNotNull(() => inner);
            SAssert.ArgumentNonNegative(() => bytesLimit);
            SAssert.ArgumentSatisfy(() => inner, s => s.CanRead,
                                    "Stream must be readable.");

            _inner     = inner;
            _bytesLeft = bytesLimit;
        }
예제 #4
0
        public T ReadSerializedBulk <T>(ISerializer <T> serializer, int bulkLength)
        {
            SAssert.ArgumentNotNull(() => serializer);
            SAssert.ArgumentNonNegative(() => bulkLength);

            var result = serializer.Read(_stream, bulkLength);

            readCrLf();

            return(result);
        }
예제 #5
0
        public void ReadBulkTo(Stream target, int bulkLength, int bufferSize)
        {
            SAssert.ArgumentNotNull(() => target);
            SAssert.ArgumentNonNegative(() => bulkLength);
            SAssert.ArgumentPositive(() => bufferSize);

            using (var limiter = new LimitingStream(_stream, bulkLength))
                limiter.CopyTo(target);

            readCrLf();
        }
예제 #6
0
        public void WriteBulkFrom(Stream source, int count)
        {
            SAssert.ArgumentNotNull(() => source);
            SAssert.ArgumentNonNegative(() => count);

            using (var limiter = new LimitingStream(source, count)) {
                limiter.CopyTo(_stream);
                writeCrLf();

                SAssert.IsTrue(limiter.BytesLeft == 0,
                               () => new InvalidOperationException("Stream does not contains enough data."));
            }
        }
예제 #7
0
        public void ReadBulk(byte[] buffer, int offset, int bulkLength)
        {
            SAssert.ArgumentNotNull(() => buffer);
            SAssert.ArgumentNonNegative(() => bulkLength);

            // special case for empty reads
            if (offset == 0 && bulkLength == 0)
            {
                return;
            }

            SAssert.ArgumentBetween(() => offset, 0, buffer.Length);
            SAssert.ArgumentSatisfy(() => offset, o => o + bulkLength <= buffer.Length,
                                    "Offset plus bulkLength is larger than the supplied buffer.");

            // read data from the stream, expect as much data as there's bulkLength
            var bytesRead = _stream.Read(buffer, 0, bulkLength);

            SAssert.IsTrue(bytesRead == bulkLength,
                           () => new ResponseException("Expected " + bulkLength.ToString() +
                                                       " bytes of bulk data, but only " + bytesRead.ToString() + " bytes are read."));

            readCrLf();
        }