Various bug fixes and optimizations

This commit is contained in:
Eric Scharff 2007-10-11 15:41:23 -06:00
parent 75c51bb5ec
commit 00cfa587bc
3 changed files with 44 additions and 7 deletions

View File

@ -6,17 +6,19 @@ public class ByteBuffer {
private int capacity; private int capacity;
private int position; private int position;
private int limit; private int limit;
private final boolean readOnly;
public static ByteBuffer allocate(int capacity) { public static ByteBuffer allocate(int capacity) {
return new ByteBuffer(new byte[capacity]); return new ByteBuffer(new byte[capacity], false);
} }
public static ByteBuffer wrap(byte[] array) { public static ByteBuffer wrap(byte[] array) {
return new ByteBuffer(array); return new ByteBuffer(array, false);
} }
private ByteBuffer(byte[] array) { private ByteBuffer(byte[] array, boolean readOnly) {
this.array = array; this.array = array;
this.readOnly = readOnly;
arrayOffset = 0; arrayOffset = 0;
capacity = array.length; capacity = array.length;
limit = capacity; limit = capacity;
@ -34,10 +36,10 @@ public class ByteBuffer {
} }
public ByteBuffer slice() { public ByteBuffer slice() {
ByteBuffer buf = new ByteBuffer(array); ByteBuffer buf = new ByteBuffer(array, true);
buf.arrayOffset = arrayOffset + position; buf.arrayOffset = arrayOffset + position;
buf.position = 0; buf.position = 0;
buf.capacity = capacity - position; buf.capacity = remaining();
buf.limit = buf.capacity; buf.limit = buf.capacity;
return buf; return buf;
} }
@ -83,13 +85,15 @@ public class ByteBuffer {
} }
public ByteBuffer put(byte val) { public ByteBuffer put(byte val) {
checkPut(1);
array[arrayOffset+(position++)] = val; array[arrayOffset+(position++)] = val;
return this; return this;
} }
public ByteBuffer put(ByteBuffer src) { public ByteBuffer put(ByteBuffer src) {
checkPut(src.remaining());
put(src.array, src.arrayOffset + src.position, src.remaining()); put(src.array, src.arrayOffset + src.position, src.remaining());
position += src.remaining(); src.position += src.remaining();
return this; return this;
} }
@ -98,12 +102,14 @@ public class ByteBuffer {
} }
public ByteBuffer put(byte[] arr, int offset, int len) { public ByteBuffer put(byte[] arr, int offset, int len) {
checkPut(len);
System.arraycopy(arr, offset, array, arrayOffset+position, len); System.arraycopy(arr, offset, array, arrayOffset+position, len);
position += len; position += len;
return this; return this;
} }
public ByteBuffer putInt(int position, int val) { public ByteBuffer putInt(int position, int val) {
checkPut(position, 4);
array[arrayOffset+position] = (byte)((val >> 24) & 0xff); array[arrayOffset+position] = (byte)((val >> 24) & 0xff);
array[arrayOffset+position+1] = (byte)((val >> 16) & 0xff); array[arrayOffset+position+1] = (byte)((val >> 16) & 0xff);
array[arrayOffset+position+2] = (byte)((val >> 8) & 0xff); array[arrayOffset+position+2] = (byte)((val >> 8) & 0xff);
@ -112,18 +118,21 @@ public class ByteBuffer {
} }
public ByteBuffer putInt(int val) { public ByteBuffer putInt(int val) {
checkPut(4);
putInt(position, val); putInt(position, val);
position += 4; position += 4;
return this; return this;
} }
public ByteBuffer putShort(short val) { public ByteBuffer putShort(short val) {
checkPut(2);
put((byte)((val >> 8) & 0xff)); put((byte)((val >> 8) & 0xff));
put((byte)(val & 0xff)); put((byte)(val & 0xff));
return this; return this;
} }
public ByteBuffer putLong(long val) { public ByteBuffer putLong(long val) {
checkPut(8);
putInt((int)(val >> 32)); putInt((int)(val >> 32));
putInt((int)val); putInt((int)val);
return this; return this;
@ -135,10 +144,12 @@ public class ByteBuffer {
} }
public byte get() { public byte get() {
checkGet(1);
return array[arrayOffset+(position++)]; return array[arrayOffset+(position++)];
} }
public byte get(int position) { public byte get(int position) {
checkGet(position, 1);
return array[arrayOffset+position]; return array[arrayOffset+position];
} }
@ -149,6 +160,7 @@ public class ByteBuffer {
} }
public int getInt() { public int getInt() {
checkGet(4);
int i = get() << 24; int i = get() << 24;
i |= (get() & 0xff) << 16; i |= (get() & 0xff) << 16;
i |= (get() & 0xff) << 8; i |= (get() & 0xff) << 8;
@ -157,14 +169,36 @@ public class ByteBuffer {
} }
public short getShort() { public short getShort() {
checkGet(2);
short s = (short)(get() << 8); short s = (short)(get() << 8);
s |= get() & 0xff; s |= get() & 0xff;
return s; return s;
} }
public long getLong() { public long getLong() {
checkGet(8);
long l = getInt() << 32; long l = getInt() << 32;
l |= getInt() & 0xffffffff; l |= getInt() & 0xffffffff;
return l; return l;
} }
private void checkPut(int amount) {
if (readOnly) throw new ReadOnlyBufferException();
if (amount > limit-position) throw new IndexOutOfBoundsException();
}
private void checkPut(int position, int amount) {
if (readOnly) throw new ReadOnlyBufferException();
if (position < 0 || position+amount > limit)
throw new IndexOutOfBoundsException();
}
private void checkGet(int amount) {
if (amount > limit-position) throw new IndexOutOfBoundsException();
}
private void checkGet(int position, int amount) {
if (position < 0 || position+amount > limit)
throw new IndexOutOfBoundsException();
}
} }

View File

@ -57,6 +57,7 @@ public class SocketChannel extends SelectableChannel
if (! connected) { if (! connected) {
natThrowWriteError(socket); natThrowWriteError(socket);
} }
if (b.remaining() == 0) return 0;
int w = natWrite(socket, b.array(), b.arrayOffset() + b.position(), b.remaining()); int w = natWrite(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
if (w > 0) { if (w > 0) {
b.position(b.position() + w); b.position(b.position() + w);

View File

@ -33,6 +33,7 @@ public class LinkedList<T> implements List<T> {
front = rear = c; front = rear = c;
} else { } else {
c.next = front; c.next = front;
front.prev = c;
front = c; front = c;
} }
} }
@ -44,6 +45,7 @@ public class LinkedList<T> implements List<T> {
front = rear = c; front = rear = c;
} else { } else {
c.prev = rear; c.prev = rear;
rear.next = c;
rear = c; rear = c;
} }
} }
@ -103,7 +105,7 @@ public class LinkedList<T> implements List<T> {
} }
public boolean add(T element) { public boolean add(T element) {
addFirst(element); addLast(element);
return true; return true;
} }