Search in sources :

Example 16 with MessageNotReadableException

use of javax.jms.MessageNotReadableException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method readInt.

/**
 * {@inheritDoc}
 */
@Override
public int readInt() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_INT > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    int i = Bits.getInt(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_INT;
    return i;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 17 with MessageNotReadableException

use of javax.jms.MessageNotReadableException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method readUTF.

/**
 * {@inheritDoc}
 */
@Override
public String readUTF() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    int posOfUtfItem = this.pos;
    // modifies pos if valid
    int lenUtfBytes = readUnsignedShort();
    // reset in case of failure
    this.pos = posOfUtfItem;
    int utfItemLen = Bits.NUM_BYTES_IN_SHORT + lenUtfBytes;
    if (posOfUtfItem + utfItemLen > this.buf.length) {
        throw new MessageFormatException("Not enough bytes in message body for UTF object");
    }
    byte[] utfBuf = new byte[utfItemLen];
    System.arraycopy(this.buf, posOfUtfItem, utfBuf, 0, utfItemLen);
    try {
        String str = new DataInputStream(new ByteArrayInputStream(utfBuf)).readUTF();
        this.pos += utfItemLen;
        return str;
    } catch (IOException ioe) {
        throw new RMQMessageFormatException("UTF String invalid format", ioe);
    }
}
Also used : RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) MessageFormatException(javax.jms.MessageFormatException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageNotReadableException(javax.jms.MessageNotReadableException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 18 with MessageNotReadableException

use of javax.jms.MessageNotReadableException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method readLong.

/**
 * {@inheritDoc}
 */
@Override
public long readLong() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_LONG > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    long l = Bits.getLong(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_LONG;
    return l;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 19 with MessageNotReadableException

use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.

the class MessageBodyTest method testBytesMessage.

@Test
public void testBytesMessage() throws Exception {
    BytesMessage m = queueProducerSession.createBytesMessage();
    // some arbitrary values
    boolean myBool = true;
    byte myByte = -111;
    short myShort = 15321;
    int myInt = 0x71ab6c80;
    long myLong = 0x20bf1e3fb6fa31dfL;
    float myFloat = Float.MAX_VALUE - 23465;
    double myDouble = Double.MAX_VALUE - 72387633;
    String myString = "abcdef&^*&!^ghijkl\uD5E2\uCAC7\uD2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
    log.trace("String is length:" + myString.length());
    char myChar = 'q';
    byte[] myBytes = new byte[] { -23, 114, -126, -12, 74, 87 };
    m.writeBoolean(myBool);
    m.writeByte(myByte);
    m.writeShort(myShort);
    m.writeChar(myChar);
    m.writeInt(myInt);
    m.writeLong(myLong);
    m.writeFloat(myFloat);
    m.writeDouble(myDouble);
    m.writeUTF(myString);
    m.writeBytes(myBytes);
    m.writeBytes(myBytes, 2, 3);
    m.writeObject(new Boolean(myBool));
    m.writeObject(new Byte(myByte));
    m.writeObject(new Short(myShort));
    m.writeObject(new Integer(myInt));
    m.writeObject(new Long(myLong));
    m.writeObject(new Float(myFloat));
    m.writeObject(new Double(myDouble));
    m.writeObject(myString);
    m.writeObject(myBytes);
    try {
        m.writeObject(new Object());
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    // OK
    }
    // Reading should not be possible when message is read-write
    try {
        m.readBoolean();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readShort();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readChar();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readInt();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readLong();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readFloat();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readDouble();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readUTF();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readUnsignedByte();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.readUnsignedShort();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        byte[] bytes = new byte[333];
        m.readBytes(bytes);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        byte[] bytes = new byte[333];
        m.readBytes(bytes, 111);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    try {
        m.getBodyLength();
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotReadableException e) {
    // OK
    }
    queueProducer.send(m);
    BytesMessage m2 = (BytesMessage) queueConsumer.receive(2000);
    ProxyAssertSupport.assertNotNull(m2);
    ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
    ProxyAssertSupport.assertEquals(myByte, m2.readByte());
    ProxyAssertSupport.assertEquals(myShort, m2.readShort());
    ProxyAssertSupport.assertEquals(myChar, m2.readChar());
    ProxyAssertSupport.assertEquals(myInt, m2.readInt());
    ProxyAssertSupport.assertEquals(myLong, m2.readLong());
    ProxyAssertSupport.assertEquals(myFloat, m2.readFloat(), 0);
    ProxyAssertSupport.assertEquals(myDouble, m2.readDouble(), 0);
    ProxyAssertSupport.assertEquals(myString, m2.readUTF());
    byte[] bytes = new byte[6];
    int ret = m2.readBytes(bytes);
    ProxyAssertSupport.assertEquals(6, ret);
    Assert.assertArrayEquals(myBytes, bytes);
    byte[] bytes2 = new byte[3];
    ret = m2.readBytes(bytes2);
    ProxyAssertSupport.assertEquals(3, ret);
    ProxyAssertSupport.assertEquals(myBytes[2], bytes2[0]);
    ProxyAssertSupport.assertEquals(myBytes[3], bytes2[1]);
    ProxyAssertSupport.assertEquals(myBytes[4], bytes2[2]);
    ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
    ProxyAssertSupport.assertEquals(myByte, m2.readByte());
    ProxyAssertSupport.assertEquals(myShort, m2.readShort());
    ProxyAssertSupport.assertEquals(myInt, m2.readInt());
    ProxyAssertSupport.assertEquals(myLong, m2.readLong());
    ProxyAssertSupport.assertEquals(myFloat, m2.readFloat(), 0);
    ProxyAssertSupport.assertEquals(myDouble, m2.readDouble(), 0);
    ProxyAssertSupport.assertEquals(myString, m2.readUTF());
    bytes = new byte[6];
    ret = m2.readBytes(bytes);
    ProxyAssertSupport.assertEquals(6, ret);
    Assert.assertArrayEquals(myBytes, bytes);
    ret = m2.readBytes(bytes);
    ProxyAssertSupport.assertEquals(-1, ret);
    // Try and read past the end of the stream
    try {
        m2.readBoolean();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readByte();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readChar();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readDouble();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readFloat();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readInt();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readLong();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readShort();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readUnsignedByte();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readUnsignedShort();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    try {
        m2.readUTF();
        ProxyAssertSupport.fail();
    } catch (MessageEOFException e) {
    // OK
    }
    // Message should not be writable in read-only mode
    try {
        m2.writeBoolean(myBool);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeByte(myByte);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeShort(myShort);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeChar(myChar);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeInt(myInt);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeLong(myLong);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeFloat(myFloat);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeDouble(myDouble);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeUTF(myString);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeBytes(myBytes);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    try {
        m2.writeObject(myString);
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageNotWriteableException e) {
    // OK
    }
    long bodyLength = m2.getBodyLength();
    ProxyAssertSupport.assertEquals(161, bodyLength);
    m2.reset();
    // test the unsigned reads
    m2.readBoolean();
    int unsignedByte = m2.readUnsignedByte();
    ProxyAssertSupport.assertEquals(myByte & 0xFF, unsignedByte);
    int unsignedShort = m2.readUnsignedShort();
    ProxyAssertSupport.assertEquals(myShort & 0xFFFF, unsignedShort);
    m2.clearBody();
    try {
        m2.getBodyLength();
        ProxyAssertSupport.fail();
    } catch (MessageNotReadableException e) {
    // OK
    }
    m2.reset();
    ProxyAssertSupport.assertEquals(0, m2.getBodyLength());
    // Test that changing the received message doesn't affect the sent message
    m.reset();
    ProxyAssertSupport.assertEquals(161, m.getBodyLength());
    // Should be diffent object instances after sending *even* if in same JVM
    ProxyAssertSupport.assertFalse(m == m2);
}
Also used : MessageEOFException(javax.jms.MessageEOFException) BytesMessage(javax.jms.BytesMessage) MessageNotWriteableException(javax.jms.MessageNotWriteableException) MessageNotReadableException(javax.jms.MessageNotReadableException) MessageFormatException(javax.jms.MessageFormatException) MessageNotReadableException(javax.jms.MessageNotReadableException) Test(org.junit.Test)

Example 20 with MessageNotReadableException

use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.

the class SimpleJMSStreamMessage method readByte.

@Override
public byte readByte() throws JMSException {
    if (bodyWriteOnly) {
        throw new MessageNotReadableException("The message body is writeonly");
    }
    try {
        Object value = content.get(position);
        offset = 0;
        if (value == null) {
            throw new NullPointerException("Value is null");
        } else if (value instanceof Byte) {
            position++;
            return ((Byte) value).byteValue();
        } else if (value instanceof String) {
            byte result = Byte.parseByte((String) value);
            position++;
            return result;
        } else {
            throw new MessageFormatException("Invalid conversion");
        }
    } catch (IndexOutOfBoundsException e) {
        throw new MessageEOFException("");
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Aggregations

MessageNotReadableException (javax.jms.MessageNotReadableException)24 MessageEOFException (javax.jms.MessageEOFException)21 MessageFormatException (javax.jms.MessageFormatException)14 MessageNotWriteableException (javax.jms.MessageNotWriteableException)3 RMQMessageFormatException (com.rabbitmq.jms.util.RMQMessageFormatException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 EOFException (java.io.EOFException)1 UTFDataFormatException (java.io.UTFDataFormatException)1 BytesMessage (javax.jms.BytesMessage)1 JMSException (javax.jms.JMSException)1 StreamMessage (javax.jms.StreamMessage)1