public void Reload() { BlockArray ba; Block b; byte v; ba = new BlockArray(0, 10, 5); ba.ExtendTo(10); Assert.Equal(10, ba.Size); v = ba[5]; b = ba.GetBlock(0); b.Offset = 0; b.Length = b.Buffer.Length; for (int i = 0; i < b.Length; i++) { b.Buffer[i] = (byte)i; } b = ba.GetBlock(1); b.Offset = 0; b.Length = b.Buffer.Length; for (int i = 0; i < b.Length; i++) { b.Buffer[i] = (byte)(i + 100); } ba.Reload(); Assert.Equal(20, ba.Size); Assert.Equal(5, ba[5]); }
public void Extract() { BlockArray ba; BlockArray ex; //------------------------- ba = new BlockArray(); ex = ba.Extract(0, 0); Assert.Equal(0, ex.Size); Assert.Equal(0, ex.Count); //------------------------- ba = new BlockArray(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); ex = ba.Extract(0, 10); Assert.Equal(10, ex.Size); Assert.Equal(1, ex.Count); Assert.Equal(0, ex.GetBlock(0).Offset); Assert.Equal(10, ex.GetBlock(0).Length); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ex.GetBlock(0).Buffer); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ex.ToByteArray()); ba.GetBlock(0).Offset = 5; ba.GetBlock(0).Length = 5; ba.Reload(); Assert.Equal(new byte[] { 5, 6, 7, 8, 9 }, ba.ToByteArray()); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ex.ToByteArray()); //------------------------- ba = new BlockArray(new Block(new byte[] { 0, 0, 0, 0, 0, 0, 1, 2, 3, 4 }, 5, 5), new Block(new byte[] { 0, 0, 0, 0, 0, 5, 6, 7, 8, 9 }, 5, 5)); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ba.ToByteArray()); ex = ba.Extract(0, 10); Assert.Equal(2, ex.Count); Assert.Equal(5, ex.GetBlock(0).Offset); Assert.Equal(5, ex.GetBlock(0).Length); Assert.Equal(5, ex.GetBlock(1).Offset); Assert.Equal(5, ex.GetBlock(1).Length); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ex.ToByteArray()); ex = ba.Extract(2, 5); Assert.Equal(2, ex.Count); Assert.Equal(7, ex.GetBlock(0).Offset); Assert.Equal(3, ex.GetBlock(0).Length); Assert.Equal(5, ex.GetBlock(1).Offset); Assert.Equal(2, ex.GetBlock(1).Length); Assert.Equal(new byte[] { 2, 3, 4, 5, 6 }, ex.ToByteArray()); ex = ba.Extract(5); Assert.Equal(new byte[] { 5, 6, 7, 8, 9 }, ex.ToByteArray()); }
/// <summary> /// Appends a block array to the end of the underlying BlockArray. /// </summary> /// <param name="blocks">The array to append.</param> /// <remarks> /// The underyling block array's SetExactSize() method will be /// called before appending the block. The stream position will /// be set to the end of the stream before the method returns. /// /// This method is a performance improvement over writing the /// a buffer to the stream via one of the write methods. /// </remarks> public void Append(BlockArray blocks) { this.blocks.SetExactSize(length); for (int i = 0; i < blocks.Count; i++) { this.blocks.Append(blocks.GetBlock(i)); } length += blocks.Size; pos = length; }
public void HttpRequest_Serialize() { HttpRequest r; byte[] buf; BlockArray blocks; //------------------------- r = new HttpRequest("get", "/foo.htm", null); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nContent-Length: 0\r\n\r\n"), r.Serialize(15).ToByteArray()); //------------------------- r.Content = new BlockArray(Encoding.ASCII.GetBytes("abcdefg")); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nContent-Length: 7\r\n\r\nabcdefg"), r.Serialize(15).ToByteArray()); //------------------------- r = new HttpRequest(); buf = Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nHeader1: Test1\r\nHeader2: Test2\r\nContent-Length: 4\r\n\r\nabcd"); r.BeginParse(80); Assert.IsTrue(r.Parse(buf, buf.Length)); r.EndParse(); buf = r.Serialize(3).ToByteArray(); blocks = new BlockArray(Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nHeader1: Test1\r\nHeader2: Test2\r\nContent-Length: 4\r\n\r\nabcd")); r = new HttpRequest(); r.BeginParse(80); for (int i = 0; i < blocks.Count; i++) { Block block = blocks.GetBlock(i); Assert.AreEqual(0, block.Offset); r.Parse(block.Buffer, block.Length); } r.EndParse(); Assert.AreEqual("GET", r.Method); Assert.AreEqual("/foo.htm", r.RawUri); Assert.AreEqual("4", r["Content-Length"]); Assert.AreEqual("Test1", r["Header1"]); Assert.AreEqual("Test2", r["Header2"]); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("abcd"), r.Content.ToByteArray()); }
public void HttpResponse_Serialize() { HttpResponse r; byte[] buf; BlockArray blocks; //------------------------- r = new HttpResponse(HttpStatus.OK, "OK"); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"), r.Serialize(15).ToByteArray()); //------------------------- r.Content = new BlockArray(Encoding.ASCII.GetBytes("abcdefg")); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\nabcdefg"), r.Serialize(15).ToByteArray()); //------------------------- r = new HttpResponse(); buf = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nHeader1: Test1\r\nHeader2: Test2\r\nContent-Length: 4\r\n\r\nabcd"); r.BeginParse(); Assert.IsTrue(r.Parse(buf, buf.Length)); r.EndParse(); buf = r.Serialize(3).ToByteArray(); blocks = new BlockArray(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nHeader1: Test1\r\nHeader2: Test2\r\nContent-Length: 4\r\n\r\nabcd")); r = new HttpResponse(); r.BeginParse(); for (int i = 0; i < blocks.Count; i++) { Block block = blocks.GetBlock(i); Assert.AreEqual(0, block.Offset); r.Parse(block.Buffer, block.Length); } r.EndParse(); Assert.AreEqual(HttpStatus.OK, r.Status); Assert.AreEqual("OK", r.Reason); Assert.AreEqual("4", r["Content-Length"]); Assert.AreEqual("Test1", r["Header1"]); Assert.AreEqual("Test2", r["Header2"]); CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("abcd"), r.Content.ToByteArray()); }
public void BlockOffset() { BlockArray ba; ba = new BlockArray(0, 10, 5); ba.ExtendTo(10); Assert.Equal(10, ba.Size); Assert.Equal(10, ba.GetBlock(0).Buffer.Length); Assert.Equal(5, ba.GetBlock(0).Length); Assert.Equal(5, ba.GetBlock(0).Offset); Assert.Equal(10, ba.GetBlock(1).Buffer.Length); Assert.Equal(5, ba.GetBlock(1).Length); Assert.Equal(5, ba.GetBlock(1).Offset); }
public void SetExactSize() { BlockArray blocks; blocks = new BlockArray(); blocks.SetExactSize(0); Assert.Equal(0, blocks.Size); Assert.Equal(0, blocks.Count); blocks.SetExactSize(1); Assert.Equal(1, blocks.Size); Assert.Equal(1, blocks.Count); Assert.Equal(1, blocks.GetBlock(0).Length); blocks.SetExactSize(blocks.BlockSize); Assert.Equal(blocks.BlockSize, blocks.Size); Assert.Equal(2, blocks.Count); Assert.Equal(1, blocks.GetBlock(0).Length); Assert.Equal(blocks.BlockSize - 1, blocks.GetBlock(1).Length); blocks.SetExactSize(blocks.BlockSize + 1); Assert.Equal(blocks.BlockSize + 1, blocks.Size); Assert.Equal(3, blocks.Count); Assert.Equal(1, blocks.GetBlock(0).Length); Assert.Equal(blocks.BlockSize - 1, blocks.GetBlock(1).Length); Assert.Equal(1, blocks.GetBlock(2).Length); blocks = new BlockArray(); blocks.ExtendTo(blocks.BlockSize * 3); blocks.SetExactSize(blocks.Size - blocks.BlockSize / 2); Assert.Equal(3, blocks.Count); Assert.Equal(blocks.BlockSize, blocks.GetBlock(0).Length); Assert.Equal(blocks.BlockSize, blocks.GetBlock(1).Length); Assert.Equal(blocks.BlockSize - blocks.BlockSize / 2, blocks.GetBlock(2).Length); blocks.SetExactSize(blocks.BlockSize * 2); Assert.Equal(2, blocks.Count); Assert.Equal(blocks.BlockSize, blocks.GetBlock(0).Length); Assert.Equal(blocks.BlockSize, blocks.GetBlock(1).Length); blocks.SetExactSize(blocks.BlockSize + 1); Assert.Equal(2, blocks.Count); Assert.Equal(blocks.BlockSize, blocks.GetBlock(0).Length); Assert.Equal(1, blocks.GetBlock(1).Length); }