}
final protected byte[] safeBytes(int amount) throws IOException {
-
byte[] result = new byte[amount];
-
int has = size-byteIndex;
if(amount >= has) {
ReadableByteChannel c = channel;
int got = c.read(bb2);
if(got == -1) throw new IOException("Unexpected end-of-file");
has += got;
+ // For some unknown reason this is needed!
+ // Spec indicates that read would increment position but it does not.
+ bb2.position(has);
}
size = c.read(bb);
bb.position(0);
int result;
if(has == 0) {
ReadableByteChannel c = channel;
- ByteBuffer bb = byteBuffer;
+ ByteBuffer bb = byteBuffer;
size = c.read(bb);
if(size == -1) {
throw new EOFException("Unexpected end-of-file");
if(size == 0)
return -1;
}
- result = bytes[byteIndex];
- if(result < 0)
- result += 256;
- ++byteIndex;
+ result = bytes[byteIndex++] & 0xff;
return result;
}
public int getDynamicUInt32() throws IOException {
- int length = getByte()&0xff;
+ int length = getByte();
if(length >= 0x80) {
if(length >= 0xc0) {
if(length >= 0xe0) {
if(length >= 0xf0) {
length &= 0x0f;
- length += ((getByte()&0xff)<<3);
- length += ((getByte()&0xff)<<11);
- length += ((getByte()&0xff)<<19);
+ length += (getByte()<<3);
+ length += (getByte()<<11);
+ length += (getByte()<<19);
length += 0x10204080;
}
else {
length &= 0x1f;
- length += ((getByte()&0xff)<<4);
- length += ((getByte()&0xff)<<12);
- length += ((getByte()&0xff)<<20);
+ length += (getByte()<<4);
+ length += (getByte()<<12);
+ length += (getByte()<<20);
length += 0x204080;
}
}
else {
length &= 0x3f;
- length += ((getByte()&0xff)<<5);
- length += ((getByte()&0xff)<<13);
+ length += (getByte()<<5);
+ length += (getByte()<<13);
length += 0x4080;
}
}
else {
length &= 0x7f;
- length += ((getByte()&0xff)<<6);
+ length += (getByte()<<6);
length += 0x80;
}
}
final protected int safeInt() throws IOException {
+ byte[] bytes = this.bytes;
+
if(byteIndex >= (size-5)) {
int result = 0;
ReadableByteChannel c = channel;
- ByteBuffer bb = byteBuffer;
+ ByteBuffer bb = byteBuffer;
if(byteIndex == size) {
size = c.read(bb);
if(size == -1) throw new EOFException("Unexpected end-of-file");
bb.position(0);
byteIndex = 0;
}
- result |= ((int)(bytes[byteIndex++]&0xff)<<0);
+ result |= ((int)(bytes[byteIndex++]&0xff));
if(byteIndex == size) {
size = c.read(bb);
bb.position(0);
} else {
return ((bytes[byteIndex++]&0xff)<<24) | ((bytes[byteIndex++]&0xff)<<16) | ((bytes[byteIndex++]&0xff)<<8) | ((bytes[byteIndex++]&0xff));
}
-
+
}
-
+
final protected int getSize() {
return size;
}