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.
This commit is contained in:
Hiram Chirino 2013-12-04 12:08:28 -05:00
parent 8378cb1ffc
commit 02ef9445dd
2 changed files with 35 additions and 27 deletions

View File

@ -50,7 +50,7 @@ public class XATransactionId extends TransactionId implements Xid, Comparable {
this.branchQualifier = xid.getBranchQualifier(); this.branchQualifier = xid.getBranchQualifier();
} }
public XATransactionId(byte[] encodedBytes) { public XATransactionId(byte[] encodedBytes) throws IOException {
encodedXidBytes = encodedBytes; encodedXidBytes = encodedBytes;
initFromEncodedBytes(); initFromEncodedBytes();
} }
@ -61,7 +61,7 @@ public class XATransactionId extends TransactionId implements Xid, Comparable {
final int XID_PREFIX_SIZE = 16; final int XID_PREFIX_SIZE = 16;
//+|-,(long)lastAck,(byte)priority,(int)formatid,(short)globalLength.... //+|-,(long)lastAck,(byte)priority,(int)formatid,(short)globalLength....
private void initFromEncodedBytes() { private void initFromEncodedBytes() throws IOException {
DataByteArrayInputStream inputStream = new DataByteArrayInputStream(encodedXidBytes); DataByteArrayInputStream inputStream = new DataByteArrayInputStream(encodedXidBytes);
inputStream.skipBytes(10); inputStream.skipBytes(10);
formatId = inputStream.readInt(); formatId = inputStream.readInt();

View File

@ -16,10 +16,7 @@
*/ */
package org.apache.activemq.util; package org.apache.activemq.util;
import java.io.DataInput; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UTFDataFormatException;
/** /**
* Optimized ByteArrayInputStream that can be used more than once * 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; 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 <code>len</code> bytes of data into an array of bytes from * Reads up to <code>len</code> bytes of data into an array of bytes from
* this input stream. * this input stream.
@ -180,45 +185,48 @@ public final class DataByteArrayInputStream extends InputStream implements DataI
return n; return n;
} }
public boolean readBoolean() { public boolean readBoolean() throws IOException {
return read() != 0; return readOrIOException() != 0;
} }
public byte readByte() { public byte readByte() throws IOException {
return (byte)read(); return (byte)readOrIOException();
} }
public int readUnsignedByte() { public int readUnsignedByte() throws IOException {
return read(); return readOrIOException();
} }
public short readShort() { public short readShort() throws IOException {
int ch1 = read(); int ch1 = readOrIOException();
int ch2 = read(); int ch2 = readOrIOException();
return (short)((ch1 << 8) + (ch2 << 0)); return (short)((ch1 << 8) + (ch2 << 0));
} }
public int readUnsignedShort() { public int readUnsignedShort() throws IOException {
int ch1 = read(); int ch1 = readOrIOException();
int ch2 = read(); int ch2 = readOrIOException();
return (ch1 << 8) + (ch2 << 0); return (ch1 << 8) + (ch2 << 0);
} }
public char readChar() { public char readChar() throws IOException {
int ch1 = read(); int ch1 = readOrIOException();
int ch2 = read(); int ch2 = readOrIOException();
return (char)((ch1 << 8) + (ch2 << 0)); return (char)((ch1 << 8) + (ch2 << 0));
} }
public int readInt() { public int readInt() throws IOException {
int ch1 = read(); int ch1 = readOrIOException();
int ch2 = read(); int ch2 = readOrIOException();
int ch3 = read(); int ch3 = readOrIOException();
int ch4 = read(); int ch4 = readOrIOException();
return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0); 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); 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); return rc + ((long)(buf[pos++] & 255) << 24) + ((buf[pos++] & 255) << 16) + ((buf[pos++] & 255) << 8) + ((buf[pos++] & 255) << 0);
} }