This commit is contained in:
Zack Buhman 2025-01-20 12:10:26 -06:00
parent 5fd397563e
commit c5b55e0a14
3 changed files with 65 additions and 24 deletions

View File

@ -1,38 +1,69 @@
package java.nio; package java.nio;
class Buffer { public abstract class Buffer {
protected int mark = -1; private final int address;
protected int position = 0; private int position;
protected int limit; private int limit;
protected final int capacity; private final int capacity;
Buffer(int mark, int position, int limit, int capacity) { protected Buffer(int address, int position, int limit, int capacity) {
this.mark = mark; this.address = address;
this.position = position; this.position = position;
this.limit = limit; this.limit = limit;
this.capacity = capacity; this.capacity = capacity;
} }
public final int position() {
return position;
}
public final int limit() {
return limit;
}
public final int capacity() { public final int capacity() {
return capacity; return capacity;
} }
public Buffer limit(int limit) { public final Buffer clear() {
this.limit = limit; position = 0;
if (position > limit) position = limit; limit = capacity;
if (mark > limit) mark = -1; return this;
}
public final Buffer flip() {
limit = position;
position = 0;
return this; return this;
} }
public final boolean hasRemaining() { public final boolean hasRemaining() {
return position < limit; 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;
}
} }

View File

@ -1,11 +1,11 @@
package java.nio; package java.nio;
public class ByteBuffer extends Buffer { public class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer> {
private int offset; private int offset;
private boolean bigEndian; private boolean bigEndian;
//private boolean sameEndian;
private byte[] array;
private ByteBuffer(byte[] array, int offset, int length) { private ByteBuffer(byte[] array, int offset, int length) {
super(-1, 0, length, length); super(-1, 0, length, length);
@ -13,6 +13,9 @@ public class ByteBuffer extends Buffer {
this.order(ByteOrder.BIG_ENDIAN); this.order(ByteOrder.BIG_ENDIAN);
} }
public static ByteBuffer allocateDirect(int capacity) {
}
public static ByteBuffer wrap(byte[] array) { public static ByteBuffer wrap(byte[] array) {
return new ByteBuffer(array, 0, array.length); return new ByteBuffer(array, 0, array.length);
} }
@ -27,7 +30,6 @@ public class ByteBuffer extends Buffer {
public ByteBuffer order(ByteOrder bo) { public ByteBuffer order(ByteOrder bo) {
bigEndian = (bo == ByteOrder.BIG_ENDIAN); bigEndian = (bo == ByteOrder.BIG_ENDIAN);
//sameEndian = (bo == ByteOrder.nativeOrder());
return this; return this;
} }

View File

@ -12,4 +12,12 @@ public class ByteOrder {
public static ByteOrder nativeOrder() { public static ByteOrder nativeOrder() {
return NATIVE_ORDER; return NATIVE_ORDER;
} }
public String toString() {
if (this == BIG_ENDIAN) {
return "BIG_ENDIAN";
} else {
return "LITTLE_ENDIAN";
}
}
} }