nevado-jms/src/main/java/org/skyscreamer/nevado/jms/message/NevadoStreamMessage.java
/**
     * Reads a byte array field from the stream message into the specified
     * <CODE>byte[]</CODE> object (the read buffer). <p/>
     * <P>
     * To read the field value, <CODE>readBytes</CODE> should be successively
     * called until it returns a value less than the length of the read buffer.
     * The value of the bytes in the buffer following the last byte read is
     * undefined. <p/>
     * <P>
     * If <CODE>readBytes</CODE> returns a value equal to the length of the
     * buffer, a subsequent <CODE>readBytes</CODE> call must be made. If there
     * are no more bytes to be read, this call returns -1. <p/>
     * <P>
     * If the byte array field value is null, <CODE>readBytes</CODE> returns
     * -1. <p/>
     * <P>
     * If the byte array field value is empty, <CODE>readBytes</CODE> returns
     * 0. <p/>
     * <P>
     * Once the first <CODE>readBytes</CODE> call on a <CODE>byte[]</CODE>
     * field value has been made, the full value of the field must be read
     * before it is valid to read the next field. An attempt to read the next
     * field before that has been done will throw a <CODE>MessageFormatException</CODE>.
     * <p/>
     * <P>
     * To read the byte field value into a new <CODE>byte[]</CODE> object, use
     * the <CODE>readObject</CODE> method.
     *
     * @param value
     *            the buffer into which the data is read
     * @return the total number of bytes read into the buffer, or -1 if there is
     *         no more data because the end of the byte field has been reached
     * @throws JMSException
     *             if the JMS provider fails to read the message due to some
     *             internal error.
     * @throws MessageEOFException
     *             if unexpected end of message stream has been reached.
     * @throws MessageFormatException
     *             if this type conversion is invalid.
     * @throws MessageNotReadableException
     *             if the message is in write-only mode.
     * @see #readObject()
     */
Reads a byte array field from the stream message into the specified
byte[] object (the read buffer). 

To read the field value, readBytes should be successively
called until it returns a value less than the length of the read buffer.
The value of the bytes in the buffer following the last byte read is
undefined. 

If readBytes returns a value equal to the length of the
buffer, a subsequent readBytes call must be made. If there
are no more bytes to be read, this call returns -1. 

If the byte array field value is null, readBytes returns
-1. 

If the byte array field value is empty, readBytes returns
0. 

Once the first readBytes call on a byte[]
field value has been made, the full value of the field must be read
before it is valid to read the next field. An attempt to read the next
field before that has been done will throw a MessageFormatException.


To read the byte field value into a new byte[] object, use
the readObject method.

@param value
the buffer into which the data is read
@return the total number of bytes read into the buffer, or -1 if there is
no more data because the end of the byte field has been reached
@throws JMSException
if the JMS provider fails to read the message due to some
internal error.
@throws MessageEOFException
if unexpected end of message stream has been reached.
@throws MessageFormatException
if this type conversion is invalid.
@throws MessageNotReadableException
if the message is in write-only mode.
@see #readObject()
Reads a byte array field from the stream message into the specified byte [] object ( the read buffer ) . To read the field value readBytes should be successively called until it returns a value less than the length of the read buffer . The value of the bytes in the buffer following the last byte read is undefined . If readBytes returns a value equal to the length of the buffer a subsequent readBytes call must be made . If there are no more bytes to be read this call returns - 1 . If the byte array field value is null readBytes returns - 1 . If the byte array field value is empty readBytes returns 0 . Once the first readBytes call on a byte [] field value has been made the full value of the field must be read before it is valid to read the next field . An attempt to read the next field before that has been done will throw a MessageFormatException . To read the byte field value into a new byte [] object use the readObject method . @param value the buffer into which the data is read @return the total number of bytes read into the buffer or - 1 if there is no more data because the end of the byte field has been reached @throws JMSException if the JMS provider fails to read the message due to some internal error . @throws MessageEOFException if unexpected end of message stream has been reached . @throws MessageFormatException if this type conversion is invalid . @throws MessageNotReadableException if the message is in write - only mode . @see #readObject ()
public int readBytes(byte[] value) throws JMSException {
        initializeReading();
        try {
            if (value == null) {
                throw new NullPointerException();
            }
            if( remainingBytes == -1 ) {
                this.dataIn.mark(value.length + 1);
                int type = this.dataIn.read();
                if (type == -1) {
                    throw new MessageEOFException("reached end of data");
                }
                if (type != MarshallingSupport.BYTE_ARRAY_TYPE) {
                    throw new MessageFormatException("Not a byte array");
                }
                remainingBytes = this.dataIn.readInt();
            } else if ( remainingBytes == 0 ) {
                remainingBytes = -1;
                return -1;
            }
            if (value.length <= remainingBytes) {
                remainingBytes -= value.length;
                this.dataIn.readFully(value);
                return value.length;
            } else {
                int rc = this.dataIn.read(value, 0, remainingBytes);
                remainingBytes=0;
                return rc;
            }
        } catch (EOFException e) {
            JMSException jmsEx = new MessageEOFException(e.getMessage());
            jmsEx.setLinkedException(e);
            throw jmsEx;
        } catch (IOException e) {
            JMSException jmsEx = new MessageFormatException(e.getMessage());
            jmsEx.setLinkedException(e);
            throw jmsEx;
        }
    }
public int readBytes ( byte [ ] value ) throws JMSException { initializeReading ( ) ; try { if ( value == null ) { throw new NullPointerException ( ) ; } if ( remainingBytes == - 1 ) { this . dataIn . mark ( value . length + 1 ) ; int type = this . dataIn . read ( ) ; if ( type == - 1 ) { throw new MessageEOFException ( "reached end of data" ) ; } if ( type != MarshallingSupport . BYTE_ARRAY_TYPE ) { throw new MessageFormatException ( "Not a byte array" ) ; } remainingBytes = this . dataIn . readInt ( ) ; } else if ( remainingBytes == 0 ) { remainingBytes = - 1 ; return - 1 ; } if ( value . length <= remainingBytes ) { remainingBytes -= value . length ; this . dataIn . readFully ( value ) ; return value . length ; } else { int rc = this . dataIn . read ( value , 0 , remainingBytes ) ; remainingBytes = 0 ; return rc ; } } catch ( EOFException e ) { JMSException jmsEx = new MessageEOFException ( e . getMessage ( ) ) ; jmsEx . setLinkedException ( e ) ; throw jmsEx ; } catch ( IOException e ) { JMSException jmsEx = new MessageFormatException ( e . getMessage ( ) ) ; jmsEx . setLinkedException ( e ) ; throw jmsEx ; } }
Reads a byte array field from the stream message into the specified
 <CODE>byte[]</CODE> object (the read buffer).
Reads a byte array field from the stream message into the specified <CODE > byte [] < / CODE > object ( the read buffer ) .
// small buffer // big buffer