package org.mockejb.jms;
import javax.jms.*;
import java.io.*;
public class BytesMessageImpl extends MessageImpl implements BytesMessage {
private final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
private ByteArrayInputStream bytesIn;
private DataInputStream dataIn;
private DataOutputStream dataOut;
public BytesMessageImpl() throws JMSException {
clearBody();
}
public BytesMessageImpl(BytesMessage msg) throws JMSException {
super(msg);
clearBody();
setBody(msg);
}
private void setBody(BytesMessage msg) throws JMSException {
int bytesRemaining = 0;
byte[] sourceBytes = null;
while (true) {
try {
msg.readByte();
bytesRemaining++;
} catch (MessageEOFException ex) {
sourceBytes = extractBytes(msg);
msg.reset();
msg.readBytes(new byte[sourceBytes.length - bytesRemaining]);
break;
} catch (MessageNotReadableException ex) {
sourceBytes = extractBytes(msg);
msg.clearBody();
msg.writeBytes(sourceBytes);
break;
}
}
writeBytes(sourceBytes);
}
private byte[] extractBytes(BytesMessage msg) throws JMSException {
msg.reset();
int numberOfBytes = 0;
while (true) {
try {
msg.readByte();
numberOfBytes++;
} catch (MessageEOFException ex) {
break;
}
}
byte[] result = new byte[numberOfBytes];
msg.reset();
msg.readBytes(result);
return result;
}
public void reset() {
setBodyReadOnly();
if (dataOut != null) {
dataOut = null;
}
bytesIn = new ByteArrayInputStream(bytesOut.toByteArray());
dataIn = new DataInputStream(bytesIn);
}
public void clearBody() throws JMSException {
super.clearBody();
if (dataIn != null) {
dataIn = null;
bytesIn = null;
}
bytesOut.reset();
dataOut = new DataOutputStream(bytesOut);
}
public long getBodyLength() throws JMSException {
checkBodyReadable();
return bytesOut.size();
}
public boolean readBoolean() throws JMSException {
checkBodyReadable();
try {
return dataIn.readBoolean();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public byte readByte() throws JMSException {
checkBodyReadable();
try {
return dataIn.readByte();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public int readUnsignedByte() throws JMSException {
checkBodyReadable();
try {
return dataIn.readUnsignedByte();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public short readShort() throws JMSException {
checkBodyReadable();
try {
return dataIn.readShort();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public int readUnsignedShort() throws JMSException {
checkBodyReadable();
try {
return dataIn.readUnsignedShort();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public char readChar() throws JMSException {
checkBodyReadable();
try {
return dataIn.readChar();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public int readInt() throws JMSException {
checkBodyReadable();
try {
return dataIn.readInt();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public long readLong() throws JMSException {
checkBodyReadable();
try {
return dataIn.readLong();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public float readFloat() throws JMSException {
checkBodyReadable();
try {
return dataIn.readFloat();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public double readDouble() throws JMSException {
checkBodyReadable();
try {
return dataIn.readDouble();
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public String readUTF() throws JMSException {
checkBodyReadable();
String result;
try {
try {
dataIn.mark(dataIn.available());
result = dataIn.readUTF();
dataIn.mark(0);
} catch (UTFDataFormatException ex) {
throw new MessageFormatException(ex.getMessage());
} catch (EOFException ex) {
throw new MessageEOFException(ex.getMessage());
} finally {
dataIn.reset();
dataIn.mark(0);
}
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
return result;
}
public int readBytes(byte[] bytes) throws JMSException {
checkBodyReadable();
try {
return dataIn.read(bytes);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public int readBytes(byte[] bytes, int length) throws JMSException {
checkBodyReadable();
try {
return dataIn.read(bytes, 0, length);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeBoolean(boolean value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeBoolean(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeByte(byte value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeByte(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeShort(short value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeShort(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeChar(char value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeChar(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeInt(int value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeInt(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeLong(long value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeLong(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeFloat(float value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeFloat(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeDouble(double value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeDouble(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeUTF(String value) throws JMSException {
checkBodyWriteable();
try {
dataOut.writeUTF(value);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeBytes(byte[] bytes) throws JMSException {
checkBodyWriteable();
try {
dataOut.write(bytes);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeBytes(byte[] bytes, int offset, int length)
throws JMSException {
checkBodyWriteable();
try {
dataOut.write(bytes, offset, length);
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public void writeObject(Object value) throws JMSException {
checkBodyWriteable();
if (value == null) {
throw new NullPointerException();
}
if (value instanceof Boolean) {
writeBoolean(((Boolean) value).booleanValue());
}
if (value instanceof Byte) {
writeByte(((Byte) value).byteValue());
}
if (value instanceof Short) {
writeShort(((Short) value).shortValue());
}
if (value instanceof Integer) {
writeInt(((Integer) value).intValue());
}
if (value instanceof Long) {
writeLong(((Long) value).longValue());
}
if (value instanceof Float) {
writeFloat(((Float) value).floatValue());
}
if (value instanceof Double) {
writeDouble(((Double) value).doubleValue());
}
if (value instanceof String) {
writeUTF((String) value);
}
if (value instanceof byte[]) {
writeBytes((byte[]) value);
}
throw new MessageFormatException("Incorrect object type!");
}
void resetBody() throws JMSException {
reset();
}
}