Skip to content

Commit

Permalink
Move direct/array accessors to Buffer, always provide direct (#11)
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
niloc132 authored May 15, 2024
1 parent b31dafc commit 9c6e390
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 254 deletions.
31 changes: 31 additions & 0 deletions src/main/java/java/nio/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,35 @@ public final Buffer rewind () {
mark = UNSET_MARK;
return this;
}

/**
* Tells whether or not this buffer is backed by an accessible array.
*
* @return Always returns false.
*/
public boolean hasArray() {
return false;
}

/**
* Always throws UnsupportedOperationException.
*/
public int arrayOffset() {
throw new UnsupportedOperationException();
}

/**
* Always throws UnsupportedOperationException.
*/
public Object array() {
throw new UnsupportedOperationException();
}

/**
* Tells whether or not this buffer is direct.
* @return Always returns true.
*/
public boolean isDirect() {
return true;
}
}
18 changes: 0 additions & 18 deletions src/main/java/java/nio/ByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import elemental2.core.ArrayBuffer;
import elemental2.core.ArrayBufferView;
import elemental2.core.Float64Array;
import elemental2.core.Int8Array;
import org.gwtproject.nio.HasArrayBufferView;
import org.gwtproject.nio.TypedArrayHelper;
Expand Down Expand Up @@ -541,15 +540,6 @@ public final short getShort (int baseOffset) {
return bytes;
}

/** Indicates whether this buffer is based on a byte array and provides read/write access.
*
* @return {@code true} if this buffer is based on a byte array and provides read/write access,
* {@code false} otherwise.
*/
public final boolean hasArray () {
return false;
}

/** Calculates this buffer's hash code from the remaining chars. The position, limit, capacity
* and mark don't affect the hash code.
*
Expand All @@ -564,14 +554,6 @@ public int hashCode () {
return hash;
}

/** Indicates whether this buffer is direct.
*
* @return {@code true} if this buffer is direct, {@code false} otherwise.
*/
public final boolean isDirect () {
return true;
}

/** Returns the byte order used by this buffer when converting bytes from/to other primitive
* types.
* <p> The default byte order of byte buffer is always {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/java/nio/CharBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,6 @@ public abstract class CharBuffer extends Buffer
super(capacity);
}

/** Returns the char array which this buffer is based on, if there is one.
*
* @return the char array which this buffer is based on.
* @exception ReadOnlyBufferException if this buffer is based on an array, but it is read-only.
* @exception UnsupportedOperationException if this buffer is not based on an array.
*/
public final char[] array () {
return protectedArray();
}

/** Returns the offset of the char array which this buffer is based on, if there is one.
* <p>
* The offset is the index of the array corresponds to the zero position of the buffer.
* </p>
*
* @return the offset of the char array which this buffer is based on.
* @exception ReadOnlyBufferException if this buffer is based on an array but it is read-only.
* @exception UnsupportedOperationException if this buffer is not based on an array.
*/
public final int arrayOffset () {
return protectedArrayOffset();
}

/** Returns a read-only buffer that shares its content with this buffer.
* <p> The returned buffer is guaranteed to be a new instance, even if this buffer is read-only
* itself. The new buffer's position, limit, capacity and mark are the same as this buffer's.
Expand Down Expand Up @@ -236,15 +213,6 @@ public CharBuffer get (char[] dest, int off, int len) {
*/
public abstract char get (int index);

/** Indicates whether this buffer is based on a char array and is read/write.
*
* @return {@code true} if this buffer is based on a byte array and provides read/write access,
* {@code false} otherwise.
*/
public final boolean hasArray () {
return protectedHasArray();
}

/** Calculates this buffer's hash code from the remaining chars. The position, limit, capacity
* and mark don't affect the hash code.
*
Expand All @@ -259,16 +227,6 @@ public int hashCode () {
return hash;
}

/** Indicates whether this buffer is direct. A direct buffer will try its best to take
* advantage of native memory APIs and it may not stay in the Java heap, so it is not affected
* by garbage collection.
* <p> A char buffer is direct if it is based on a byte buffer and the byte buffer is direct.
* </p>
*
* @return {@code true} if this buffer is direct, {@code false} otherwise.
*/
public abstract boolean isDirect ();

/** Returns the number of remaining chars.
*
* @return the number of remaining chars.
Expand All @@ -285,21 +243,6 @@ public final int length () {
*/
public abstract ByteOrder order ();

/** Child class implements this method to realize {@code array()}.
*
* @see #array() */
abstract char[] protectedArray ();

/** Child class implements this method to realize {@code arrayOffset()}.
*
* @see #arrayOffset() */
abstract int protectedArrayOffset ();

/** Child class implements this method to realize {@code hasArray()}.
*
* @see #hasArray() */
abstract boolean protectedHasArray ();

/** Writes the given char to the current position and increases the position by 1.
*
* @param c the char to write.
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/java/nio/DoubleBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,6 @@ public abstract class DoubleBuffer extends Buffer implements Comparable<DoubleBu
super(capacity);
}

/** Returns the double array which this buffer is based on, if there is one.
*
* @return the double array which this buffer is based on.
* @exception ReadOnlyBufferException if this buffer is based on an array but it is read-only.
* @exception UnsupportedOperationException if this buffer is not based on an array.
*/
public final double[] array () {
return protectedArray();
}

/** Returns the offset of the double array which this buffer is based on, if there is one.
* <p>
* The offset is the index of the array corresponding to the zero position of the buffer.
* </p>
*
* @return the offset of the double array which this buffer is based on.
* @exception ReadOnlyBufferException if this buffer is based on an array, but it is read-only.
* @exception UnsupportedOperationException if this buffer is not based on an array.
*/
public final int arrayOffset () {
return protectedArrayOffset();
}

/** Returns a read-only buffer that shares its content with this buffer.
* <p> The returned buffer is guaranteed to be a new instance, even if this buffer is read-only
* itself. The new buffer's position, limit, capacity and mark are the same as this buffer's.
Expand Down Expand Up @@ -219,15 +196,6 @@ public DoubleBuffer get (double[] dest, int off, int len) {
*/
public abstract double get (int index);

/** Indicates whether this buffer is based on a double array and is read/write.
*
* @return {@code true} if this buffer is based on a double array and provides read/write
* access, {@code false} otherwise.
*/
public final boolean hasArray () {
return protectedHasArray();
}

// /**
// * Calculates this buffer's hash code from the remaining chars. The
// * position, limit, capacity and mark don't affect the hash code.
Expand All @@ -245,16 +213,6 @@ public final boolean hasArray () {
// return hash;
// }

/** Indicates whether this buffer is direct. A direct buffer will try its best to take advantage
* of native memory APIs and it may not stay in the Java heap, so it is not affected by garbage
* collection.
* <p> A double buffer is direct if it is based on a byte buffer and the byte buffer is direct.
* </p>
*
* @return {@code true} if this buffer is direct, {@code false} otherwise.
*/
public abstract boolean isDirect ();

/** Returns the byte order used by this buffer when converting doubles from/to bytes.
* <p> If this buffer is not based on a byte buffer, then this always returns the platform's
* native byte order. </p>
Expand All @@ -263,21 +221,6 @@ public final boolean hasArray () {
*/
public abstract ByteOrder order ();

/** Child class implements this method to realize {@code array()}.
*
* @see #array() */
abstract double[] protectedArray ();

/** Child class implements this method to realize {@code arrayOffset()}.
*
* @see #arrayOffset() */
abstract int protectedArrayOffset ();

/** Child class implements this method to realize {@code hasArray()}.
*
* @see #hasArray() */
abstract boolean protectedHasArray ();

/** Writes the given double to the current position and increases the position by 1.
*
* @param d the double to write.
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/java/nio/FloatBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,6 @@ public float get (int index) {
return (float)(double)floatArray.getAt(index);
}

/** Indicates whether this buffer is based on a float array and is read/write.
*
* @return {@code true} if this buffer is based on a float array and provides read/write
* access, {@code false} otherwise.
*/
public final boolean hasArray () {
return false;
}

// /**
// * Calculates this buffer's hash code from the remaining chars. The
// * position, limit, capacity and mark don't affect the hash code.
Expand All @@ -246,18 +237,6 @@ public final boolean hasArray () {
// return hash;
// }

/** Indicates whether this buffer is direct. A direct buffer will try its best to take
* advantage of native memory APIs and it may not stay in the Java heap, so it is not affected
* by garbage collection.
* <p> A float buffer is direct if it is based on a byte buffer and the byte buffer is direct.
* </p>
*
* @return {@code true} if this buffer is direct, {@code false} otherwise.
*/
public boolean isDirect () {
return true;
}

/** Returns the byte order used by this buffer when converting floats from/to bytes.
* <p> If this buffer is not based on a byte buffer, then always return the platform's native
* byte order. </p>
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/java/nio/IntBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,6 @@ public int get (int index) {
return (int)(double)intArray.getAt(index);
}

/** Indicates whether this buffer is based on a int array and is read/write.
*
* @return {@code true} if this buffer is based on a int array and provides read/write access,
* {@code false} otherwise.
*/
public final boolean hasArray () {
return false;
}

/** Calculates this buffer's hash code from the remaining chars. The position, limit, capacity
* and mark don't affect the hash code.
*
Expand All @@ -247,19 +238,6 @@ public int hashCode () {
return hash;
}

/** Indicates whether this buffer is direct. A direct buffer will try its best to take
* advantage of native memory APIs and it may not stay in the Java heap, so it is not affected
* by garbage collection.
* <p>
* An int buffer is direct if it is based on a byte buffer and the byte buffer is direct.
* </p>
*
* @return {@code true} if this buffer is direct, {@code false} otherwise.
*/
public boolean isDirect () {
return true;
}

/** Returns the byte order used by this buffer when converting ints from/to bytes.
* <p> If this buffer is not based on a byte buffer, then always return the platform's native
* byte order. </p>
Expand Down
Loading

0 comments on commit 9c6e390

Please sign in to comment.