public void TestShrink2() { IoBuffer buf = IoBuffer.Allocate(36); buf.Put(Encoding.Default.GetBytes("012345")); buf.Flip(); buf.Position = 4; buf.MinimumCapacity = 8; IoBuffer newBuf = buf.Shrink(); Assert.AreEqual(4, newBuf.Position); Assert.AreEqual(6, newBuf.Limit); Assert.AreEqual(9, newBuf.Capacity); Assert.AreEqual(8, newBuf.MinimumCapacity); buf = IoBuffer.Allocate(6); buf.Put(Encoding.Default.GetBytes("012345")); buf.Flip(); buf.Position = 4; newBuf = buf.Shrink(); Assert.AreEqual(4, newBuf.Position); Assert.AreEqual(6, newBuf.Limit); Assert.AreEqual(6, newBuf.Capacity); Assert.AreEqual(6, newBuf.MinimumCapacity); }
public void TestCapacity() { IoBuffer buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); // See if we can decrease the capacity (we shouldn't be able to go under the minimul capacity) buffer.Capacity = 7; Assert.AreEqual(10, buffer.Capacity); // See if we can increase the capacity buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); buffer.Capacity = 14; Assert.AreEqual(14, buffer.Capacity); buffer.Put(0, (byte)'9'); Assert.AreEqual((byte)'9', buffer.Get(0)); Assert.AreEqual((byte)'9', buffer.Get(0)); // See if we can go down when the minimum capacity is below the current capacity // We should not. buffer = IoBuffer.Allocate(10); buffer.Capacity = 5; Assert.AreEqual(10, buffer.MinimumCapacity); Assert.AreEqual(10, buffer.Capacity); }
public void TestGetSlice() { IoBuffer buf = IoBuffer.Allocate(36); for (Byte i = 0; i < 36; i++) { buf.Put(i); } IoBuffer res = buf.GetSlice(1, 3); // The limit should be 3, the pos should be 0 and the bytes read // should be 0x01, 0x02 and 0x03 Assert.AreEqual(0, res.Position); Assert.AreEqual(3, res.Limit); Assert.AreEqual(0x01, res.Get()); Assert.AreEqual(0x02, res.Get()); Assert.AreEqual(0x03, res.Get()); // Now test after a flip buf.Flip(); res = buf.GetSlice(1, 3); // The limit should be 3, the pos should be 0 and the bytes read // should be 0x01, 0x02 and 0x03 Assert.AreEqual(0, res.Position); Assert.AreEqual(3, res.Limit); Assert.AreEqual(0x01, res.Get()); Assert.AreEqual(0x02, res.Get()); Assert.AreEqual(0x03, res.Get()); }
public bool Send(IoBuffer output) { try { output.Flip(); var buffer = output.GetRemainingArray(); Client.Client.Send(buffer); } catch (Exception) { return false; } if (handler != null) return handler.Await(); return true; }
public void TestFillByteSize() { int length = 1024 * 1020; IoBuffer buffer = IoBuffer.Allocate(length); buffer.Fill((byte)0x80, length); buffer.Flip(); for (int i = 0; i < length; i++) { Assert.AreEqual((byte)0x80, buffer.Get()); } }
public void TestGetUnsigned() { IoBuffer buf = IoBuffer.Allocate(16); buf.Put((byte)0xA4); buf.Put((byte)0xD0); buf.Put((byte)0xB3); buf.Put((byte)0xCD); buf.Flip(); buf.Order = ByteOrder.LittleEndian; buf.Mark(); Assert.AreEqual(0xA4, buf.Get()); buf.Reset(); Assert.AreEqual(0xD0A4, (UInt16)buf.GetInt16()); buf.Reset(); Assert.AreEqual(0xCDB3D0A4L, (UInt32)buf.GetInt32()); }
public void TestExpandPos() { IoBuffer buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); Assert.AreEqual(6, buffer.Remaining); // See if we can expand with a lower number of remaining bytes. We should not. IoBuffer newBuffer = buffer.Expand(3, 2); Assert.AreEqual(6, newBuffer.Limit); Assert.AreEqual(10, newBuffer.Capacity); Assert.AreEqual(0, newBuffer.Position); // Now, let's expand the buffer above the number of current bytes but below the limit buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); newBuffer = buffer.Expand(3, 5); Assert.AreEqual(8, newBuffer.Limit); Assert.AreEqual(10, newBuffer.Capacity); Assert.AreEqual(0, newBuffer.Position); // Last, expand the buffer above the limit buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); newBuffer = buffer.Expand(3, 9); Assert.AreEqual(12, newBuffer.Limit); Assert.AreEqual(12, newBuffer.Capacity); Assert.AreEqual(0, newBuffer.Position); // Now, move forward in the buffer buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); buffer.Position = 4; // See if we can expand with a lower number of remaining bytes. We should not be. newBuffer = buffer.Expand(5, 1); Assert.AreEqual(6, newBuffer.Limit); Assert.AreEqual(10, newBuffer.Capacity); Assert.AreEqual(4, newBuffer.Position); // Expand above the current limit buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); buffer.Position = 4; newBuffer = buffer.Expand(5, 2); Assert.AreEqual(7, newBuffer.Limit); Assert.AreEqual(10, newBuffer.Capacity); Assert.AreEqual(4, newBuffer.Position); // Expand above the current capacity buffer = IoBuffer.Allocate(10); buffer.Put(Encoding.Default.GetBytes("012345")); buffer.Flip(); buffer.Position = 4; newBuffer = buffer.Expand(5, 6); Assert.AreEqual(11, newBuffer.Limit); Assert.AreEqual(11, newBuffer.Capacity); Assert.AreEqual(4, newBuffer.Position); }
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); }
private void ManipulateIoBuffer(IoSession session, IoBuffer buffer) { if ((buffer.Remaining > 0) && (_removeByteProbability > _rng.Next(1000))) { if (log.IsInfoEnabled) log.Info(buffer.GetHexDump()); // where to remove bytes ? int pos = _rng.Next(buffer.Remaining); // how many byte to remove ? int count = _rng.Next(buffer.Remaining - pos) + 1; if (count == buffer.Remaining) count = buffer.Remaining - 1; IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining - count); for (int i = 0; i < pos; i++) newBuff.Put(buffer.Get()); buffer.Skip(count); // hole while (newBuff.Remaining > 0) newBuff.Put(buffer.Get()); newBuff.Flip(); // copy the new buffer in the old one buffer.Rewind(); buffer.Put(newBuff); buffer.Flip(); if (log.IsInfoEnabled) { log.Info("Removed " + count + " bytes at position " + pos + "."); log.Info(buffer.GetHexDump()); } } if ((buffer.Remaining > 0) && (_changeByteProbability > _rng.Next(1000))) { if (log.IsInfoEnabled) log.Info(buffer.GetHexDump()); // how many byte to change ? int count = _rng.Next(buffer.Remaining - 1) + 1; byte[] values = new byte[count]; _rng.NextBytes(values); for (int i = 0; i < values.Length; i++) { int pos = _rng.Next(buffer.Remaining); buffer.Put(pos, values[i]); } if (log.IsInfoEnabled) { log.Info("Modified " + count + " bytes."); log.Info(buffer.GetHexDump()); } } }
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)); }