public void ReadManyStreamsBytePerByte() { using (var s = new FastCgiStreamImpl(true)) { int numStreams = 3; var streams = new RecordContentsStream[numStreams]; // Write numStreams streams with 10 bytes each, storing sequential numbers byte[] buf = new byte[10]; for (int i = 0; i < buf.Length; ++i) { buf[i] = (byte)i; } for (int i = 0; i < numStreams; ++i) { streams[i] = new RecordContentsStream(); streams[i].Write(buf, 0, 10); s.AppendStream(streams[i]); } // Now read stuff for (int i = 0; i < buf.Length * numStreams; ++i) { Assert.AreEqual(30, 30 * s.Read(buf, 0, 1)); Assert.AreEqual((byte)(i % 10), buf[0]); } for (int i = 0; i < numStreams; ++i) { streams[0].Dispose(); } } }
public void ReadCheckAllAtOnce() { using (var s = new FastCgiStreamImpl(true)) { int numStreams = 3; var streams = new RecordContentsStream[numStreams]; // Write numStreams streams with 10 bytes each, storing sequential numbers byte[] buf = new byte[10]; for (int i = 0; i < buf.Length; ++i) { buf[i] = (byte)i; } for (int i = 0; i < numStreams; ++i) { streams[i] = new RecordContentsStream(); streams[i].Write(buf, 0, 10); streams[i].Seek(0, SeekOrigin.Begin); s.AppendStream(streams[i]); } // Now read stuff byte[] superBuf = new byte[numStreams * 10]; Assert.AreEqual(superBuf.Length, s.Read(superBuf, 0, superBuf.Length)); Assert.AreEqual(superBuf.Length, s.Position); for (int i = 0; i < superBuf.Length; ++i) { Assert.AreEqual(buf[i % 10], superBuf[i]); } } }
public override void AppendStream(RecordContentsStream stream) { // If a stream is appended, it means that the last one filled up (except for the very first one)! if (underlyingStreams.Count > 0) OnStreamFill(); base.AppendStream(stream); }
public override void AppendStream(RecordContentsStream stream) { // If a stream is appended, it means that the last one filled up (except for the very first one)! if (underlyingStreams.Count > 0) { OnStreamFill(); } base.AppendStream(stream); }
/// <summary> /// Base constructor that initializes this record's contents' stream to use secondary storage. The life-cycle of the supplied /// secondary storage stream has no relation with this record whatsoever. /// </summary> public StreamRecordBase(RecordType recordType, ushort requestId, Stream secondaryStorageStream) : this(recordType, requestId) { if (secondaryStorageStream == null) { throw new ArgumentNullException("secondaryStorageOps"); } Contents = new RecordContentsStream(secondaryStorageStream); }
public void OnlyWriteAtEndOfStream() { var s = new RecordContentsStream(); byte[] testbuf = new byte[10]; s.Write(testbuf, 0, testbuf.Length); s.Position = 0; Assert.Throws <NotImplementedException>(() => { s.Write(testbuf, 0, 1); }); }
public void WritePositionLength() { var s = new RecordContentsStream(); byte[] testbuf = new byte[10]; for (int i = 0; i < testbuf.Length; ++i) { s.Write(testbuf, i, 1); } Assert.AreEqual(testbuf.Length, s.Length); Assert.AreEqual(testbuf.Length, s.Position); }
public void WriteMoreThanLimit() { var s = new RecordContentsStream(); // 65535 bytes is the limit of the contents of a record byte[] anything = new byte[1024]; for (int i = 0; i < 64; ++i) { s.Write(anything, 0, i == 63 ? 1023 : 1024); } // Exception now! Assert.Throws <InvalidOperationException>(() => s.Write(anything, 0, 1)); }
internal StreamRecordBase(RecordType recordType, byte[] data, int offset, int length, out int endOfRecord) : base(data, offset, length, recordType) { Contents = new RecordContentsStream(); if (ContentLength + PaddingLength == 0) { endOfRecord = offset + 7; } else if (length > 8) { FeedBytes(data, offset + 8, length - 8, out endOfRecord); } else { endOfRecord = -1; } }
/// <summary> /// Adds more content data that has been read from the socket. If we have reached the end of this record's content, then <paramref name="lastByteOfContent"/> will be >= 0. Otherwise, it will be -1, /// meaning more data needs to be added to this Content. /// </summary> /// <remarks>Do not use this method to build a record's contents to be sent over the wire. Use the stream directly in that case.</remarks> internal override void FeedBytes(byte[] data, int offset, int length, out int lastByteOfRecord) { AssertArrayOperation(data, offset, length); if (Contents == null) { Contents = new RecordContentsStream(); } // Fill up the Content if we can int contentNeeded = ContentLength - addedContentLength; if (contentNeeded <= length) { if (contentNeeded != 0) { Contents.Write(data, offset, contentNeeded); addedContentLength = ContentLength; } // Check for the end of padding too! int paddingAvailable = length - contentNeeded; int paddingNeeded = PaddingLength - addedPaddingLength; if (paddingAvailable >= paddingNeeded) { lastByteOfRecord = offset + contentNeeded + paddingNeeded - 1; addedPaddingLength = PaddingLength; } else { lastByteOfRecord = offset + contentNeeded + paddingAvailable - 1; addedPaddingLength += paddingAvailable; } } else { Contents.Write(data, offset, length); addedContentLength += length; lastByteOfRecord = -1; } }
public void ReadCheckOneStreamOneByteOffsetDifferentThanZero() { using (var s = new FastCgiStreamImpl(true)) { var stream = new RecordContentsStream(); byte[] buf = new byte[1]; byte data = (byte)93; buf[0] = data; stream.Write(buf, 0, 1); stream.Seek(0, SeekOrigin.Begin); s.AppendStream(stream); // Now read stuff. Set an offset to something different than 0 byte[] newBuf = new byte[16]; int offset = 10; Assert.AreEqual(1, s.Read(newBuf, offset, 1)); Assert.AreEqual(1, s.Position); Assert.AreEqual(data, newBuf[offset]); } }
public void ReadTwoStreamsOneByteEachWithSeeking() { using (var s = new FastCgiStreamImpl(true)) { var streams = new RecordContentsStream[2]; byte[] buf = new byte[2]; byte b0 = (byte)32; byte b1 = (byte)33; buf[0] = b0; buf[1] = b1; streams[0] = new RecordContentsStream(); streams[0].Write(buf, 0, 1); streams[1] = new RecordContentsStream(); streams[1].Write(buf, 1, 1); // Add streams to the FragmentedStream s.AppendStream(streams[0]); s.AppendStream(streams[1]); // Now scramble the buffer and read those bytes buf[0] = (byte)0; buf[1] = (byte)0; // Make sure seeking works Assert.AreEqual(0, s.Position); for (int i = 0; i < 2; ++i) { s.Read(buf, 0, 1); s.Read(buf, 1, 1); Assert.AreEqual(b0, buf[0]); Assert.AreEqual(b1, buf[1]); s.Position = 0; } } }
public void WriteSeekAndReadPositionLength() { var s = new RecordContentsStream(); byte[] testbuf = new byte[10]; testbuf[0] = (byte)1; testbuf[5] = (byte)7; for (int i = 0; i < testbuf.Length; ++i) { s.Write(testbuf, i, 1); } s.Seek(0, System.IO.SeekOrigin.Begin); Assert.AreEqual(testbuf.Length, s.Length); Assert.AreEqual(0, s.Position); s.Position = 5; s.Read(testbuf, 0, 1); Assert.AreEqual(6, s.Position); Assert.AreEqual(testbuf.Length, s.Length); Assert.AreEqual(testbuf[0], testbuf[5]); Assert.AreEqual(testbuf[0], (byte)7); }
/// <summary> /// Base constructor that initializes this record's contents' stream to use memory. /// </summary> public StreamRecordBase(RecordType recordType, ushort requestId) : base(recordType, requestId) { Contents = new RecordContentsStream(); }