qpid-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jiri Daněk (JIRA) <j...@apache.org>
Subject [jira] [Commented] (QPIDJMS-325) reading from empty buffer of a BytesMessage should return 0, not -1
Date Thu, 21 Sep 2017 11:06:00 GMT

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

Jiri Daněk commented on QPIDJMS-325:
------------------------------------

I've done brief search on GitHub and from the code there, having looked at about 8 pages of
results, it appears usual usage pattern is

{code}
byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(bytes);
{code}

and in one case I saw

{code}
// https://github.com/jianbinjiang/activemq/blob/0209d1d2b149988f933b7b50ae61322c7c84dc28/src/main/java/com/hk/demo/activemq/consumer/ConsumerService.java
while ((len = bm.readBytes(b)) != -1) {
    System.out.println(new String(b, 0, len));
}
{code}

The return value would make a difference in the second case. When receiving empty array, it
would either print empty line, or nothing, depending on return value.

If I could choose, I'd make the return value to be 0 on EOF for the first read and all subsequent
ones for BytesMessage... Sadly, that is something the Javadoc does not allow, and I don't
have preference between the ActiveMQ behavior and (qpid-jms + ActiveMQ Artemis) behavior.

https://github.com/search?utf8=%E2%9C%93&q=BytesMessage+readBytes+language%3AJava+path%3A%2Fsrc%2Fmain%2Fjava&type=Code&ref=advsearch&l=Java&l=

I realize GitHub is not good corpus for this, because most of relevant JMS client code is
probably not there.

> 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-unsubscribe@qpid.apache.org
For additional commands, e-mail: dev-help@qpid.apache.org


Mime
View raw message