?? bytesmessageimpl.java
字號:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: BytesMessageImpl.java,v 1.2 2005/03/18 03:50:12 tanderson Exp $
*/
package org.exolab.jms.message;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.UTFDataFormatException;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MessageEOFException;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotReadableException;
import javax.jms.MessageNotWriteableException;
/**
* This class implements the {@link BytesMessage} interface.
*
* @version $Revision: 1.2 $ $Date: 2005/03/18 03:50:12 $
* @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
* @author <a href="mailto:tima@netspace.net.au">Tim Anderson</a>
* @see BytesMessage
*/
public final class BytesMessageImpl extends MessageImpl
implements BytesMessage {
/**
* Object version no. for serialization.
*/
static final long serialVersionUID = 1;
/**
* Empty byte array for initialisation purposes.
*/
private static final byte[] EMPTY = new byte[]{};
/**
* The byte stream to store data.
*/
private byte[] _bytes = EMPTY;
/**
* The stream used for writes.
*/
private DataOutputStream _out = null;
/**
* The byte stream backing the output stream.
*/
private ByteArrayOutputStream _byteOut = null;
/**
* The stream used for reads.
*/
private DataInputStream _in = null;
/**
* The byte stream backing the input stream.
*/
private ByteArrayInputStream _byteIn = null;
/**
* The offset of the byte stream to start reading from. This defaults
* to 0, and only applies to messages that are cloned from a
* read-only instance where part of the stream had already been read.
*/
private int _offset = 0;
/**
* Construct a new BytesMessage. When first created, the message is in
* write-only mode.
*
* @throws JMSException if the message type can't be set
*/
public BytesMessageImpl() throws JMSException {
setJMSType("BytesMessage");
}
/**
* Clone an instance of this object.
*
* @return a copy of this object
* @throws CloneNotSupportedException if object or attributes aren't
* cloneable
*/
public final Object clone() throws CloneNotSupportedException {
BytesMessageImpl result = (BytesMessageImpl) super.clone();
if (_bodyReadOnly) {
result._bytes = new byte[_bytes.length];
System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length);
if (_byteIn != null) {
// if a client subsequently reads from the cloned object,
// start reading from offset of the original stream
_offset = _bytes.length - _byteIn.available();
}
result._byteIn = null;
result._in = null;
} else {
if (_out != null) {
try {
_out.flush();
} catch (IOException exception) {
throw new CloneNotSupportedException(
exception.getMessage());
}
result._bytes = _byteOut.toByteArray();
result._byteOut = null;
result._out = null;
} else {
result._bytes = new byte[_bytes.length];
System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length);
}
}
return result;
}
/**
* Serialize out this message's data.
*
* @param out the stream to serialize out to
* @throws IOException if any I/O exceptions occurr
*/
public final void writeExternal(ObjectOutput out) throws IOException {
// If it was in write mode, extract the byte array.
if (!_bodyReadOnly && _out != null) {
_out.flush();
_bytes = _byteOut.toByteArray();
}
super.writeExternal(out);
out.writeLong(serialVersionUID);
out.writeInt(_bytes.length);
out.write(_bytes);
out.flush();
}
/**
* Serialize in this message's data.
*
* @param in the stream to serialize in from
* @throws ClassNotFoundException if the class for an object being
* restored cannot be found.
* @throws IOException if any I/O exceptions occur
*/
public final void readExternal(ObjectInput in)
throws ClassNotFoundException, IOException {
super.readExternal(in);
long version = in.readLong();
if (version == serialVersionUID) {
int length = in.readInt();
_bytes = new byte[length];
in.readFully(_bytes);
} else {
throw new IOException("Incorrect version enountered: " + version +
". This version = " + serialVersionUID);
}
}
/**
* Gets the number of bytes of the message body when the message is in
* read-only mode. The value returned can be used to allocate a byte array.
* The value returned is the entire length of the message body, regardless
* of where the pointer for reading the message is currently located.
*
* @return number of bytes in the message
* @throws JMSException if the JMS provider fails to read the
* message due to some internal error.
* @throws MessageNotReadableException if the message is in write-only
* mode.
*/
public long getBodyLength() throws JMSException {
checkRead();
return _bytes.length;
}
/**
* Read a <code>boolean</code> from the bytes message stream.
*
* @return the <code>boolean</code> value read
* @throws JMSException if JMS fails to read message due to some internal
* JMS error
* @throws MessageEOFException if end of bytes stream
* @throws MessageNotReadableException if message is in write-only mode
*/
public final boolean readBoolean() throws JMSException {
boolean result = false;
prepare();
try {
result = _in.readBoolean();
} catch (IOException exception) {
revert(exception);
}
return result;
}
/**
* Read a signed 8-bit value from the bytes message stream.
*
* @return the next byte from the bytes message stream as a signed 8-bit
* <code>byte</code>
* @throws JMSException if JMS fails to read message due to some internal
* JMS error
* @throws MessageEOFException if end of message stream
* @throws MessageNotReadableException if message is in write-only mode
*/
public final byte readByte() throws JMSException {
byte result = 0;
prepare();
try {
result = _in.readByte();
} catch (IOException exception) {
revert(exception);
}
return result;
}
/**
* Read an unsigned 8-bit number from the bytes message stream.
*
* @return the next byte from the bytes message stream, interpreted as an
* unsigned 8-bit number
* @throws JMSException if JMS fails to read message due to some internal
* JMS error
* @throws MessageNotReadableException if message is in write-only mode
* @throws MessageEOFException if end of message stream
*/
public final int readUnsignedByte() throws JMSException {
int result = 0;
prepare();
try {
result = _in.readUnsignedByte();
} catch (IOException exception) {
revert(exception);
}
return result;
}
/**
* Read a signed 16-bit number from the bytes message stream.
*
* @return the next two bytes from the bytes message stream, interpreted
* as a signed 16-bit number
* @throws JMSException if JMS fails to read message due to some internal
* JMS error
* @throws MessageEOFException if end of message stream
* @throws MessageNotReadableException if message is in write-only mode
*/
public final short readShort() throws JMSException {
prepare();
try {
result = _in.readShort();
} catch (IOException exception) {
revert(exception);
}
return result;
}
/**
* Read an unsigned 16-bit number from the bytes message stream.
*
* @return the next two bytes from the bytes message stream, interpreted
* as an unsigned 16-bit integer
*
* @throws JMSException if JMS fails to read message due to some internal
* JMS error
* @throws MessageEOFException if end of message stream
* @throws MessageNotReadableException if message is in write-only mode
*/
public final int readUnsignedShort() throws JMSException {
int result = 0;
prepare();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -