diff --git a/classes/java/nio/Buffer.java b/classes/java/nio/Buffer.java index 6bd9160..9c60429 100644 --- a/classes/java/nio/Buffer.java +++ b/classes/java/nio/Buffer.java @@ -1,38 +1,69 @@ package java.nio; -class Buffer { - protected int mark = -1; - protected int position = 0; - protected int limit; - protected final int capacity; +public abstract class Buffer { + private final int address; + private int position; + private int limit; + private final int capacity; - Buffer(int mark, int position, int limit, int capacity) { - this.mark = mark; + protected Buffer(int address, int position, int limit, int capacity) { + this.address = address; this.position = position; this.limit = limit; this.capacity = capacity; } - public final int position() { - return position; - } - - public final int limit() { - return limit; - } - public final int capacity() { return capacity; } - public Buffer limit(int limit) { - this.limit = limit; - if (position > limit) position = limit; - if (mark > limit) mark = -1; + public final Buffer clear() { + position = 0; + limit = capacity; + return this; + } + + public final Buffer flip() { + limit = position; + position = 0; return this; } public final boolean hasRemaining() { return position < limit; } + + public final int limit() { + return limit; + } + + public Buffer limit(int newLimit) { + if (newLimit < 0 || newLimit > capacity) + throw new IllegalArgumentException(); + this.limit = newLimit; + if (position > newLimit) position = newLimit; + return this; + } + + public final int position() { + return position; + } + + public final Buffer position(int newPosition) { + if (newPosition < 0 || newPosition > limit) + throw new IllegalArgumentException(); + position = newPosition; + } + + public final int remaining() { + int elements = limit - position; + if (elements < 0) + elements = 0; + return elements; + } + + public final Buffer rewind() { + position = 0; + return this; + } } diff --git a/classes/java/nio/ByteBuffer.java b/classes/java/nio/ByteBuffer.java index 2f02349..156066e 100644 --- a/classes/java/nio/ByteBuffer.java +++ b/classes/java/nio/ByteBuffer.java @@ -1,11 +1,11 @@ package java.nio; -public class ByteBuffer extends Buffer { +public class ByteBuffer + extends Buffer + implements Comparable { + private int offset; private boolean bigEndian; - //private boolean sameEndian; - - private byte[] array; private ByteBuffer(byte[] array, int offset, int length) { super(-1, 0, length, length); @@ -13,6 +13,9 @@ public class ByteBuffer extends Buffer { this.order(ByteOrder.BIG_ENDIAN); } + public static ByteBuffer allocateDirect(int capacity) { + } + public static ByteBuffer wrap(byte[] array) { return new ByteBuffer(array, 0, array.length); } @@ -27,7 +30,6 @@ public class ByteBuffer extends Buffer { public ByteBuffer order(ByteOrder bo) { bigEndian = (bo == ByteOrder.BIG_ENDIAN); - //sameEndian = (bo == ByteOrder.nativeOrder()); return this; } diff --git a/classes/java/nio/ByteOrder.java b/classes/java/nio/ByteOrder.java index caae026..7dd2211 100644 --- a/classes/java/nio/ByteOrder.java +++ b/classes/java/nio/ByteOrder.java @@ -12,4 +12,12 @@ public class ByteOrder { public static ByteOrder nativeOrder() { return NATIVE_ORDER; } + + public String toString() { + if (this == BIG_ENDIAN) { + return "BIG_ENDIAN"; + } else { + return "LITTLE_ENDIAN"; + } + } }