[ 
https://issues.apache.org/jira/browse/QPIDJMS-325?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16173256#comment-16173256
 ] 

Rob Godfrey commented on QPIDJMS-325:
-------------------------------------

Reading the javadoc, I think I agree with Jiri's original reasoning:

{quote}
If the number of bytes remaining in the stream is less than the length of array 
value, the bytes should be read into the array. The return value of the total 
number of bytes read will be less than the length of the array, indicating that 
there are no more bytes left to be read from the stream.
{quote}

Followed by

{quote}
The *next* read of the stream returns -1.
{quote}

(emphasis mine).

 To me that means that -1 is only returned after a prior read had already been 
completed with that prior read returning a number *less* than the number of 
bytes available to read into the array.  (The implication of the latter is, I 
think, that {code}msg.readBytes(new byte[0]);{code} should *ALWAYS* return 0, 
unless a prior read has already returned -1).

As an aside on the validity of defining semantics from the javadoc... I note 
that the Oracle 
[javadoc|http://docs.oracle.com/javaee/7/api/javax/jms/BytesMessage.html#readBytes-byte:A-int-]
 for the {code}int readBytes(byte[] value, int length){code} form is entirely 
incorrect as it appears to have been copy and pasted from the single argument 
method.

> reading from empty buffer of a BytesMessage should return 0, not -1
> -------------------------------------------------------------------
>
>                 Key: QPIDJMS-325
>                 URL: https://issues.apache.org/jira/browse/QPIDJMS-325
>             Project: Qpid JMS
>          Issue Type: Bug
>          Components: qpid-jms-client
>    Affects Versions: 0.25.0
>            Reporter: Jiri Daněk
>            Priority: Trivial
>
> Consider the following test. According to 
> http://docs.oracle.com/javaee/7/api/javax/jms/BytesMessage.html#readBytes-byte:A-
>  the #readBytes method should return 0 when it is first called, as the number 
> of bytes in the buffer is 0 and it read 0 bytes. Only on subsequent calls it 
> should return -1. What happens now is that the method returns -1 the first 
> time.
> See the commented lines to try the same thing with ActiveMQ JMS Client 
> library, and with StreamMessage instead of BytesMessage. The behavior there 
> should be the same.
> ActiveMQ passes the test with BytesMessage and fails it with StreamMessage. 
> Qpid JMS fails with BytesMessage and passes with StreamMessage.
> {code}
> import org.apache.activemq.ActiveMQConnectionFactory;
> import org.apache.qpid.jms.JmsConnectionFactory;
> import org.junit.Test;
> import javax.jms.*;
> import static com.google.common.truth.Truth.assertThat;
> public class EmptyBufferInputTest {
>     @Test
>     public void testEmptyBufferInput() throws JMSException {
> //        ConnectionFactory connectionFactory = new 
> ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
>         JmsConnectionFactory connectionFactory = new 
> JmsConnectionFactory("amqp://127.0.0.1:5672");
>         Connection connection = connectionFactory.createConnection();
>         Session session = connection.createSession(false, 
> Session.AUTO_ACKNOWLEDGE);
>         final byte[] BYTE_LIST = {1, 2, 4};
> //        StreamMessage message = session.createStreamMessage();
>         BytesMessage message = session.createBytesMessage();
>         message.clearBody();
>         byte[] readList = new byte[BYTE_LIST.length - 1];
>         byte[] emptyList = {};
>         message.writeBytes(emptyList);
>         message.reset();
>         final int IS_EMPTY = 0;
>         assertThat(message.readBytes(readList)).isEqualTo(IS_EMPTY);
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org
For additional commands, e-mail: dev-h...@qpid.apache.org

Reply via email to