diff options
author | Robert Greig <rgreig@apache.org> | 2006-10-12 18:19:32 +0000 |
---|---|---|
committer | Robert Greig <rgreig@apache.org> | 2006-10-12 18:19:32 +0000 |
commit | 1e6a034ccd8e260e615195bf193aed7d37b928a8 (patch) | |
tree | 07ba8b13599c75f7c322336667dac75f9fc471e7 /java/client/src | |
parent | 0629359c41c88ab1b7c14c96cb4735fbc25764f7 (diff) | |
download | qpid-python-1e6a034ccd8e260e615195bf193aed7d37b928a8.tar.gz |
QPID-2. JMSBytesMessage.readXXX methods now test the appropriate number of bytes are available in the message and throw the appropriate exception if not. Unit test updated to check behaviour of readXXX methods.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@463363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client/src')
-rw-r--r-- | java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java b/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java index 2cd635f6eb..ba13997e79 100644 --- a/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java +++ b/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java @@ -25,6 +25,7 @@ import org.apache.mina.common.ByteBuffer; import javax.jms.JMSException; import javax.jms.MessageNotReadableException; import javax.jms.MessageNotWriteableException; +import javax.jms.MessageEOFException; import java.io.*; import java.nio.charset.Charset; import java.nio.charset.CharacterCodingException; @@ -141,6 +142,19 @@ public class JMSBytesMessage extends AbstractJMSMessage implements javax.jms.Byt } } + /** + * Check that there is at least a certain number of bytes available to read + * @param len the number of bytes + * @throws MessageEOFException if there are less than len bytes available to read + */ + private void checkAvailable(int len) throws MessageEOFException + { + if (_data.remaining() < len) + { + throw new MessageEOFException("Unable to read " + len + " bytes"); + } + } + private void checkWritable() throws MessageNotWriteableException { if (_readable) @@ -152,66 +166,84 @@ public class JMSBytesMessage extends AbstractJMSMessage implements javax.jms.Byt public boolean readBoolean() throws JMSException { checkReadable(); + checkAvailable(1); return _data.get() != 0; } public byte readByte() throws JMSException { checkReadable(); + checkAvailable(1); return _data.get(); } public int readUnsignedByte() throws JMSException { checkReadable(); + checkAvailable(1); return _data.getUnsigned(); } public short readShort() throws JMSException { checkReadable(); + checkAvailable(2); return _data.getShort(); } public int readUnsignedShort() throws JMSException { checkReadable(); + checkAvailable(2); return _data.getUnsignedShort(); } + /** + * Note that this method reads a unicode character as two bytes from the stream + * @return the character read from the stream + * @throws JMSException + */ public char readChar() throws JMSException { checkReadable(); + checkAvailable(2); return _data.getChar(); } public int readInt() throws JMSException { checkReadable(); + checkAvailable(4); return _data.getInt(); } public long readLong() throws JMSException { checkReadable(); + checkAvailable(8); return _data.getLong(); } public float readFloat() throws JMSException { checkReadable(); + checkAvailable(4); return _data.getFloat(); } public double readDouble() throws JMSException { checkReadable(); + checkAvailable(8); return _data.getDouble(); } public String readUTF() throws JMSException { checkReadable(); + // we check only for one byte since theoretically the string could be only a + // single byte when using UTF-8 encoding + checkAvailable(1); try { return _data.getString(Charset.forName("UTF-8").newDecoder()); @@ -232,8 +264,15 @@ public class JMSBytesMessage extends AbstractJMSMessage implements javax.jms.Byt } checkReadable(); int count = (_data.remaining() >= bytes.length ? bytes.length : _data.remaining()); - _data.get(bytes, 0, count); - return count; + if (count == 0) + { + return -1; + } + else + { + _data.get(bytes, 0, count); + return count; + } } public int readBytes(byte[] bytes, int maxLength) throws JMSException @@ -248,8 +287,15 @@ public class JMSBytesMessage extends AbstractJMSMessage implements javax.jms.Byt } checkReadable(); int count = (_data.remaining() >= maxLength ? maxLength : _data.remaining()); - _data.get(bytes, 0, count); - return count; + if (count == 0) + { + return -1; + } + else + { + _data.get(bytes, 0, count); + return count; + } } public void writeBoolean(boolean b) throws JMSException |