/// <summary> /// Gets the buffer stream instance which can be reused. /// </summary> /// <typeparam name="TStream">The type of the stream.</typeparam> /// <typeparam name="TPackageInfo">The type of the package info.</typeparam> /// <param name="receiveFilter">The receive filter.</param> /// <param name="data">The data.</param> /// <returns></returns> public static BufferListStream GetBufferStream <TStream, TPackageInfo>(this IReceiveFilter <TPackageInfo> receiveFilter, IList <ArraySegment <byte> > data) where TStream : BufferListStream, new() where TPackageInfo : IPackageInfo { var stream = BufferListStream.GetCurrent <BufferListStream>(); stream.Initialize(data); return(stream); }
/// <summary> /// Gets the buffer stream instance which can be reused. /// </summary> /// <typeparam name="TStream">The type of the stream.</typeparam> /// <typeparam name="TPackageInfo">The type of the package info.</typeparam> /// <param name="receiveFilter">The receive filter.</param> /// <param name="data">The data.</param> /// <returns></returns> public static BufferListStream GetBufferStream <TStream, TPackageInfo>(this IReceiveFilter <TPackageInfo> receiveFilter, IList <ArraySegment <byte> > data) where TStream : BufferListStream, new() where TPackageInfo : IPackageInfo { //var stream = BufferListStream.GetCurrent<BufferListStream>(); // don't use thread context for BufferListReader for now var stream = new BufferListStream(); stream.Initialize(data); return(stream); }
public void should_be_able_to_advance_the_position_into_the_next_segment() { BufferListStream sut = new BufferListStream(); List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); segments.Add(GetSegment(new byte[16], 512, 1024)); segments.Add(GetSegment(new byte[16], 512, 1024)); sut.Initialize(segments); Assert.AreEqual(0, sut.Position); sut.Position += 24; Assert.AreEqual(24, sut.Position); }
public void should_be_able_to_advance_the_position_within_current_segment() { int segmentLength = 16; int expectedPosition = segmentLength/2; BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { GetSegment(new byte[segmentLength], 512, 1024) }); Assert.AreEqual(0, sut.Position); sut.Position += expectedPosition; Assert.AreEqual(expectedPosition, sut.Position); }
public void should_be_able_to_call_skip_to_advance_the_position() { BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { GetSegment(new byte[] { 0x01 }, 123, 1024) }); // pre-condition Assert.AreEqual(1, sut.Length); // act var actual = sut.Skip(1); // assert Assert.NotNull(actual); Assert.AreEqual(1, sut.Position); }
public void should_be_able_to_get_correct_position() { var data = Encoding.UTF8.GetBytes("SuperSocket rocks!"); var segments = new List<ArraySegment<byte>>(); segments.Add(new ArraySegment<byte>(data)); using (var bufferListStream = new BufferListStream()) { bufferListStream.Initialize(segments); Assert.AreEqual(data.Length, bufferListStream.Length); Assert.AreEqual(0, bufferListStream.Position); bufferListStream.Seek(1, SeekOrigin.Begin); Assert.AreEqual(1, bufferListStream.Position); bufferListStream.Seek(1, SeekOrigin.Current); Assert.AreEqual(2, bufferListStream.Position); bufferListStream.Seek(data.Length - 1, SeekOrigin.Begin); Assert.AreEqual(data.Length - 1, bufferListStream.Position); } }
public void should_not_be_able_to_skip_past_the_end_of_all_the_buffers() { int segmentSize = 16; BufferListStream sut = new BufferListStream(); List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); segments.Add(GetSegment(new byte[segmentSize], 512, 1024)); segments.Add(GetSegment(new byte[segmentSize], 512, 1024)); sut.Initialize(segments); sut.Skip(segmentSize * 2 + 1); }
public void should_not_be_able_to_skip_backwards() { BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { GetSegment(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 123, 1024) }); sut.Position = 1; sut.Skip(-1); }
public void should_be_able_to_take_a_buffer_from_the_current_position() { BufferListStream sut = new BufferListStream(); List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); for (int i = 0; i < 10; i++) { segments.Add(GetSegment(new byte[16], 512, 1024)); } sut.Initialize(segments); // pre-condition Assert.AreEqual(0, sut.Position); sut.Position += 24; // act var actual = sut.Take(36); var totalLength = actual.Sum(segment => segment.Count); // assert Assert.IsNotNull(actual); Assert.AreEqual(36, totalLength); // TODO: check if the data is correct }
public void should_be_able_to_rewind_the_position_into_the_previous_segment() { int segmentLength = 16; int expectedPosition = segmentLength * 3 / 2; BufferListStream sut = new BufferListStream(); List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); segments.Add(GetSegment(new byte[segmentLength], 512, 1024)); segments.Add(GetSegment(new byte[segmentLength], 512, 1024)); sut.Initialize(segments); Assert.AreEqual(0, sut.Position); sut.Position += expectedPosition; Assert.AreEqual(expectedPosition, sut.Position); sut.Position -= segmentLength; Assert.AreEqual(segmentLength/2, sut.Position); }
public void should_be_able_to_read_a_string_from_one_segment() { string expected = "The quick brown fox jumps over the lazy dog"; var encoding = Encoding.ASCII; var bytes = encoding.GetBytes(expected); ArraySegment<byte> segment = new ArraySegment<byte>(bytes); BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { segment }); // act var actual = sut.ReadString(expected.Length, encoding); // assert Assert.AreEqual(expected, actual); Assert.AreEqual(expected.Length, sut.Position); }
public void should_be_able_to_read_a_string_from_multiple_segments() { string expected = "The quick brown fox jumps over the lazy dog"; Encoding encoding = Encoding.ASCII; // create n-segments of each word and space Random random = new Random(0); List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); var words = expected.Split(' '); for (int i = 0; i < words.Length; i++) { if (i != 0) { segments.Add(GetSegment(encoding.GetBytes(" "), random.Next(0,256), random.Next(1, 4) * 1024)); } segments.Add(GetSegment(encoding.GetBytes(words[i]), random.Next(0, 256), random.Next(1, 4) * 1024)); } BufferListStream sut = new BufferListStream(); sut.Initialize(segments); // pre-condition Assert.AreEqual(expected.Length, sut.Length); string actual = sut.ReadString(expected.Length, encoding); Assert.AreEqual(expected, actual); Assert.AreEqual(expected.Length, sut.Position); }
public void should_be_able_to_read_a_single_byte() { byte expected = 0x01; BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { GetSegment(new byte[] { expected }, 123, 1024) }); // pre-condition Assert.AreEqual(1, sut.Length); Assert.AreEqual(0, sut.Position); // act var actual = sut.ReadByte(); // assert Assert.AreEqual(expected, actual); Assert.AreEqual(1, sut.Position); }
public void should_be_able_to_read_a_short_integer(bool littleEndian, byte[] bytes, short expected) { List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); segments.Add(new ArraySegment<byte>(bytes)); BufferListStream sut = new BufferListStream(); sut.Initialize(segments); // act var actual = sut.ReadInt16(littleEndian); // assert Assert.AreEqual(expected, actual); Assert.AreEqual(2, sut.Position); }
public void should_be_able_to_read_a_byte_and_the_position_should_be_advanced_by_one() { BufferListStream sut = new BufferListStream(); sut.Initialize(new[] { GetSegment(new byte[] { 0x01, 0x02 }, 512, 1024) }); // pre-condition Assert.AreEqual(2, sut.Length); Assert.AreEqual(0, sut.Position); // act var first = sut.ReadByte(); // assert Assert.AreEqual(1, first); Assert.AreEqual(1, sut.Position); }
public void should_be_able_to_read_a_string_from_segments() { string expected = "The quick brown fox jumps over the lazy dog"; BufferListStream sut = new BufferListStream(); sut.Initialize(GetSegments(expected, Encoding.ASCII, 2)); // pre-condition Assert.AreEqual(expected.Length, sut.Length); var actual = sut.ReadString(expected.Length, Encoding.ASCII); // assert Assert.AreEqual(expected, actual); Assert.AreEqual(expected.Length, sut.Position); }
public void should_be_able_to_read_an_unsigned_long_integer(bool littleEndian, byte[] bytes, ulong expected) { List<ArraySegment<byte>> segments = new List<ArraySegment<byte>>(); segments.Add(new ArraySegment<byte>(bytes)); BufferListStream sut = new BufferListStream(); sut.Initialize(segments); // act ulong actual = sut.ReadUInt64(littleEndian); // assert Assert.AreEqual(8, bytes.Length); Assert.AreEqual(expected, actual); Assert.AreEqual(8, sut.Position); }