/// <summary> /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. /// </summary> /// <returns> /// The unsigned byte cast to an Int32, or -1 if at the end of the stream. /// </returns> public override int ReadByte() { if (recvDataCache == null || !recvDataCache.IsReadable()) { recvDataCache = streamSocket.Receive(); } return(recvDataCache.ReadByte()); }
/// <summary> /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. /// </summary> /// <returns> /// The unsigned byte cast to an Int32, or -1 if at the end of the stream. /// </returns> public override int ReadByte() { if (recvDataCache == null || !recvDataCache.IsReadable()) { recvDataCache = streamSocket.Receive(); } var v = recvDataCache.ReadByte(); if (recvDataCache.IsReadable()) { return(v); } // Release cache if it is not readable. If we do not reset the cache here, // the cache will be used in next Read() that caused 0 bytes return. recvDataCache.Release(); recvDataCache = null; return(v); }
private void WriteReadByteBufTest(ByteBuf byteBuf) { var initWriteIndex = byteBuf.WriterIndex; var initReadIndex = byteBuf.WriterIndex; Assert.AreEqual(initWriteIndex, initReadIndex); Assert.AreEqual(byteBuf.Capacity, byteBuf.WritableBytes); Assert.AreEqual(0, byteBuf.ReadableBytes); Assert.IsFalse(byteBuf.IsReadable()); Assert.IsTrue(byteBuf.IsWritable()); // Verify WriteBytes() function const string writeStr = "Write bytes to ByteBuf."; var writeBytes = Encoding.UTF8.GetBytes(writeStr); byteBuf.WriteBytes(writeBytes, 0, writeBytes.Length); Assert.AreEqual(initWriteIndex + writeBytes.Length, byteBuf.WriterIndex); Assert.AreEqual(byteBuf.Capacity - writeBytes.Length, byteBuf.WritableBytes); Assert.AreEqual(writeBytes.Length, byteBuf.ReadableBytes); Assert.AreEqual(initReadIndex, byteBuf.ReaderIndex); Assert.IsTrue(byteBuf.IsReadable()); // Verify ReadBytes() function var readBytes = new byte[writeBytes.Length]; var ret = byteBuf.ReadBytes(readBytes, 0, readBytes.Length); Assert.AreEqual(writeBytes.Length, ret); var readStr = Encoding.UTF8.GetString(readBytes, 0, ret); Assert.AreEqual(writeStr, readStr); Assert.AreEqual(initWriteIndex + writeBytes.Length, byteBuf.WriterIndex); Assert.AreEqual(byteBuf.Capacity - writeBytes.Length, byteBuf.WritableBytes); Assert.AreEqual(0, byteBuf.ReadableBytes); Assert.AreEqual(initReadIndex + ret, byteBuf.ReaderIndex); // Verify ReadByte() function byteBuf.WriteBytes(writeBytes, 0, 1); var retByte = byteBuf.ReadByte(); Assert.AreEqual(writeBytes[0], retByte); // Verify clear() function byteBuf.Clear(); Assert.AreEqual(0, byteBuf.ReaderIndex); Assert.AreEqual(0, byteBuf.WriterIndex); }