public void TestPutPrefixedString() { Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); IoBuffer buf = IoBuffer.Allocate(16); buf.FillAndReset(buf.Remaining); // Without autoExpand buf.PutPrefixedString("ABC", encoding); Assert.AreEqual(5, buf.Position); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual(3, buf.Get(1)); Assert.AreEqual((byte)'A', buf.Get(2)); Assert.AreEqual((byte)'B', buf.Get(3)); Assert.AreEqual((byte)'C', buf.Get(4)); buf.Clear(); try { buf.PutPrefixedString("123456789012345", encoding); Assert.Fail(); } catch (OverflowException) { // Expected an Exception, signifies test success Assert.IsTrue(true); } // With autoExpand buf.Clear(); buf.AutoExpand = true; buf.PutPrefixedString("123456789012345", encoding); Assert.AreEqual(17, buf.Position); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual(15, buf.Get(1)); Assert.AreEqual((byte)'1', buf.Get(2)); Assert.AreEqual((byte)'2', buf.Get(3)); Assert.AreEqual((byte)'3', buf.Get(4)); Assert.AreEqual((byte)'4', buf.Get(5)); Assert.AreEqual((byte)'5', buf.Get(6)); Assert.AreEqual((byte)'6', buf.Get(7)); Assert.AreEqual((byte)'7', buf.Get(8)); Assert.AreEqual((byte)'8', buf.Get(9)); Assert.AreEqual((byte)'9', buf.Get(10)); Assert.AreEqual((byte)'0', buf.Get(11)); Assert.AreEqual((byte)'1', buf.Get(12)); Assert.AreEqual((byte)'2', buf.Get(13)); Assert.AreEqual((byte)'3', buf.Get(14)); Assert.AreEqual((byte)'4', buf.Get(15)); Assert.AreEqual((byte)'5', buf.Get(16)); }
public void TestObjectSerialization() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(16); buf.AutoExpand = true; List <Object> o = new List <Object>(); o.Add(new DateTime()); o.Add(typeof(Int64)); // Test writing an object. buf.PutObject(o); // Test reading an object. buf.Clear(); Object o2 = buf.GetObject(); #if !NETFX_CORE Assert.IsInstanceOf(o.GetType(), o2); #else Assert.IsInstanceOfType(o2, o.GetType()); #endif List <Object> l2 = (List <Object>)o2; Assert.AreEqual(o.Count, l2.Count); for (Int32 i = 0; i < o.Count; i++) { Assert.AreEqual(o[i], l2[i]); } // This assertion is just to make sure that deserialization occurred. Assert.AreNotSame(o, o2); }
public void TestGetPrefixedString() { IoBuffer buf = IoBuffer.Allocate(16); Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); buf.PutInt16((short)3); buf.PutString("ABCD", encoding); buf.Clear(); Assert.AreEqual("ABC", buf.GetPrefixedString(encoding)); }
public void TestWrapSubArray() { byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IoBuffer buf = ByteBufferAllocator.Instance.Wrap(array, 3, 4); Assert.AreEqual(3, buf.Position); Assert.AreEqual(7, buf.Limit); Assert.AreEqual(10, buf.Capacity); buf.Clear(); Assert.AreEqual(0, buf.Position); Assert.AreEqual(10, buf.Limit); Assert.AreEqual(10, buf.Capacity); }
public void TestSweepNonZeros() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(4); Int32 i; unchecked { i = (Int32)0xdeadbeef; } buf.PutInt32(i); buf.Clear(); Assert.AreEqual(i, buf.GetInt32()); Assert.AreEqual(4, buf.Position); Assert.AreEqual(4, buf.Limit); buf.Sweep((byte)0x45); Assert.AreEqual(0, buf.Position); Assert.AreEqual(4, buf.Limit); Assert.AreEqual(0x45454545, buf.GetInt32()); }
public void TestSweepWithZeros() { IoBuffer buf = allocator.Allocate(4); Int32 i; unchecked { i = (Int32)0xdeadbeef; } buf.PutInt32(i); buf.Clear(); Assert.AreEqual(i, buf.GetInt32()); Assert.AreEqual(4, buf.Position); Assert.AreEqual(4, buf.Limit); buf.Sweep(); Assert.AreEqual(0, buf.Position); Assert.AreEqual(4, buf.Limit); Assert.AreEqual(0x0, buf.GetInt32()); }
public void TestPutString() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(16); Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); buf.PutString("ABC", encoding); Assert.AreEqual(3, buf.Position); buf.Clear(); Assert.AreEqual((Byte)'A', buf.Get(0)); Assert.AreEqual((Byte)'B', buf.Get(1)); Assert.AreEqual((Byte)'C', buf.Get(2)); buf.PutString("D", 5, encoding); Assert.AreEqual(5, buf.Position); buf.Clear(); Assert.AreEqual((Byte)'D', buf.Get(0)); Assert.AreEqual(0, buf.Get(1)); buf.PutString("EFG", 2, encoding); Assert.AreEqual(2, buf.Position); buf.Clear(); Assert.AreEqual((Byte)'E', buf.Get(0)); Assert.AreEqual((Byte)'F', buf.Get(1)); Assert.AreEqual((Byte)'C', buf.Get(2)); // C may not be overwritten // UTF-16: We specify byte order to omit BOM. encoding = Encoding.GetEncoding("UTF-16BE"); buf.Clear(); buf.PutString("ABC", encoding); Assert.AreEqual(6, buf.Position); buf.Clear(); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual((Byte)'A', buf.Get(1)); Assert.AreEqual(0, buf.Get(2)); Assert.AreEqual((Byte)'B', buf.Get(3)); Assert.AreEqual(0, buf.Get(4)); Assert.AreEqual((Byte)'C', buf.Get(5)); buf.PutString("D", 10, encoding); Assert.AreEqual(10, buf.Position); buf.Clear(); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual((Byte)'D', buf.Get(1)); Assert.AreEqual(0, buf.Get(2)); Assert.AreEqual(0, buf.Get(3)); buf.PutString("EFG", 4, encoding); Assert.AreEqual(4, buf.Position); buf.Clear(); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual((Byte)'E', buf.Get(1)); Assert.AreEqual(0, buf.Get(2)); Assert.AreEqual((Byte)'F', buf.Get(3)); Assert.AreEqual(0, buf.Get(4)); // C may not be overwritten Assert.AreEqual((Byte)'C', buf.Get(5)); // C may not be overwritten // Test putting an emptry string buf.PutString("", encoding); Assert.AreEqual(0, buf.Position); buf.PutString("", 4, encoding); Assert.AreEqual(4, buf.Position); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual(0, buf.Get(1)); }
public void TestGetString() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(16); Encoding encoding = Encoding.UTF8; buf.Clear(); buf.PutString("hello", encoding); buf.Put((Byte)0); buf.Flip(); Assert.AreEqual("hello", buf.GetString(encoding)); buf.Clear(); buf.PutString("hello", encoding); buf.Flip(); Assert.AreEqual("hello", buf.GetString(encoding)); encoding = Encoding.GetEncoding("ISO-8859-1"); buf.Clear(); buf.Put((Byte)'A'); buf.Put((Byte)'B'); buf.Put((Byte)'C'); buf.Put((Byte)0); buf.Position = 0; Assert.AreEqual("ABC", buf.GetString(encoding)); Assert.AreEqual(4, buf.Position); buf.Position = 0; buf.Limit = 1; Assert.AreEqual("A", buf.GetString(encoding)); Assert.AreEqual(1, buf.Position); buf.Clear(); Assert.AreEqual("ABC", buf.GetString(10, encoding)); Assert.AreEqual(10, buf.Position); buf.Clear(); Assert.AreEqual("A", buf.GetString(1, encoding)); Assert.AreEqual(1, buf.Position); // Test a trailing garbage buf.Clear(); buf.Put((Byte)'A'); buf.Put((Byte)'B'); buf.Put((Byte)0); buf.Put((Byte)'C'); buf.Position = 0; Assert.AreEqual("AB", buf.GetString(4, encoding)); Assert.AreEqual(4, buf.Position); buf.Clear(); buf.FillAndReset(buf.Limit); encoding = Encoding.GetEncoding("UTF-16BE"); buf.Put((Byte)0); buf.Put((Byte)'A'); buf.Put((Byte)0); buf.Put((Byte)'B'); buf.Put((Byte)0); buf.Put((Byte)'C'); buf.Put((Byte)0); buf.Put((Byte)0); buf.Position = 0; Assert.AreEqual("ABC", buf.GetString(encoding)); Assert.AreEqual(8, buf.Position); buf.Position = 0; buf.Limit = 2; Assert.AreEqual("A", buf.GetString(encoding)); Assert.AreEqual(2, buf.Position); buf.Position = 0; buf.Limit = 3; Assert.AreEqual("A", buf.GetString(encoding)); Assert.AreEqual(2, buf.Position); buf.Clear(); Assert.AreEqual("ABC", buf.GetString(10, encoding)); Assert.AreEqual(10, buf.Position); buf.Clear(); Assert.AreEqual("A", buf.GetString(2, encoding)); Assert.AreEqual(2, buf.Position); buf.Clear(); try { buf.GetString(1, encoding); Assert.Fail(); } catch (Exception) { // Expected an Exception, signifies test success Assert.IsTrue(true); } // Test getting strings from an empty buffer. buf.Clear(); buf.Limit = 0; Assert.AreEqual("", buf.GetString(encoding)); Assert.AreEqual("", buf.GetString(2, encoding)); // Test getting strings from non-empty buffer which is filled with 0x00 buf.Clear(); buf.PutInt32(0); buf.Clear(); buf.Limit = 4; Assert.AreEqual("", buf.GetString(encoding)); Assert.AreEqual(2, buf.Position); Assert.AreEqual(4, buf.Limit); buf.Position = 0; Assert.AreEqual("", buf.GetString(2, encoding)); Assert.AreEqual(2, buf.Position); Assert.AreEqual(4, buf.Limit); }
public void TestAutoShrink() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(8); buf.AutoShrink = true; // Make sure the buffer doesn't shrink too much (less than the initial // capacity.) buf.Sweep((byte)1); buf.Fill(7); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(1, buf.Position); Assert.AreEqual(8, buf.Limit); buf.Clear(); Assert.AreEqual(1, buf.Get()); // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when only 1/4 is being used. buf.Sweep((byte)1); buf.Fill(24); buf.Compact(); Assert.AreEqual(16, buf.Capacity); Assert.AreEqual(8, buf.Position); Assert.AreEqual(16, buf.Limit); buf.Clear(); for (int i = 0; i < 8; i++) { Assert.AreEqual(1, buf.Get()); } // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when only 1/8 is being used. buf.Sweep((byte)1); buf.Fill(28); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(4, buf.Position); Assert.AreEqual(8, buf.Limit); buf.Clear(); for (int i = 0; i < 4; i++) { Assert.AreEqual(1, buf.Get()); } // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when 0 byte is being used. buf.Fill(32); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(0, buf.Position); Assert.AreEqual(8, buf.Limit); // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer doesn't shrink when more than 1/4 is being used. buf.Sweep((byte)1); buf.Fill(23); buf.Compact(); Assert.AreEqual(32, buf.Capacity); Assert.AreEqual(9, buf.Position); Assert.AreEqual(32, buf.Limit); buf.Clear(); for (int i = 0; i < 9; i++) { Assert.AreEqual(1, buf.Get()); } }
private void InternalFlush(INextFilter nextFilter, IoSession session, IoBuffer buf) { IoBuffer tmp = null; lock (buf) { buf.Flip(); tmp = buf.Duplicate(); buf.Clear(); } if (log.IsDebugEnabled) log.Debug("Flushing buffer: " + tmp); nextFilter.FilterWrite(session, new DefaultWriteRequest(tmp)); }