From 02ef9445dddcffc5f701dcb43ef0fa3d6acbc307 Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Wed, 4 Dec 2013 12:08:28 -0500 Subject: [PATCH] Fixing bug where the DataByteArrayInputStream was not throwing EOFExceptions when reads were going past the end of the byte array. This would make it harder to figure out when things went wrong. --- .../activemq/command/XATransactionId.java | 4 +- .../util/DataByteArrayInputStream.java | 58 +++++++++++-------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/command/XATransactionId.java b/activemq-client/src/main/java/org/apache/activemq/command/XATransactionId.java index e963c87258..5f786e56a4 100755 --- a/activemq-client/src/main/java/org/apache/activemq/command/XATransactionId.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/XATransactionId.java @@ -50,7 +50,7 @@ public class XATransactionId extends TransactionId implements Xid, Comparable { this.branchQualifier = xid.getBranchQualifier(); } - public XATransactionId(byte[] encodedBytes) { + public XATransactionId(byte[] encodedBytes) throws IOException { encodedXidBytes = encodedBytes; initFromEncodedBytes(); } @@ -61,7 +61,7 @@ public class XATransactionId extends TransactionId implements Xid, Comparable { final int XID_PREFIX_SIZE = 16; //+|-,(long)lastAck,(byte)priority,(int)formatid,(short)globalLength.... - private void initFromEncodedBytes() { + private void initFromEncodedBytes() throws IOException { DataByteArrayInputStream inputStream = new DataByteArrayInputStream(encodedXidBytes); inputStream.skipBytes(10); formatId = inputStream.readInt(); diff --git a/activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayInputStream.java b/activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayInputStream.java index 9bf229d893..0a13aa0c48 100755 --- a/activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayInputStream.java +++ b/activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayInputStream.java @@ -16,10 +16,7 @@ */ package org.apache.activemq.util; -import java.io.DataInput; -import java.io.IOException; -import java.io.InputStream; -import java.io.UTFDataFormatException; +import java.io.*; /** * Optimized ByteArrayInputStream that can be used more than once @@ -124,6 +121,14 @@ public final class DataByteArrayInputStream extends InputStream implements DataI return (pos < buf.length) ? (buf[pos++] & 0xff) : -1; } + public int readOrIOException() throws IOException { + int rc = read(); + if( rc == -1 ) { + throw new EOFException(); + } + return rc; + } + /** * Reads up to len bytes of data into an array of bytes from * this input stream. @@ -180,45 +185,48 @@ public final class DataByteArrayInputStream extends InputStream implements DataI return n; } - public boolean readBoolean() { - return read() != 0; + public boolean readBoolean() throws IOException { + return readOrIOException() != 0; } - public byte readByte() { - return (byte)read(); + public byte readByte() throws IOException { + return (byte)readOrIOException(); } - public int readUnsignedByte() { - return read(); + public int readUnsignedByte() throws IOException { + return readOrIOException(); } - public short readShort() { - int ch1 = read(); - int ch2 = read(); + public short readShort() throws IOException { + int ch1 = readOrIOException(); + int ch2 = readOrIOException(); return (short)((ch1 << 8) + (ch2 << 0)); } - public int readUnsignedShort() { - int ch1 = read(); - int ch2 = read(); + public int readUnsignedShort() throws IOException { + int ch1 = readOrIOException(); + int ch2 = readOrIOException(); return (ch1 << 8) + (ch2 << 0); } - public char readChar() { - int ch1 = read(); - int ch2 = read(); + public char readChar() throws IOException { + int ch1 = readOrIOException(); + int ch2 = readOrIOException(); return (char)((ch1 << 8) + (ch2 << 0)); } - public int readInt() { - int ch1 = read(); - int ch2 = read(); - int ch3 = read(); - int ch4 = read(); + public int readInt() throws IOException { + int ch1 = readOrIOException(); + int ch2 = readOrIOException(); + int ch3 = readOrIOException(); + int ch4 = readOrIOException(); return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0); } - public long readLong() { + public long readLong() throws IOException { + if (pos >= buf.length ) { + throw new EOFException(); + } long rc = ((long)buf[pos++] << 56) + ((long)(buf[pos++] & 255) << 48) + ((long)(buf[pos++] & 255) << 40) + ((long)(buf[pos++] & 255) << 32); return rc + ((long)(buf[pos++] & 255) << 24) + ((buf[pos++] & 255) << 16) + ((buf[pos++] & 255) << 8) + ((buf[pos++] & 255) << 0); }