Search in sources :

Example 1 with MessageNotReadableException

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

the class RMQBytesMessage method readBytes.

/**
 * {@inheritDoc}
 */
@Override
public int readBytes(byte[] value, int length) throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (length < 0 || length > value.length) {
        throw new IndexOutOfBoundsException();
    }
    if (this.pos < this.buf.length) {
        int readLen = Math.min(length, this.buf.length - this.pos);
        System.arraycopy(this.buf, this.pos, value, 0, readLen);
        this.pos += readLen;
        return readLen;
    }
    // means EOF already
    return -1;
}
Also used : MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 2 with MessageNotReadableException

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

the class RMQBytesMessage method readDouble.

/**
 * {@inheritDoc}
 */
@Override
public double readDouble() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_DOUBLE > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    double dbl = Bits.getDouble(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_DOUBLE;
    return dbl;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 3 with MessageNotReadableException

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

the class RMQBytesMessage method readChar.

/**
 * {@inheritDoc}
 */
@Override
public char readChar() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_CHAR > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    char ch = Bits.getChar(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_CHAR;
    return ch;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 4 with MessageNotReadableException

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

the class RMQBytesMessage method readFloat.

/**
 * {@inheritDoc}
 */
@Override
public float readFloat() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_FLOAT > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    float flt = Bits.getFloat(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_FLOAT;
    return flt;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 5 with MessageNotReadableException

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

the class RMQStreamMessage method readPrimitiveType.

private Object readPrimitiveType(Class<?> type) throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.readbuf != null) {
        throw new MessageFormatException("You must call 'int readBytes(byte[])' since the buffer is not empty");
    }
    boolean success = true;
    try {
        this.bin.mark(0);
        Object o = RMQMessage.readPrimitive(in);
        if (o instanceof byte[]) {
            if (type == ByteArray.class || type == Object.class) {
                return o;
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
            }
        } else if (type == ByteArray.class) {
            if (o == null) {
                return null;
            }
            throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
        } else if (type == Boolean.class) {
            if (o == null) {
                return Boolean.FALSE;
            } else if (o instanceof Boolean) {
                return o;
            } else if (o instanceof String) {
                return Boolean.parseBoolean((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "boolean"));
            }
        } else if (type == Byte.class) {
            if (o instanceof Byte) {
                return o;
            } else if (o instanceof String) {
                return Byte.parseByte((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
            }
        } else if (type == Short.class) {
            if (o instanceof Byte) {
                return (short) (Byte) o;
            } else if (o instanceof Short) {
                return o;
            } else if (o instanceof String) {
                return Short.parseShort((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
            }
        } else if (type == Integer.class) {
            if (o instanceof Byte) {
                return (int) (Byte) o;
            } else if (o instanceof Short) {
                return (int) (Short) o;
            } else if (o instanceof Integer) {
                return o;
            } else if (o instanceof String) {
                return Integer.parseInt((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "int"));
            }
        } else if (type == Character.class) {
            if (o instanceof Character) {
                return o;
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "char"));
            }
        } else if (type == Long.class) {
            if (o instanceof Byte) {
                return (long) (Byte) o;
            } else if (o instanceof Short) {
                return (long) (Short) o;
            } else if (o instanceof Integer) {
                return (long) (Integer) o;
            } else if (o instanceof Long) {
                return o;
            } else if (o instanceof String) {
                return Long.parseLong((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "long"));
            }
        } else if (type == Float.class) {
            if (o instanceof Float) {
                return (Float) o;
            } else if (o instanceof String) {
                return Float.parseFloat((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "float"));
            }
        } else if (type == Double.class) {
            if (o instanceof Float) {
                return (double) (Float) o;
            } else if (o instanceof Double) {
                return (Double) o;
            } else if (o instanceof String) {
                return Double.parseDouble((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "double"));
            }
        } else if (type == String.class) {
            if (o == null) {
                return null;
            } else if (o instanceof byte[]) {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "String"));
            } else {
                return o.toString();
            }
        } else if (type == Object.class) {
            return o;
        } else {
            throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, type.toString()));
        }
    } catch (NumberFormatException x) {
        success = false;
        throw x;
    } catch (ClassNotFoundException x) {
        success = false;
        throw new RMQJMSException(x);
    } catch (EOFException x) {
        success = false;
        throw new MessageEOFException(MSG_EOF);
    } catch (UTFDataFormatException x) {
        success = false;
        throw new RMQMessageFormatException(x);
    } catch (IOException x) {
        success = false;
        throw new RMQJMSException(x);
    } catch (Exception x) {
        success = false;
        if (x instanceof JMSException) {
            throw (JMSException) x;
        } else {
            throw new RMQJMSException(x);
        }
    } finally {
        if (!success) {
            this.bin.reset();
        }
    }
}
Also used : MessageEOFException(javax.jms.MessageEOFException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) MessageEOFException(javax.jms.MessageEOFException) EOFException(java.io.EOFException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) MessageFormatException(javax.jms.MessageFormatException) MessageNotReadableException(javax.jms.MessageNotReadableException) IOException(java.io.IOException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) IOException(java.io.IOException) MessageEOFException(javax.jms.MessageEOFException) EOFException(java.io.EOFException) JMSException(javax.jms.JMSException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) MessageFormatException(javax.jms.MessageFormatException) MessageNotReadableException(javax.jms.MessageNotReadableException) UTFDataFormatException(java.io.UTFDataFormatException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) UTFDataFormatException(java.io.UTFDataFormatException)

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