bytesmessageimpl.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 968 行 · 第 1/3 页

JAVA
968
字号
        try {            getOutputStream().writeLong(value);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Convert the float argument to an <code>int</code> using the     * <code>floatToIntBits</code> method in class <code>Float</code>,     * and then writes that <code>int</code> value to the bytes message     * stream as a 4-byte quantity, high byte first.     *     * @param value the <code>float</code> value to be written.     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     * @throws MessageNotWriteableException if message is in read-only mode     */    public final void writeFloat(float value) throws JMSException {        checkWrite();        try {            getOutputStream().writeFloat(value);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Convert the double argument to a <code>long</code> using the     * <code>doubleToLongBits</code> method in class <code>Double</code>,     * and then writes that <code>long</code> value to the bytes message     * stream as an 8-byte quantity, high byte first.     *     * @param value the <code>double</code> value to be written.     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     * @throws MessageNotWriteableException if message is in read-only mode     */    public final void writeDouble(double value) throws JMSException {        checkWrite();        try {            getOutputStream().writeDouble(value);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Write a string to the bytes message stream using UTF-8 encoding in a     * machine-independent manner.     *     * <p>For more information on the UTF-8 format, see "File System Safe     * UCS Transformation Format (FSS_UFT)", X/Open Preliminary Specification,     * X/Open Company Ltd., Document Number: P316. This information also     * appears in ISO/IEC 10646, Annex P.     *     * @param value the <code>String</code> value to be written     * @throws MessageNotWriteableException if message is in read-only mode     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     */    public final void writeUTF(String value) throws JMSException {        checkWrite();        try {            getOutputStream().writeUTF(value);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Write a byte array to the bytes message stream.     *     * @param value the byte array to be written.     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     * @throws MessageNotWriteableException if message is in read-only mode     */    public final void writeBytes(byte[] value) throws JMSException {        checkWrite();        try {            getOutputStream().write(value);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Write a portion of a byte array to the bytes message stream     *     * @param value the byte array value to be written.     * @param offset the initial offset within the byte array.     * @param length the number of bytes to use.     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     * @throws MessageNotWriteableException if message is in read-only mode     */    public final void writeBytes(byte[] value, int offset, int length)        throws JMSException {        checkWrite();        try {            getOutputStream().write(value, offset, length);        } catch (IOException exception) {            raise(exception);        }    }    /**     * Write a Java object to the bytes message stream.     *     * <p>Note that this method only works for the objectified primitive     * object types (Integer, Double, Long ...), String's and byte arrays.     *     * @param value the Java object to be written. Must not be null.     *     * @throws JMSException if JMS fails to write message due to some internal     * JMS error     * @throws MessageFormatException if object is invalid type     * @throws MessageNotWriteableException if message in read-only mode     * @throws NullPointerException if parameter <code>value</code> is null     */    public final void writeObject(Object value) throws JMSException {        if (value instanceof Boolean) {            writeBoolean(((Boolean) value).booleanValue());        } else if (value instanceof Byte) {            writeByte(((Byte) value).byteValue());        } else if (value instanceof Short) {            writeShort(((Short) value).shortValue());        } else if (value instanceof Character) {            writeChar(((Character) value).charValue());        } else if (value instanceof Integer) {            writeInt(((Integer) value).intValue());        } else if (value instanceof Long) {            writeLong(((Long) value).longValue());        } else if (value instanceof Float) {            writeFloat(((Float) value).floatValue());        } else if (value instanceof Double) {            writeDouble(((Double) value).doubleValue());        } else if (value instanceof String) {            writeUTF((String) value);        } else if (value instanceof byte[]) {            writeBytes((byte[]) value);        } else if (value == null) {            throw new NullPointerException(                "BytesMessage does not support null");        } else {            throw new MessageFormatException("Cannot write objects of type=" +                value.getClass().getName());        }    }    /**     * Put the message body in read-only mode, and reposition the stream of     * bytes to the beginning.     *     * @throws JMSException if JMS fails to reset the message due to some     * internal JMS error     */    public final void reset() throws JMSException {        try {            if (!_bodyReadOnly) {                _bodyReadOnly = true;                if (_out != null) {                    _out.flush();                    _bytes = _byteOut.toByteArray();                    _byteOut = null;                    _out.close();                    _out = null;                }            } else {                if (_in != null) {                    _byteIn = null;                    _in.close();                    _in = null;                }            }        } catch (IOException exception) {            raise(exception);        }    }    /**     * Overide the super class method to reset the streams, and put the     * message body in write only mode.     *     * <p>If <code>clearBody</code> is called on a message in read-only mode,     * the message body is cleared and the message is in write-only mode.     * bytes to the beginning.     *     *	<p>If <code>clearBody</code> is called on a message already in     * 	write-only mode, the spec does not define the outcome, so do nothing.     * 	Client must then call <code>reset</code>, followed by     *	<code>clearBody</code> to reset the stream at the beginning for a     *	new write.     * @throws JMSException if JMS fails to reset the message due to some     * internal JMS error     */    public final void clearBody() throws JMSException {        try {            if (_bodyReadOnly) {                // in read-only mode                _bodyReadOnly = false;                if (_in != null) {                    _byteIn = null;                    _in.close();                    _in = null;                    _offset = 0;                }            } else if (_out != null) {                // already in write-only mode                _byteOut = null;                _out.close();                _out = null;            }            _bytes = EMPTY;            _byteOut = null;            _out = null;        } catch (IOException exception) {            raise(exception);        }    }    /**     * Set the read-only mode of the message.     *     * @param readOnly if true, make the message body and properties read-only,     * and invoke {@link #reset}     * @throws JMSException if the read-only mode cannot be changed     */    public final void setReadOnly(boolean readOnly) throws JMSException {        if (readOnly) {            reset();        }        super.setReadOnly(readOnly);    }    /**     * Prepare to do a read.     *     * @throws JMSException if the current position in the stream can't be     * marked     * @throws MessageNotReadableException if the message is in write-only mode     */    private final void prepare() throws JMSException {        checkRead();        getInputStream();        try {            _in.mark(_bytes.length - _in.available());        } catch (IOException exception) {            raise(exception);        }    }    /**     * Reverts the stream to its prior position if an I/O exception is     * thrown, and propagates the exception as a JMSException.     *     * @param exception the exception that caused the reset     * @throws JMSException for general IOException errors     * @throws MessageEOFException if end-of-file was reached     */    private final void revert(IOException exception) throws JMSException {        try {            _in.reset();        } catch (IOException ignore) {        }        JMSException error = null;        if (exception instanceof EOFException) {            error = new MessageEOFException(exception.getMessage());        } else if (exception instanceof UTFDataFormatException) {            error = new MessageFormatException(exception.getMessage());        } else {            error = new JMSException(exception.getMessage());        }        error.setLinkedException(exception);        throw error;    }    /**     * Initialise the input stream if it hasn't been intialised.     *     * @return the input stream     */    private DataInputStream getInputStream() {        if (_in == null) {            _byteIn = new ByteArrayInputStream(_bytes, _offset,                _bytes.length - _offset);            _in = new DataInputStream(_byteIn);        }        return _in;    }    /**     * Initialise the output stream if it hasn't been intialised.     *     * @return the output stream     * @throws IOException if the output stream can't be created     */    private final DataOutputStream getOutputStream() throws IOException {        if (_out == null) {            _byteOut = new ByteArrayOutputStream();            _out = new DataOutputStream(_byteOut);            _out.write(_bytes);        }        return _out;    }    /**     * Helper to raise a JMSException when an I/O error occurs.     *     * @param exception the exception that caused the failure     * @throws JMSException     */    private final void raise(IOException exception) throws JMSException {        JMSException error = new JMSException(exception.getMessage());        error.setLinkedException(exception);        throw error;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?