コード例 #1
0
        protected override AbstractMessage DecodeBody(IoSession session, IoBuffer input)
        {
            if (!_readCode)
            {
                if (input.Remaining < Constants.RESULT_CODE_LEN)
                {
                    return null; // Need more data.
                }

                _code = input.GetInt16();
                _readCode = true;
            }

            if (_code == Constants.RESULT_OK)
            {
                if (input.Remaining < Constants.RESULT_VALUE_LEN)
                {
                    return null;
                }

                ResultMessage m = new ResultMessage();
                m.OK = true;
                m.Value = input.GetInt32();
                _readCode = false;
                return m;
            }
            else
            {
                ResultMessage m = new ResultMessage();
                m.OK = false;
                _readCode = false;
                return m;
            }
        }
コード例 #2
0
ファイル: IoBufferWrapper.cs プロジェクト: zhangf911/Mina.NET
 /// <summary>
 /// </summary>
 protected IoBufferWrapper(IoBuffer buf)
     : base(-1, 0, 0, 0)
 {
     if (buf == null)
         throw new ArgumentNullException("buf");
     _buf = buf;
 }
コード例 #3
0
        public static String GetHexdump(IoBuffer buf, Int32 lengthLimit)
        {
            if (lengthLimit <= 0)
                throw new ArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)");
            Boolean truncate = buf.Remaining > lengthLimit;
            Int32 size = truncate ? lengthLimit : buf.Remaining;

            if (size == 0)
                return "empty";

            StringBuilder sb = new StringBuilder(size * 3 + 3);
            Int32 oldPos = buf.Position;

            // fill the first
            Int32 byteValue = buf.Get() & 0xFF;
            sb.Append((char)highDigits[byteValue]);
            sb.Append((char)lowDigits[byteValue]);
            size--;

            // and the others, too
            for (; size > 0; size--)
            {
                sb.Append(' ');
                byteValue = buf.Get() & 0xFF;
                sb.Append((char)highDigits[byteValue]);
                sb.Append((char)lowDigits[byteValue]);
            }

            buf.Position = oldPos;

            if (truncate)
                sb.Append("...");

            return sb.ToString();
        }
コード例 #4
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                if (input.Remaining >= _length)
                {
                    Int32 limit = input.Limit;
                    input.Limit = input.Position + _length;
                    IoBuffer product = input.Slice();
                    input.Position = input.Position + _length;
                    input.Limit = limit;
                    return FinishDecode(product, output);
                }

                _buffer = IoBuffer.Allocate(_length);
                _buffer.Put(input);
                return this;
            }

            if (input.Remaining >= _length - _buffer.Position)
            {
                Int32 limit = input.Limit;
                input.Limit = input.Position + _length - _buffer.Position;
                _buffer.Put(input);
                input.Limit = limit;
                IoBuffer product = _buffer;
                _buffer = null;
                return FinishDecode(product.Flip(), output);
            }

            _buffer.Put(input);
            return this;
        }
コード例 #5
0
ファイル: Primitives.cs プロジェクト: Egipto87/DOOP.ec
        static void WriteVarint32(IoBuffer buffer, uint value)
        {
            for (; value >= 0x80u; value >>= 7)
                buffer.Put((byte)(value | 0x80u));

            buffer.Put((byte)value);
        }
コード例 #6
0
 public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.Decode(session, input, output);
     }
 }
コード例 #7
0
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            // Try to skip header if not read.
            if (!_readHeader)
            {
                input.GetInt16(); // Skip 'type'.
                _sequence = input.GetInt32(); // Get 'sequence'.
                _readHeader = true;
            }

            // Try to decode body
            AbstractMessage m = DecodeBody(session, input);
            // Return NEED_DATA if the body is not fully read.
            if (m == null)
            {
                return MessageDecoderResult.NeedData;
            }
            else
            {
                _readHeader = false; // reset readHeader for the next decode
            }
            m.Sequence = _sequence;
            output.Write(m);

            return MessageDecoderResult.OK;
        }
コード例 #8
0
        public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
        {
            if ((MessageType)input.Get() != MessageType.Update_ZipFiles)
            {
                return MessageDecoderResult.NotOK;
            }

            var zipFileInfo = JsonConvert.DeserializeObject<TransferingZipFileInfo>(input.GetString(Encoding.UTF8));
            var fileSize = zipFileInfo.FileSize;
            var hashBytes = zipFileInfo.HashBytes;
            if (input.Remaining < fileSize)
            {
                return MessageDecoderResult.NeedData;
            }

            var filesBytes = new byte[fileSize];
            input.Get(filesBytes, 0, (int)fileSize);

            if (FileHashHelper.CompareHashValue(FileHashHelper.ComputeFileHash(filesBytes), hashBytes))
            {
                _zipFileInfoMessage = new TransferingZipFile(filesBytes);
                return MessageDecoderResult.OK;
            }
            return MessageDecoderResult.NotOK;
        }
コード例 #9
0
ファイル: IoSessionStream.cs プロジェクト: zhangf911/Mina.NET
 /// <summary>
 /// </summary>
 public IoSessionStream()
 {
     _syncRoot = new Byte[0];
     _buf = IoBuffer.Allocate(16);
     _buf.AutoExpand = true;
     _buf.Limit = 0;
 }
コード例 #10
0
ファイル: DuplexMessageDecoder.cs プロジェクト: zesus19/c5.v1
 protected override object DoDecode(MessageVersion version, IoBuffer input)
 {
     using (var scope = ObjectHost.Host.BeginLifetimeScope())
     {
         return scope.ResolveKeyed<IMessageReader<DuplexMessage>>(version).Read(input);
     }
 }
コード例 #11
0
 /// <inheritdoc/>
 protected override void BeginSend(IWriteRequest request, IoBuffer buf)
 {
     EndPoint destination = request.Destination;
     if (destination == null)
         destination = this.RemoteEndPoint;
     BeginSend(buf, destination);
 }
コード例 #12
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.HasRemaining)
                return FinishDecode(input.Get(), output);

            return this;
        }
コード例 #13
0
ファイル: AbstractPacket.cs プロジェクト: zesus19/c5.v1
 private byte ComputeChecksum(IoBuffer buffer)
 {
     buffer.Rewind();
     byte checksum = 0;
     for (int i = 0; i < Length - 1; i++) checksum += buffer.Get();
     return checksum;
 }
コード例 #14
0
        public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_session == null)
                _session = session;
            else if (_session != session)
                throw new InvalidOperationException(GetType().Name + " is a stateful decoder.  "
                        + "You have to create one per session.");

            _undecodedBuffers.Enqueue(input);
            while (true)
            {
                IoBuffer b;
                if (!_undecodedBuffers.TryPeek(out b))
                    break;

                Int32 oldRemaining = b.Remaining;
                _state.Decode(b, output);
                Int32 newRemaining = b.Remaining;
                if (newRemaining != 0)
                {
                    if (oldRemaining == newRemaining)
                        throw new InvalidOperationException(_state.GetType().Name
                            + " must consume at least one byte per decode().");
                }
                else
                {
                    _undecodedBuffers.TryDequeue(out b);
                }
            }
        }
コード例 #15
0
        internal ParameterListEncapsulation(IoBuffer bb)
        {
#if TODO
        this.options = bb.GetInt16();
        this.parameters = bb.GetParameterList();
#endif
            throw new NotImplementedException();
        }
コード例 #16
0
ファイル: DataPacket.cs プロジェクト: zesus19/c5.v1
 protected override void Fill(IoBuffer buffer)
 {
     buffer.Put(Length);
     buffer.Put(FRAME_SOURCE_ADDR);
     buffer.Put(Protocol);
     buffer.Put(FRAME_SEQUENCE_VALUE);
     this.FillData(buffer);
 }
コード例 #17
0
 public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
 {
     if ((MessageType)input.Get() != MessageType.Update_UpdateInfo)
     {
         return MessageDecoderResult.NotOK;
     }
     _appUdateInfo = (IClientInfo)JsonConvert.DeserializeObject(input.GetString(Encoding.UTF8));
     return MessageDecoderResult.OK;
 }
コード例 #18
0
        /// <inheritdoc/>
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (!input.PrefixedDataAvailable(4, _maxObjectSize))
                return false;

            input.GetInt32();
            output.Write(input.GetObject());
            return true;
        }
コード例 #19
0
        private void TestDecoderAndInputStream(String expected, IoBuffer input)
        {
            // Test ProtocolDecoder
            IProtocolDecoder decoder = new ObjectSerializationDecoder();
            ProtocolCodecSession session = new ProtocolCodecSession();
            IProtocolDecoderOutput decoderOut = session.DecoderOutput;
            decoder.Decode(session, input.Duplicate(), decoderOut);

            Assert.AreEqual(1, session.DecoderOutputQueue.Count);
            Assert.AreEqual(expected, session.DecoderOutputQueue.Dequeue());
        }
コード例 #20
0
ファイル: LocatorEncoder.cs プロジェクト: Egipto87/DOOP.ec
        public static void ReadLocator(IoBuffer buffer, ref Locator obj)
        {
            if (obj == null)
                obj = new Locator();
            obj.Kind = (LocatorKind)buffer.GetInt32();
            obj.Port = (int)buffer.GetInt32(); ;
            byte[] tmp = new byte[16];

            buffer.Get(tmp, 0, 16);
            obj.SocketAddressBytes = tmp;
        }
コード例 #21
0
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.PrefixedDataAvailable(PrefixLength, MaxDataLength))
            {
                String msg = input.GetPrefixedString(PrefixLength, Encoding);
                output.Write(msg);
                return true;
            }

            return false;
        }
コード例 #22
0
        protected override Message.AbstractMessage DecodeBody(IoSession session, IoBuffer input)
        {
            if (input.Remaining < Constants.ADD_BODY_LEN)
            {
                return null;
            }

            AddMessage m = new AddMessage();
            m.Value = input.GetInt32();
            return m;
        }
コード例 #23
0
 public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
 {
     var type=(MessageType)input.Get();
     if (type==MessageType.Update_FileHash)
     {
         var message = input.GetString(Encoding.UTF8);
         _decodeMessage=JsonConvert.DeserializeObject<IList<IFileHash>>(message);
         return MessageDecoderResult.OK;
     }
     return MessageDecoderResult.NotOK;
 }
コード例 #24
0
 /// <inheritdoc/>
 public Int32 Read(IoBuffer buffer)
 {
     using (FileStream fs = _file.OpenRead())
     {
         fs.Position = _position;
         Byte[] bytes = new Byte[buffer.Remaining];
         Int32 read = fs.Read(bytes, 0, bytes.Length);
         buffer.Put(bytes, 0, read);
         Update(read);
         return read;
     }
 }
コード例 #25
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 terminatorPos = input.IndexOf(_terminator);

            if (terminatorPos >= 0)
            {
                Int32 limit = input.Limit;
                IoBuffer product;

                if (input.Position < terminatorPos)
                {
                    input.Limit = terminatorPos;

                    if (_buffer == null)
                    {
                        product = input.Slice();
                    }
                    else
                    {
                        _buffer.Put(input);
                        product = _buffer.Flip();
                        _buffer = null;
                    }

                    input.Limit = limit;
                }
                else
                {
                    // When input contained only terminator rather than actual data...
                    if (_buffer == null)
                    {
                        product = IoBuffer.Allocate(0);
                    }
                    else
                    {
                        product = _buffer.Flip();
                        _buffer = null;
                    }
                }
                input.Position = terminatorPos + 1;
                return FinishDecode(product, output);
            }

            if (_buffer == null)
            {
                _buffer = IoBuffer.Allocate(input.Remaining);
                _buffer.AutoExpand = true;
            }

            _buffer.Put(input);
            return this;
        }
コード例 #26
0
 protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     if (input.Remaining >= 4)
     {
         Message request = input.GetMessage();
         output.Write(request);
         return true;
     }
     else
     {
         return false;
     }
 }
コード例 #27
0
ファイル: SimpleTcpClient.cs プロジェクト: zesus19/c5.v1
 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;
 }
コード例 #28
0
        public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
        {
            // Return NeedData if the whole header is not read yet.
            if (input.Remaining < Constants.HEADER_LEN)
                return MessageDecoderResult.NeedData;

            // Return OK if type and bodyLength matches.
            if (_type == input.GetInt16())
                return MessageDecoderResult.OK;

            // Return NotOK if not matches.
            return MessageDecoderResult.NotOK;
        }
コード例 #29
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                _buffer = IoBuffer.Allocate(256);
                _buffer.AutoExpand = true;
            }

            if (_buffer.Position + input.Remaining > _maxLength)
                throw new ProtocolDecoderException("Received data exceeds " + _maxLength + " byte(s).");
         
            _buffer.Put(input);
            return this;
        }
コード例 #30
0
 public IDecodingState FinishDecode(IProtocolDecoderOutput output)
 {
     IoBuffer readData;
     if (_buffer == null)
     {
         readData = IoBuffer.Allocate(0);
     }
     else
     {
         readData = _buffer.Flip();
         _buffer = null;
     }
     return FinishDecode(readData, output);
 }
コード例 #31
0
        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());
        }
コード例 #32
0
        public static String GetHexdump(IoBuffer buf, Int32 lengthLimit)
        {
            if (lengthLimit <= 0)
            {
                throw new ArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)");
            }
            Boolean truncate = buf.Remaining > lengthLimit;
            Int32   size     = truncate ? lengthLimit : buf.Remaining;

            if (size == 0)
            {
                return("empty");
            }

            StringBuilder sb     = new StringBuilder(size * 3 + 3);
            Int32         oldPos = buf.Position;

            // fill the first
            Int32 byteValue = buf.Get() & 0xFF;

            sb.Append((char)highDigits[byteValue]);
            sb.Append((char)lowDigits[byteValue]);
            size--;

            // and the others, too
            for (; size > 0; size--)
            {
                sb.Append(' ');
                byteValue = buf.Get() & 0xFF;
                sb.Append((char)highDigits[byteValue]);
                sb.Append((char)lowDigits[byteValue]);
            }

            buf.Position = oldPos;

            if (truncate)
            {
                sb.Append("...");
            }

            return(sb.ToString());
        }
コード例 #33
0
ファイル: ByteBuffer.cs プロジェクト: xlg210/Mina.NET
        /// <inheritdoc/>
        protected override void PutInternal(IoBuffer src)
        {
            ByteBuffer bb = src as ByteBuffer;

            if (bb == null)
            {
                base.PutInternal(src);
            }
            else
            {
                Int32 n = bb.Remaining;
                if (n > Remaining)
                {
                    throw new OverflowException();
                }
                System.Buffer.BlockCopy(bb._hb, bb.Offset(bb.Position), _hb, Offset(Position), n);
                bb.Position += n;
                Position    += n;
            }
        }
コード例 #34
0
        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());
        }
コード例 #35
0
        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());
        }
コード例 #36
0
        public void TestAutoExpandMark()
        {
            IoBuffer buf = ByteBufferAllocator.Instance.Allocate(4);

            buf.AutoExpand = true;

            buf.Put((byte)0);
            buf.Put((byte)0);
            buf.Put((byte)0);

            // Position should be 3 when we reset this buffer.
            buf.Mark();

            // Overflow it
            buf.Put((byte)0);
            buf.Put((byte)0);

            Assert.AreEqual(5, buf.Position);
            buf.Reset();
            Assert.AreEqual(3, buf.Position);
        }
コード例 #37
0
        public void TestIndexOf()
        {
            for (int i = 0; i < 2; i++)
            {
                IoBuffer buf = IoBuffer.Allocate(16);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Position = 2;
                buf.Limit    = 5;

                Assert.AreEqual(4, buf.IndexOf((byte)0x1));
                Assert.AreEqual(-1, buf.IndexOf((byte)0x2));
                Assert.AreEqual(2, buf.IndexOf((byte)0x3));
                Assert.AreEqual(3, buf.IndexOf((byte)0x4));
            }
        }
コード例 #38
0
ファイル: AbstractIoBuffer.cs プロジェクト: xlg210/Mina.NET
        /// <inheritdoc/>
        public override IoBuffer GetSlice(Int32 length)
        {
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            Int32 pos     = Position;
            Int32 limit   = Limit;
            Int32 nextPos = pos + length;

            if (limit < nextPos)
            {
                throw new IndexOutOfRangeException("position + length (" + nextPos + ") is greater "
                                                   + "than limit (" + limit + ").");
            }

            Limit = pos + length;
            IoBuffer slice = Slice();

            Position = nextPos;
            Limit    = limit;
            return(slice);
        }
コード例 #39
0
 /// <summary>
 /// </summary>
 public IoBufferStream(IoBuffer buf)
 {
     _buf = buf;
 }
コード例 #40
0
ファイル: ByteBufferR.cs プロジェクト: xlg210/Mina.NET
 protected override void PutInternal(IoBuffer src)
 {
     throw new NotSupportedException();
 }
コード例 #41
0
ファイル: IoBufferWrapper.cs プロジェクト: eltonfan/Mina.NET
 /// <inheritdoc/>
 public override IoBuffer Put(IoBuffer src)
 {
     _buf.Put(src);
     return(this);
 }
コード例 #42
0
ファイル: IoBuffer.cs プロジェクト: eltonfan/Mina.NET
 /// <summary>
 /// Writes the content of the specified <paramref name="src"/> into this buffer.
 /// </summary>
 /// <returns>itself</returns>
 public abstract IoBuffer Put(IoBuffer src);
コード例 #43
0
        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);
        }
コード例 #44
0
        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));
        }
コード例 #45
0
        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);
        }
コード例 #46
0
        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());
            }
        }