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;
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;
}
}

View File

@ -1,11 +1,11 @@
package java.nio;
public class ByteBuffer extends Buffer {
public class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer> {
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;
}

View File

@ -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";
}
}
}