Apache.NMS.ActiveMQ.Commands.ActiveMQStreamMessage.ReadBytes C# (CSharp) Method

ReadBytes() public method

public ReadBytes ( byte value ) : int
value byte
return int
        public int ReadBytes(byte[] value)
        {
            InitializeReading();

            if(value == null)
            {
                throw new NullReferenceException("Passed Byte Array is null");
            }

            try
            {
                if(this.bytesRemaining == -1)
                {
                    long startingPos = this.byteBuffer.Position;
                    byte type = this.dataIn.ReadByte();

                    if(type != PrimitiveMap.BYTE_ARRAY_TYPE)
                    {
                        this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
                        throw new MessageFormatException("Not a byte array");
                    }

                    this.bytesRemaining = this.dataIn.ReadInt32();
                }
                else if(this.bytesRemaining == 0)
                {
                    this.bytesRemaining = -1;
                    return -1;
                }

                if(value.Length <= this.bytesRemaining)
                {
                    // small buffer
                    this.bytesRemaining -= value.Length;
                    this.dataIn.Read(value, 0, value.Length);
                    return value.Length;
                }
                else
                {
                    // big buffer
                    int rc = this.dataIn.Read(value, 0, this.bytesRemaining);
                    this.bytesRemaining = 0;
                    return rc;
                }
            }
            catch(EndOfStreamException ex)
            {
                throw NMSExceptionSupport.CreateMessageEOFException(ex);
            }
            catch(IOException ex)
            {
                throw NMSExceptionSupport.CreateMessageFormatException(ex);
            }
        }

Usage Example

 public void TestReadOnlyBody()
 {
     ActiveMQStreamMessage message = new ActiveMQStreamMessage();
     try
     {
         message.WriteBoolean(true);
         message.WriteByte((byte)1);
         message.WriteBytes(new byte[1]);
         message.WriteBytes(new byte[3], 0, 2);
         message.WriteChar('a');
         message.WriteDouble(1.5);
         message.WriteSingle((float)1.5);
         message.WriteInt32(1);
         message.WriteInt64(1);
         message.WriteObject("stringobj");
         message.WriteInt16((short)1);
         message.WriteString("string");
     }
     catch(MessageNotWriteableException)
     {
         Assert.Fail("Should be writeable");
     }
     message.Reset();
     try
     {
         message.ReadBoolean();
         message.ReadByte();
         Assert.AreEqual(1, message.ReadBytes(new byte[10]));
         Assert.AreEqual(-1, message.ReadBytes(new byte[10]));
         Assert.AreEqual(2, message.ReadBytes(new byte[10]));
         Assert.AreEqual(-1, message.ReadBytes(new byte[10]));
         message.ReadChar();
         message.ReadDouble();
         message.ReadSingle();
         message.ReadInt32();
         message.ReadInt64();
         message.ReadString();
         message.ReadInt16();
         message.ReadString();
     }
     catch(MessageNotReadableException)
     {
         Assert.Fail("Should be readable");
     }
     try
     {
         message.WriteBoolean(true);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteByte((byte)1);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteBytes(new byte[1]);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteBytes(new byte[3], 0, 2);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try {
         message.WriteChar('a');
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteDouble(1.5);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteSingle((float)1.5);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteInt32(1);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteInt64(1);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteObject("stringobj");
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteSingle((short)1);
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
     try
     {
         message.WriteString("string");
         Assert.Fail("Should have thrown exception");
     }
     catch(MessageNotWriteableException)
     {
     }
 }
All Usage Examples Of Apache.NMS.ActiveMQ.Commands.ActiveMQStreamMessage::ReadBytes