You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ge...@apache.org on 2005/12/01 07:04:00 UTC
svn commit: r350181 [128/198] - in
/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core: ./ depends/
depends/files/ depends/jars/ depends/libs/ depends/libs/linux.IA32/
depends/libs/win.IA32/ depends/oss/ depends/oss/linux.IA32/
depends/oss/win....
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/FloatBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/FloatBuffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/FloatBuffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/FloatBuffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,549 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+import com.ibm.io.nio.BufferFactory;
+
+/**
+ * A buffer of <code>float</code>s.
+ * <p>
+ * A float buffer can be created in either of the following ways:
+ * <ul>
+ * <li>{@link #allocate(int) Allocate} a new float array and create a buffer
+ * based on it;</li>
+ * <li>{@link #wrap(float[]) Wrap} an existing float array to create a new
+ * buffer;</li>
+ * <li>Use {@link java.nio.ByteBuffer#asFloatBuffer() ByteBuffer.asFloatBuffer}
+ * to create a float buffer based on a byte buffer.</li>
+ * </ul>
+ * </p>
+ *
+ */
+public abstract class FloatBuffer extends Buffer implements Comparable {
+
+ /**
+ * Creates a float buffer based on a new allocated float array.
+ *
+ * @param capacity The capacity of the new buffer
+ * @return The created float buffer
+ * @throws IllegalArgumentException If <code>capacity</code> is less than zero
+ */
+ public static FloatBuffer allocate(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newFloatBuffer(capacity);
+ }
+
+ /**
+ * Creates a new float buffer by wrapping the given float array.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>wrap(array, 0, array.length)</code>.</p>
+ *
+ * @param array The float array which the new buffer will be based on
+ * @return The created float buffer
+ */
+ public static FloatBuffer wrap(float[] array) {
+ return wrap(array, 0, array.length);
+ }
+
+ /**
+ * Creates new a float buffer by wrapping the given float array.
+ * <p>
+ * The new buffer's position will be <code>start</code>, limit will be
+ * <code>start + len</code>, capacity will be the length of the array.
+ * </p>
+ *
+ * @param array
+ * The float array which the new buffer will be based on
+ * @param start
+ * The start index, must be no less than zero and no greater than
+ * <code>array.length</code>
+ * @param len
+ * The length, must be no less than zero and no greater than
+ * <code>array.length - start</code>
+ * @return The created float buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>start</code> or <code>len</code> is
+ * invalid
+ */
+ public static FloatBuffer wrap(float[] array, int start, int len) {
+ if (start < 0 || start > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || start + len > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ FloatBuffer buf = BufferFactory.newFloatBuffer(array);
+ buf.position = start;
+ buf.limit = start + len;
+
+ return buf;
+ }
+
+ /**
+ * Constructs a <code>FloatBuffer</code> with given capacity.
+ *
+ * @param capacity The capacity of the buffer
+ */
+ protected FloatBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Returns the float array which this buffer is based on, if there's one.
+ *
+ * @return The float array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final float[] array() {
+ return protectedArray();
+ }
+
+ /**
+ * Returns the offset of the float array which this buffer is based on, if
+ * there's one.
+ * <p>
+ * The offset is the index of the array corresponds to the zero position of
+ * the buffer.
+ * </p>
+ *
+ * @return The offset of the float array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final int arrayOffset() {
+ return protectedArrayOffset();
+ }
+
+ /**
+ * Returns a readonly buffer that shares content with this buffer.
+ * <p>
+ * The returned buffer is guaranteed to be a new instance, even this
+ * buffer is readonly itself. The new buffer's position, limit, capacity
+ * and mark are the same as this buffer.</p>
+ * <p>
+ * The new buffer shares content with this buffer, which means
+ * this buffer's change of content will be visible to the new buffer.
+ * The two buffer's position, limit and mark are independent.</p>
+ *
+ * @return A readonly version of this buffer.
+ */
+ public abstract FloatBuffer asReadOnlyBuffer();
+
+ /**
+ * Compacts this float buffer.
+ * <p>
+ * The remaining <code>float</code>s will be moved to the head of the
+ * buffer, staring from position zero. Then the position is set to
+ * <code>remaining()</code>; the limit is set to capacity; the mark is
+ * cleared.
+ * </p>
+ *
+ * @return This buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract FloatBuffer compact();
+
+ /**
+ * Compare the remaining <code>float</code>s of this buffer to another
+ * float buffer's remaining <code>float</code>s.
+ *
+ * @param other
+ * Another float buffer
+ * @return a negative value if this is less than <code>other</code>; 0 if
+ * this equals to <code>other</code>; a positive value if this is
+ * greater than <code>other</code>
+ * @exception ClassCastException
+ * If <code>other</code> is not a float buffer
+ */
+ public int compareTo(Object other) {
+ FloatBuffer otherBuffer = (FloatBuffer) other;
+ int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+ : otherBuffer.remaining();
+ int thisPos = position;
+ int otherPos = otherBuffer.position;
+ float thisByte, otherByte;
+ while (compareRemaining > 0) {
+ thisByte = get(thisPos);
+ otherByte = otherBuffer.get(otherPos);
+ if (thisByte != otherByte) {
+ return thisByte < otherByte ? -1 : 1;
+ }
+ thisPos++;
+ otherPos++;
+ compareRemaining--;
+ }
+ return remaining() - otherBuffer.remaining();
+ }
+
+ /**
+ * Returns a duplicated buffer that shares content with this buffer.
+ * <p>
+ * The duplicated buffer's position, limit, capacity and mark are the
+ * same as this buffer. The duplicated buffer's readonly property and
+ * byte order are same as this buffer too.</p>
+ * <p>
+ * The new buffer shares content with this buffer, which means
+ * either buffer's change of content will be visible to the other.
+ * The two buffer's position, limit and mark are independent.</p>
+ *
+ * @return A duplicated buffer that shares content with this buffer.
+ */
+ public abstract FloatBuffer duplicate();
+
+ /**
+ * Tests whether this float buffer equals to another object.
+ * <p>
+ * If <code>other</code> is not a float buffer, then false is returned.</p>
+ * <p>
+ * Two float buffers are equals if, and only if, their remaining
+ * <code>float</code>s are exactly the same. Position, limit, capacity and
+ * mark are not considered.</p>
+ *
+ * @param other the object to be compared against
+ * @return Whether this float buffer equals to another object.
+ */
+ public boolean equals(Object other) {
+ if (!(other instanceof FloatBuffer)) {
+ return false;
+ }
+ FloatBuffer otherBuffer = (FloatBuffer) other;
+
+ if (remaining() != otherBuffer.remaining()) {
+ return false;
+ }
+
+ int myPosition = position;
+ int otherPosition = otherBuffer.position;
+ boolean equalSoFar = true;
+ while (equalSoFar && (myPosition < limit)) {
+ equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
+ }
+
+ return equalSoFar;
+ }
+
+ /**
+ * Returns the float at the current position and increase the position by 1.
+ *
+ * @return The float at the current position.
+ * @exception BufferUnderflowException If the position is equal or greater than limit
+ */
+ public abstract float get();
+
+ /**
+ * Reads <code>float</code>s from the current position into the specified
+ * float array and increase the position by the number of <code>float</code>s
+ * read.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>get(dest, 0, dest.length)</code>.
+ * </p>
+ *
+ * @param dest
+ * The destination float array
+ * @return This buffer
+ * @exception BufferUnderflowException
+ * if <code>dest.length</code> is greater than
+ * <code>remaining()</code>
+ */
+ public FloatBuffer get(float[] dest) {
+ return get(dest, 0, dest.length);
+ }
+
+ /**
+ * Reads <code>float</code>s from the current position into the specified
+ * float array, starting from the specified offset, and increase the
+ * position by the number of <code>float</code>s read.
+ *
+ * @param dest
+ * The target float array
+ * @param off
+ * The offset of the float array, must be no less than zero and
+ * no greater than <code>dest.length</code>
+ * @param len
+ * The number of <code>float</code>s to read, must be no less
+ * than zero and no greater than <code>dest.length - off</code>
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception BufferUnderflowException
+ * If <code>len</code> is greater than
+ * <code>remaining()</code>
+ */
+ public FloatBuffer get(float[] dest, int off, int len) {
+ if (off < 0 || off > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if ((len < 0) || off + len > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferUnderflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ dest[i] = get();
+ }
+ return this;
+ }
+
+ /**
+ * Returns a float at the specifed index, and the position is not changed.
+ *
+ * @param index The index, must be no less than zero and less than limit
+ * @return A float at the specifed index.
+ * @exception IndexOutOfBoundsException If index is invalid
+ */
+ public abstract float get(int index);
+
+ /**
+ * Returns whether this buffer is based on a float array and is read/write.
+ * <p>
+ * If this buffer is readonly, then false is returned.</p>
+ *
+ * @return Whether this buffer is based on a float array and is read/write.
+ */
+ public final boolean hasArray() {
+ return protectedHasArray();
+ }
+
+ /**
+ * Hash code is calculated from the remaining <code>float</code>s.
+ * <p>
+ * Position, limit, capacity and mark don't affect the hash code.</p>
+ *
+ * @return The hash code calculated from the remaining <code>float</code>s.
+ */
+ public int hashCode() {
+ int myPosition = position;
+ int hash = 0;
+ while (myPosition < limit) {
+ hash = hash + Float.floatToIntBits(get(myPosition++));
+ }
+ return hash;
+ }
+
+ /**
+ * Returns true if this buffer is direct.
+ * <p>
+ * A direct buffer will try its best to take advantage of native memory
+ * APIs and it may not stay in java heap, thus not affected by GC.</p>
+ * <p>
+ * A float buffer is direct, if it is based on a byte buffer and the byte
+ * buffer is direct.</p>
+ *
+ * @return True if this buffer is direct.
+ */
+ public abstract boolean isDirect();
+
+ /**
+ * Returns the byte order used by this buffer when converting
+ * <code>float</code>s from/to <code>byte</code>s.
+ * <p>
+ * If this buffer is not based on a byte buffer, then always return the
+ * platform's native byte order.</p>
+ *
+ * @return The byte order used by this buffer when converting
+ * <code>float</code>s from/to <code>byte</code>s.
+ */
+ public abstract ByteOrder order();
+
+ /**
+ * Child class implements this method to realize <code>array()</code>.
+ *
+ * @return see <code>array()</code>
+ */
+ protected abstract float[] protectedArray();
+
+ /**
+ * Child class implements this method to realize <code>arrayOffset()</code>.
+ *
+ * @return see <code>arrayOffset()</code>
+ */
+ protected abstract int protectedArrayOffset();
+
+ /**
+ * Child class implements this method to realize <code>hasArray()</code>.
+ *
+ * @return see <code>hasArray()</code>
+ */
+ protected abstract boolean protectedHasArray();
+
+ /**
+ * Writes the given float to the current position and increase the position
+ * by 1.
+ *
+ * @param f
+ * The float to write
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If position is equal or greater than limit
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract FloatBuffer put(float f);
+
+ /**
+ * Writes <code>float</code>s in the given float array to the current
+ * position and increase the position by the number of <code>float</code>s
+ * writtern.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>put(src, 0, src.length)</code>.
+ * </p>
+ *
+ * @param src
+ * The source float array
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>src.length</code>
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public final FloatBuffer put(float[] src) {
+ return put(src, 0, src.length);
+ }
+
+ /**
+ * Writes <code>float</code>s in the given float array, starting from the
+ * specified offset, to the current position and increase the position by
+ * the number of <code>float</code>s writtern.
+ *
+ * @param src
+ * The source float array
+ * @param off
+ * The offset of float array, must be no less than zero and no
+ * greater than <code>src.length</code>
+ * @param len
+ * The number of <code>float</code>s to write, must be no less
+ * than zero and no greater than <code>src.length - off</code>
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>len</code>
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public FloatBuffer put(float[] src, int off, int len) {
+ if (off < 0 || off > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || off + len > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferOverflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ put(src[i]);
+ }
+ return this;
+ }
+
+ /**
+ * Writes all the remaining <code>float</code>s of the <code>src</code>
+ * float buffer to this buffer's current position, and increase both
+ * buffers' position by the number of <code>float</code>s copied.
+ *
+ * @param src
+ * The source float buffer
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>src.remaining()</code> is greater than this
+ * buffer's <code>remaining()</code>
+ * @exception IllegalArgumentException
+ * If <code>src</code> is this buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public FloatBuffer put(FloatBuffer src) {
+ if (src == this) {
+ throw new IllegalArgumentException();
+ }
+ if (src.remaining() > remaining()) {
+ throw new BufferOverflowException();
+ }
+ while (src.hasRemaining()) {
+ put(src.get());
+ }
+ return this;
+ }
+
+ /**
+ * Write a float to the specified index of this buffer and the position is
+ * not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than the limit
+ * @param f
+ * The float to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract FloatBuffer put(int index, float f);
+
+ /**
+ * Returns a sliced buffer that shares content with this buffer.
+ * <p>
+ * The sliced buffer's capacity will be this buffer's
+ * <code>remaining()</code>, and its zero position will correspond to
+ * this buffer's current position. The new buffer's position will be
+ * 0, limit will be its capacity, and its mark is unset. The new buffer's
+ * readonly property and byte order are same as this buffer.</p>
+ * <p>
+ * The new buffer shares content with this buffer, which means
+ * either buffer's change of content will be visible to the other.
+ * The two buffer's position, limit and mark are independent.</p>
+ *
+ * @return A sliced buffer that shares content with this buffer.
+ */
+ public abstract FloatBuffer slice();
+
+ /**
+ * Returns a string represents the state of this float buffer.
+ *
+ * @return A string represents the state of this float buffer.
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append(getClass().getName());
+ buf.append(", status: capacity="); //$NON-NLS-1$
+ buf.append(capacity());
+ buf.append(" position="); //$NON-NLS-1$
+ buf.append(position());
+ buf.append(" limit="); //$NON-NLS-1$
+ buf.append(limit());
+ return buf.toString();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/IntBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/IntBuffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/IntBuffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/IntBuffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,566 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+import com.ibm.io.nio.BufferFactory;
+
+/**
+ * A buffer of <code>int</code>s.
+ * <p>
+ * A int buffer can be created in either of the following ways:
+ * <ul>
+ * <li>{@link #allocate(int) Allocate} a new int array and create a buffer
+ * based on it;</li>
+ * <li>{@link #wrap(int[]) Wrap} an existing int array to create a new buffer;</li>
+ * <li>Use {@link java.nio.ByteBuffer#asIntBuffer() ByteBuffer.asIntBuffer} to
+ * create a int buffer based on a byte buffer.</li>
+ * </ul>
+ * </p>
+ *
+ */
+public abstract class IntBuffer extends Buffer implements Comparable {
+
+ /**
+ * Creates a int buffer based on a new allocated int array.
+ *
+ * @param capacity
+ * The capacity of the new buffer
+ * @return The created int buffer
+ * @throws IllegalArgumentException
+ * If <code>capacity</code> is less than zero
+ */
+ public static IntBuffer allocate(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newIntBuffer(capacity);
+ }
+
+ /**
+ * Creates a new int buffer by wrapping the given int array.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>wrap(array, 0, array.length)</code>.
+ * </p>
+ *
+ * @param array
+ * The int array which the new buffer will be based on
+ * @return The created int buffer
+ */
+ public static IntBuffer wrap(int[] array) {
+ return wrap(array, 0, array.length);
+ }
+
+ /**
+ * Creates new a int buffer by wrapping the given int array.
+ * <p>
+ * The new buffer's position will be <code>start</code>, limit will be
+ * <code>start + len</code>, capacity will be the length of the array.
+ * </p>
+ *
+ * @param array
+ * The int array which the new buffer will be based on
+ * @param start
+ * The start index, must be no less than zero and no greater than
+ * <code>array.length</code>
+ * @param len
+ * The length, must be no less than zero and no greater than
+ * <code>array.length - start</code>
+ * @return The created int buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>start</code> or <code>len</code> is
+ * invalid
+ */
+ public static IntBuffer wrap(int[] array, int start, int len) {
+ if (start < 0 || start > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || start + len > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ IntBuffer buf = BufferFactory.newIntBuffer(array);
+ buf.position = start;
+ buf.limit = start + len;
+
+ return buf;
+ }
+
+ /**
+ * Constructs a <code>IntBuffer</code> with given capacity.
+ *
+ * @param capacity
+ * The capacity of the buffer
+ */
+ protected IntBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Returns the int array which this buffer is based on, if there's one.
+ *
+ * @return The int array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final int[] array() {
+ return protectedArray();
+ }
+
+ /**
+ * Returns the offset of the int array which this buffer is based on, if
+ * there's one.
+ * <p>
+ * The offset is the index of the array corresponds to the zero position of
+ * the buffer.
+ * </p>
+ *
+ * @return The offset of the int array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final int arrayOffset() {
+ return protectedArrayOffset();
+ }
+
+ /**
+ * Returns a readonly buffer that shares content with this buffer.
+ * <p>
+ * The returned buffer is guaranteed to be a new instance, even this buffer
+ * is readonly itself. The new buffer's position, limit, capacity and mark
+ * are the same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means this buffer's
+ * change of content will be visible to the new buffer. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A readonly version of this buffer.
+ */
+ public abstract IntBuffer asReadOnlyBuffer();
+
+ /**
+ * Compacts this int buffer.
+ * <p>
+ * The remaining <code>int</code>s will be moved to the head of the
+ * buffer, staring from position zero. Then the position is set to
+ * <code>remaining()</code>; the limit is set to capacity; the mark is
+ * cleared.
+ * </p>
+ *
+ * @return This buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract IntBuffer compact();
+
+ /**
+ * Compare the remaining <code>int</code>s of this buffer to another int
+ * buffer's remaining <code>int</code>s.
+ *
+ * @param other
+ * Another int buffer
+ * @return a negative value if this is less than <code>other</code>; 0 if
+ * this equals to <code>other</code>; a positive value if this is
+ * greater than <code>other</code>
+ * @exception ClassCastException
+ * If <code>other</code> is not a int buffer
+ */
+ public int compareTo(Object other) {
+ IntBuffer otherBuffer = (IntBuffer) other;
+ int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+ : otherBuffer.remaining();
+ int thisPos = position;
+ int otherPos = otherBuffer.position;
+ int thisByte, otherByte;
+ while (compareRemaining > 0) {
+ thisByte = get(thisPos);
+ otherByte = otherBuffer.get(otherPos);
+ if (thisByte != otherByte) {
+ return thisByte < otherByte ? -1 : 1;
+ }
+ thisPos++;
+ otherPos++;
+ compareRemaining--;
+ }
+ return remaining() - otherBuffer.remaining();
+ }
+
+ /**
+ * Returns a duplicated buffer that shares content with this buffer.
+ * <p>
+ * The duplicated buffer's position, limit, capacity and mark are the same
+ * as this buffer. The duplicated buffer's readonly property and byte order
+ * are same as this buffer too.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A duplicated buffer that shares content with this buffer.
+ */
+ public abstract IntBuffer duplicate();
+
+ /**
+ * Tests whether this int buffer equals to another object.
+ * <p>
+ * If <code>other</code> is not a int buffer, then false is returned.
+ * </p>
+ * <p>
+ * Two int buffers are equals if, and only if, their remaining
+ * <code>int</code>s are exactly the same. Position, limit, capacity and
+ * mark are not considered.
+ * </p>
+ *
+ * @param other
+ * the object to be compared against
+ * @return Whether this int buffer equals to another object.
+ */
+ public boolean equals(Object other) {
+ if (!(other instanceof IntBuffer)) {
+ return false;
+ }
+ IntBuffer otherBuffer = (IntBuffer) other;
+
+ if (remaining() != otherBuffer.remaining()) {
+ return false;
+ }
+
+ int myPosition = position;
+ int otherPosition = otherBuffer.position;
+ boolean equalSoFar = true;
+ while (equalSoFar && (myPosition < limit)) {
+ equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
+ }
+
+ return equalSoFar;
+ }
+
+ /**
+ * Returns the int at the current position and increase the position by 1.
+ *
+ * @return The int at the current position.
+ * @exception BufferUnderflowException
+ * If the position is equal or greater than limit
+ */
+ public abstract int get();
+
+ /**
+ * Reads <code>int</code>s from the current position into the specified
+ * int array and increase the position by the number of <code>int</code>s
+ * read.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>get(dest, 0, dest.length)</code>.
+ * </p>
+ *
+ * @param dest
+ * The destination int array
+ * @return This buffer
+ * @exception BufferUnderflowException
+ * if <code>dest.length</code> is greater than
+ * <code>remaining()</code>
+ */
+ public IntBuffer get(int[] dest) {
+ return get(dest, 0, dest.length);
+ }
+
+ /**
+ * Reads <code>int</code>s from the current position into the specified
+ * int array, starting from the specified offset, and increase the position
+ * by the number of <code>int</code>s read.
+ *
+ * @param dest
+ * The target int array
+ * @param off
+ * The offset of the int array, must be no less than zero and no
+ * greater than <code>dest.length</code>
+ * @param len
+ * The number of <code>int</code>s to read, must be no less
+ * than zero and no greater than <code>dest.length - off</code>
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception BufferUnderflowException
+ * If <code>len</code> is greater than
+ * <code>remaining()</code>
+ */
+ public IntBuffer get(int[] dest, int off, int len) {
+ if (off < 0 || off > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if ((len < 0) || off + len > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferUnderflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ dest[i] = get();
+ }
+ return this;
+ }
+
+ /**
+ * Returns a int at the specifed index, and the position is not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than limit
+ * @return A int at the specifed index.
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ */
+ public abstract int get(int index);
+
+ /**
+ * Returns whether this buffer is based on a int array and is read/write.
+ * <p>
+ * If this buffer is readonly, then false is returned.
+ * </p>
+ *
+ * @return Whether this buffer is based on a int array and is read/write.
+ */
+ public final boolean hasArray() {
+ return protectedHasArray();
+ }
+
+ /**
+ * Hash code is calculated from the remaining <code>int</code>s.
+ * <p>
+ * Position, limit, capacity and mark don't affect the hash code.
+ * </p>
+ *
+ * @return The hash code calculated from the remaining <code>int</code>s.
+ */
+ public int hashCode() {
+ int myPosition = position;
+ int hash = 0;
+ while (myPosition < limit) {
+ hash = hash + get(myPosition++);
+ }
+ return hash;
+ }
+
+ /**
+ * Returns true if this buffer is direct.
+ * <p>
+ * A int buffer is direct, if it is based on a byte buffer and the byte
+ * buffer is direct.
+ * </p>
+ *
+ * @return True if this buffer is direct.
+ */
+ public abstract boolean isDirect();
+
+ /**
+ * Returns the byte order used by this buffer when converting
+ * <code>int</code>s from/to <code>byte</code>s.
+ * <p>
+ * If this buffer is not based on a byte buffer, then always return the
+ * platform's native byte order.
+ * </p>
+ *
+ * @return The byte order used by this buffer when converting
+ * <code>int</code>s from/to <code>byte</code>s.
+ */
+ public abstract ByteOrder order();
+
+ /**
+ * Child class implements this method to realize <code>array()</code>.
+ *
+ * @return see <code>array()</code>
+ */
+ protected abstract int[] protectedArray();
+
+ /**
+ * Child class implements this method to realize <code>arrayOffset()</code>.
+ *
+ * @return see <code>arrayOffset()</code>
+ */
+ protected abstract int protectedArrayOffset();
+
+ /**
+ * Child class implements this method to realize <code>hasArray()</code>.
+ *
+ * @return see <code>hasArray()</code>
+ */
+ protected abstract boolean protectedHasArray();
+
+ /**
+ * Writes the given int to the current position and increase the position by
+ * 1.
+ *
+ * @param i
+ * The int to write
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If position is equal or greater than limit
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract IntBuffer put(int i);
+
+ /**
+ * Writes <code>int</code>s in the given int array to the current
+ * position and increase the position by the number of <code>int</code>s
+ * writtern.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>put(src, 0, src.length)</code>.
+ * </p>
+ *
+ * @param src
+ * The source int array
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>src.length</code>
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public final IntBuffer put(int[] src) {
+ return put(src, 0, src.length);
+ }
+
+ /**
+ * Writes <code>int</code>s in the given int array, starting from the
+ * specified offset, to the current position and increase the position by
+ * the number of <code>int</code>s writtern.
+ *
+ * @param src
+ * The source int array
+ * @param off
+ * The offset of int array, must be no less than zero and no
+ * greater than <code>src.length</code>
+ * @param len
+ * The number of <code>int</code>s to write, must be no less
+ * than zero and no greater than <code>src.length - off</code>
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>len</code>
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public IntBuffer put(int[] src, int off, int len) {
+ if (off < 0 || off > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || off + len > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferOverflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ put(src[i]);
+ }
+ return this;
+ }
+
+ /**
+ * Writes all the remaining <code>int</code>s of the <code>src</code>
+ * int buffer to this buffer's current position, and increase both buffers'
+ * position by the number of <code>int</code>s copied.
+ *
+ * @param src
+ * The source int buffer
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>src.remaining()</code> is greater than this
+ * buffer's <code>remaining()</code>
+ * @exception IllegalArgumentException
+ * If <code>src</code> is this buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public IntBuffer put(IntBuffer src) {
+ if (src == this) {
+ throw new IllegalArgumentException();
+ }
+ if (src.remaining() > remaining()) {
+ throw new BufferOverflowException();
+ }
+ while (src.hasRemaining()) {
+ put(src.get());
+ }
+ return this;
+ }
+
+ /**
+ * Write a int to the specified index of this buffer and the position is not
+ * changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than the limit
+ * @param i
+ * The int to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract IntBuffer put(int index, int i);
+
+ /**
+ * Returns a sliced buffer that shares content with this buffer.
+ * <p>
+ * The sliced buffer's capacity will be this buffer's
+ * <code>remaining()</code>, and its zero position will correspond to
+ * this buffer's current position. The new buffer's position will be 0,
+ * limit will be its capacity, and its mark is unset. The new buffer's
+ * readonly property and byte order are same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A sliced buffer that shares content with this buffer.
+ */
+ public abstract IntBuffer slice();
+
+ /**
+ * Returns a string represents the state of this int buffer.
+ *
+ * @return A string represents the state of this int buffer.
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append(getClass().getName());
+ buf.append(", status: capacity="); //$NON-NLS-1$
+ buf.append(capacity());
+ buf.append(" position="); //$NON-NLS-1$
+ buf.append(position());
+ buf.append(" limit="); //$NON-NLS-1$
+ buf.append(limit());
+ return buf.toString();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/InvalidMarkException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/InvalidMarkException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/InvalidMarkException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/InvalidMarkException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,34 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+/**
+ * A <code>InvalidMarkException</code> is thrown when <code>reset()</code>
+ * is called on a buffer, but there is no mark set previously.
+ *
+ */
+public class InvalidMarkException extends IllegalStateException {
+
+ static final long serialVersionUID = 1698329710438510774L;
+
+ /**
+ * Constructs an <code>InvalidMarkException</code>.
+ */
+ public InvalidMarkException() {
+ super();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/LongBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/LongBuffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/LongBuffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/LongBuffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,573 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+import com.ibm.io.nio.BufferFactory;
+
+/**
+ * A buffer of <code>long</code>s.
+ * <p>
+ * A long buffer can be created in either of the following ways:
+ * <ul>
+ * <li>{@link #allocate(int) Allocate} a new long array and create a buffer
+ * based on it;</li>
+ * <li>{@link #wrap(long[]) Wrap} an existing long array to create a new
+ * buffer;</li>
+ * <li>Use {@link java.nio.ByteBuffer#asLongBuffer() ByteBuffer.asLongBuffer}
+ * to create a long buffer based on a byte buffer.</li>
+ * </ul>
+ * </p>
+ *
+ */
+public abstract class LongBuffer extends Buffer implements Comparable {
+
+ /**
+ * Creates a long buffer based on a new allocated long array.
+ *
+ * @param capacity
+ * The capacity of the new buffer
+ * @return The created long buffer
+ * @throws IllegalArgumentException
+ * If <code>capacity</code> is less than zero
+ */
+ public static LongBuffer allocate(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newLongBuffer(capacity);
+ }
+
+ /**
+ * Creates a new long buffer by wrapping the given long array.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>wrap(array, 0, array.length)</code>.
+ * </p>
+ *
+ * @param array
+ * The long array which the new buffer will be based on
+ * @return The created long buffer
+ */
+ public static LongBuffer wrap(long[] array) {
+ return wrap(array, 0, array.length);
+ }
+
+ /**
+ * Creates new a long buffer by wrapping the given long array.
+ * <p>
+ * The new buffer's position will be <code>start</code>, limit will be
+ * <code>start + len</code>, capacity will be the length of the array.
+ * </p>
+ *
+ * @param array
+ * The long array which the new buffer will be based on
+ * @param start
+ * The start index, must be no less than zero and no greater than
+ * <code>array.length</code>
+ * @param len
+ * The length, must be no less than zero and no greater than
+ * <code>array.length - start</code>
+ * @return The created long buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>start</code> or <code>len</code> is
+ * invalid
+ */
+ public static LongBuffer wrap(long[] array, int start, int len) {
+ if (start < 0 || start > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || start + len > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ LongBuffer buf = BufferFactory.newLongBuffer(array);
+ buf.position = start;
+ buf.limit = start + len;
+
+ return buf;
+ }
+
+ /**
+ * Constructs a <code>LongBuffer</code> with given capacity.
+ *
+ * @param capacity
+ * The capacity of the buffer
+ */
+ protected LongBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Returns the long array which this buffer is based on, if there's one.
+ *
+ * @return The long array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final long[] array() {
+ return protectedArray();
+ }
+
+ /**
+ * Returns the offset of the long array which this buffer is based on, if
+ * there's one.
+ * <p>
+ * The offset is the index of the array corresponds to the zero position of
+ * the buffer.
+ * </p>
+ *
+ * @return The offset of the long array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final int arrayOffset() {
+ return protectedArrayOffset();
+ }
+
+ /**
+ * Returns a readonly buffer that shares content with this buffer.
+ * <p>
+ * The returned buffer is guaranteed to be a new instance, even this buffer
+ * is readonly itself. The new buffer's position, limit, capacity and mark
+ * are the same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means this buffer's
+ * change of content will be visible to the new buffer. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A readonly version of this buffer.
+ */
+ public abstract LongBuffer asReadOnlyBuffer();
+
+ /**
+ * Compacts this long buffer.
+ * <p>
+ * The remaining <code>long</code>s will be moved to the head of the
+ * buffer, staring from position zero. Then the position is set to
+ * <code>remaining()</code>; the limit is set to capacity; the mark is
+ * cleared.
+ * </p>
+ *
+ * @return This buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract LongBuffer compact();
+
+ /**
+ * Compare the remaining <code>long</code>s of this buffer to another
+ * long buffer's remaining <code>long</code>s.
+ *
+ * @param other
+ * Another long buffer
+ * @return a negative value if this is less than <code>other</code>; 0 if
+ * this equals to <code>other</code>; a positive value if this is
+ * greater than <code>other</code>
+ * @exception ClassCastException
+ * If <code>other</code> is not a long buffer
+ */
+ public int compareTo(Object other) {
+ LongBuffer otherBuffer = (LongBuffer) other;
+ int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+ : otherBuffer.remaining();
+ int thisPos = position;
+ int otherPos = otherBuffer.position;
+ long thisByte, otherByte;
+ while (compareRemaining > 0) {
+ thisByte = get(thisPos);
+ otherByte = otherBuffer.get(otherPos);
+ if (thisByte != otherByte) {
+ return thisByte < otherByte ? -1 : 1;
+ }
+ thisPos++;
+ otherPos++;
+ compareRemaining--;
+ }
+ return remaining() - otherBuffer.remaining();
+ }
+
+ /**
+ * Returns a duplicated buffer that shares content with this buffer.
+ * <p>
+ * The duplicated buffer's position, limit, capacity and mark are the same
+ * as this buffer. The duplicated buffer's readonly property and byte order
+ * are same as this buffer too.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A duplicated buffer that shares content with this buffer.
+ */
+ public abstract LongBuffer duplicate();
+
+ /**
+ * Tests whether this long buffer equals to another object.
+ * <p>
+ * If <code>other</code> is not a long buffer, then false is returned.
+ * </p>
+ * <p>
+ * Two long buffers are equals if, and only if, their remaining
+ * <code>long</code>s are exactly the same. Position, limit, capacity and
+ * mark are not considered.
+ * </p>
+ *
+ * @param other
+ * the object to be compared against
+ * @return Whether this long buffer equals to another object.
+ */
+ public boolean equals(Object other) {
+ if (!(other instanceof LongBuffer)) {
+ return false;
+ }
+ LongBuffer otherBuffer = (LongBuffer) other;
+
+ if (remaining() != otherBuffer.remaining()) {
+ return false;
+ }
+
+ int myPosition = position;
+ int otherPosition = otherBuffer.position;
+ boolean equalSoFar = true;
+ while (equalSoFar && (myPosition < limit)) {
+ equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
+ }
+
+ return equalSoFar;
+ }
+
+ /**
+ * Returns the long at the current position and increase the position by 1.
+ *
+ * @return The long at the current position.
+ * @exception BufferUnderflowException
+ * If the position is equal or greater than limit
+ */
+ public abstract long get();
+
+ /**
+ * Reads <code>long</code>s from the current position into the specified
+ * long array and increase the position by the number of <code>long</code>s
+ * read.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>get(dest, 0, dest.length)</code>.
+ * </p>
+ *
+ * @param dest
+ * The destination long array
+ * @return This buffer
+ * @exception BufferUnderflowException
+ * if <code>dest.length</code> is greater than
+ * <code>remaining()</code>
+ */
+ public LongBuffer get(long[] dest) {
+ return get(dest, 0, dest.length);
+ }
+
+ /**
+ * Reads <code>long</code>s from the current position into the specified
+ * long array, starting from the specified offset, and increase the position
+ * by the number of <code>long</code>s read.
+ *
+ * @param dest
+ * The target long array
+ * @param off
+ * The offset of the long array, must be no less than zero and no
+ * greater than <code>dest.length</code>
+ * @param len
+ * The number of <code>long</code>s to read, must be no less
+ * than zero and no greater than <code>dest.length - off</code>
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception BufferUnderflowException
+ * If <code>len</code> is greater than
+ * <code>remaining()</code>
+ */
+ public LongBuffer get(long[] dest, int off, int len) {
+ if (off < 0 || off > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if ((len < 0) || off + len > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferUnderflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ dest[i] = get();
+ }
+ return this;
+ }
+
+ /**
+ * Returns a long at the specifed index, and the position is not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than limit
+ * @return A long at the specifed index.
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ */
+ public abstract long get(int index);
+
+ /**
+ * Returns whether this buffer is based on a long array and is read/write.
+ * <p>
+ * If this buffer is readonly, then false is returned.
+ * </p>
+ *
+ * @return Whether this buffer is based on a long array and is read/write.
+ */
+ public final boolean hasArray() {
+ return protectedHasArray();
+ }
+
+ /**
+ * Hash code is calculated from the remaining <code>long</code>s.
+ * <p>
+ * Position, limit, capacity and mark don't affect the hash code.
+ * </p>
+ *
+ * @return The hash code calculated from the remaining <code>long</code>s.
+ */
+ public int hashCode() {
+ int myPosition = position;
+ int hash = 0;
+ long l;
+ while (myPosition < limit) {
+ l = get(myPosition++);
+ hash = hash + ((int) l) ^ ((int) (l >> 32));
+ }
+ return hash;
+ }
+
+ /**
+ * Returns true if this buffer is direct.
+ * <p>
+ * A direct buffer will try its best to take advantage of native memory APIs
+ * and it may not stay in java heap, thus not affected by GC.
+ * </p>
+ * <p>
+ * A long buffer is direct, if it is based on a byte buffer and the byte
+ * buffer is direct.
+ * </p>
+ *
+ * @return True if this buffer is direct.
+ */
+ public abstract boolean isDirect();
+
+ /**
+ * Returns the byte order used by this buffer when converting
+ * <code>long</code>s from/to <code>byte</code>s.
+ * <p>
+ * If this buffer is not based on a byte buffer, then always return the
+ * platform's native byte order.
+ * </p>
+ *
+ * @return The byte order used by this buffer when converting
+ * <code>long</code>s from/to <code>byte</code>s.
+ */
+ public abstract ByteOrder order();
+
+ /**
+ * Child class implements this method to realize <code>array()</code>.
+ *
+ * @return see <code>array()</code>
+ */
+ protected abstract long[] protectedArray();
+
+ /**
+ * Child class implements this method to realize <code>arrayOffset()</code>.
+ *
+ * @return see <code>arrayOffset()</code>
+ */
+ protected abstract int protectedArrayOffset();
+
+ /**
+ * Child class implements this method to realize <code>hasArray()</code>.
+ *
+ * @return see <code>hasArray()</code>
+ */
+ protected abstract boolean protectedHasArray();
+
+ /**
+ * Writes the given long to the current position and increase the position
+ * by 1.
+ *
+ * @param l
+ * The long to write
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If position is equal or greater than limit
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract LongBuffer put(long l);
+
+ /**
+ * Writes <code>long</code>s in the given long array to the current
+ * position and increase the position by the number of <code>long</code>s
+ * writtern.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>put(src, 0, src.length)</code>.
+ * </p>
+ *
+ * @param src
+ * The source long array
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>src.length</code>
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public final LongBuffer put(long[] src) {
+ return put(src, 0, src.length);
+ }
+
+ /**
+ * Writes <code>long</code>s in the given long array, starting from the
+ * specified offset, to the current position and increase the position by
+ * the number of <code>long</code>s writtern.
+ *
+ * @param src
+ * The source long array
+ * @param off
+ * The offset of long array, must be no less than zero and no
+ * greater than <code>src.length</code>
+ * @param len
+ * The number of <code>long</code>s to write, must be no less
+ * than zero and no greater than <code>src.length - off</code>
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>len</code>
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public LongBuffer put(long[] src, int off, int len) {
+ if (off < 0 || off > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || off + len > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferOverflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ put(src[i]);
+ }
+ return this;
+ }
+
+ /**
+ * Writes all the remaining <code>long</code>s of the <code>src</code>
+ * long buffer to this buffer's current position, and increase both buffers'
+ * position by the number of <code>long</code>s copied.
+ *
+ * @param src
+ * The source long buffer
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>src.remaining()</code> is greater than this
+ * buffer's <code>remaining()</code>
+ * @exception IllegalArgumentException
+ * If <code>src</code> is this buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public LongBuffer put(LongBuffer src) {
+ if (src == this) {
+ throw new IllegalArgumentException();
+ }
+ if (src.remaining() > remaining()) {
+ throw new BufferOverflowException();
+ }
+ while (src.hasRemaining()) {
+ put(src.get());
+ }
+ return this;
+ }
+
+ /**
+ * Write a long to the specified index of this buffer and the position is
+ * not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than the limit
+ * @param l
+ * The long to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract LongBuffer put(int index, long l);
+
+ /**
+ * Returns a sliced buffer that shares content with this buffer.
+ * <p>
+ * The sliced buffer's capacity will be this buffer's
+ * <code>remaining()</code>, and its zero position will correspond to
+ * this buffer's current position. The new buffer's position will be 0,
+ * limit will be its capacity, and its mark is unset. The new buffer's
+ * readonly property and byte order are same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A sliced buffer that shares content with this buffer.
+ */
+ public abstract LongBuffer slice();
+
+ /**
+ * Returns a string represents the state of this long buffer.
+ *
+ * @return A string represents the state of this long buffer.
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append(getClass().getName());
+ buf.append(", status: capacity="); //$NON-NLS-1$
+ buf.append(capacity());
+ buf.append(" position="); //$NON-NLS-1$
+ buf.append(position());
+ buf.append(" limit="); //$NON-NLS-1$
+ buf.append(limit());
+ return buf.toString();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/MappedByteBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/MappedByteBuffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/MappedByteBuffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/MappedByteBuffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,181 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel.MapMode;
+
+/**
+ * <code>MappedByteBuffer</code> is a special kind of direct byte buffer,
+ * which maps a region of file to memory.
+ * <p>
+ * <code>MappedByteBuffer</code> can be created by calling
+ * {@link java.nio.channels.FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long) FileChannel.map}.
+ * Once created, the mapping between the byte buffer and the file region remains
+ * valid until the byte buffer is garbage collected.
+ * </p>
+ * <p>
+ * All or part of a <code>MappedByteBuffer</code>'s content may change or
+ * become inaccessible at any time, since the mapped file region can be modified
+ * by another thread or process at any time. If this happens, the behavior of
+ * the <code>MappedByteBuffer</code> is undefined.
+ * </p>
+ *
+ */
+public abstract class MappedByteBuffer extends ByteBuffer {
+
+ /**
+ * The map mode used when creating this buffer.
+ */
+ protected final MapMode mapMode;
+
+ /**
+ * The mapped file.
+ */
+ protected final File mappedFile;
+
+ /**
+ * The offset of the mapped region. The size of the mapped region is defined
+ * by capacity.
+ */
+ protected final long offset;
+
+ /**
+ * The wrapped byte buffer.
+ */
+ protected final ByteBuffer wrappedBuffer;
+
+ /**
+ * Create a new mapped byte buffer, mapping to the specified region of file.
+ * A new direct byte buffer will be allocated.
+ *
+ * @param mappedFile
+ * @param offset
+ * @param size
+ * @param mapMode
+ */
+ protected MappedByteBuffer(File mappedFile, long offset, int size,
+ MapMode mapMode) {
+ super(size);
+ this.mappedFile = mappedFile;
+ this.offset = offset;
+ this.mapMode = mapMode;
+ this.wrappedBuffer = ByteBuffer.allocateDirect(size);
+
+ load();
+ }
+
+ /**
+ * Create a new mapped byte buffer, mapping to the specified region of file.
+ * The specified byte buffer is used.
+ *
+ * @param mappedFile
+ * @param offset
+ * @param size
+ * @param mapMode
+ * @param wrappedBuffer
+ */
+ protected MappedByteBuffer(File mappedFile, long offset, int size,
+ MapMode mapMode, ByteBuffer wrappedBuffer) {
+ super(size);
+ this.mappedFile = mappedFile;
+ this.offset = offset;
+ this.mapMode = mapMode;
+ this.wrappedBuffer = wrappedBuffer;
+ this.wrappedBuffer.clear();
+
+ if (wrappedBuffer.capacity() != size) {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ /**
+ * Writes all changes made to this buffer's content back to the file.
+ * <p>
+ * If this buffer is not mapped in read/write mode, then this method does
+ * nothing.
+ * </p>
+ *
+ * @return This buffer
+ */
+ public final MappedByteBuffer force() {
+ // TODO re-impl in a direct way
+ if (mapMode == MapMode.READ_WRITE) {
+ RandomAccessFile accessFile = null;
+ try {
+ accessFile = new RandomAccessFile(mappedFile, "rw"); //$NON-NLS-1$
+ accessFile.seek(offset);
+ for (int i = 0; i < capacity; i++) {
+ accessFile.writeByte(wrappedBuffer.get(i));
+ }
+ } catch (IOException e) {
+ // javadoc does not specify how to report this exception
+ e.printStackTrace();
+ } finally {
+ if (accessFile != null) {
+ try {
+ accessFile.close();
+ } catch (IOException e) {
+ // nothing to do
+ }
+ }
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Returns true if this buffer's content is loaded.
+ *
+ * @return True if this buffer's content is loaded.
+ */
+ public final boolean isLoaded() {
+ return true;
+ }
+
+ /**
+ * Loads this buffer's content into memory.
+ *
+ * @return This buffer
+ */
+ public final MappedByteBuffer load() {
+ // TODO re-impl in a direct way
+ RandomAccessFile accessFile = null;
+ try {
+ accessFile = new RandomAccessFile(mappedFile, "r"); //$NON-NLS-1$
+ accessFile.seek(offset);
+ for (int i = 0; i < capacity; i++) {
+ wrappedBuffer.put(i, accessFile.readByte());
+ }
+ } catch (IOException e) {
+ // javadoc does not specify how to report this exception
+ e.printStackTrace();
+ } finally {
+ if (accessFile != null) {
+ try {
+ accessFile.close();
+ } catch (IOException e) {
+ // nothing to do
+ }
+ }
+ }
+ return this;
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ReadOnlyBufferException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ReadOnlyBufferException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ReadOnlyBufferException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ReadOnlyBufferException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,34 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+/**
+ * A <code>ReadOnlyBufferException</code> is thrown when some write operation
+ * is called on a readonly buffer.
+ *
+ */
+public class ReadOnlyBufferException extends UnsupportedOperationException {
+
+ static final long serialVersionUID = -1210063976496234090L;
+
+ /**
+ * Constructs a <code>ReadOnlyBufferException</code>.
+ */
+ public ReadOnlyBufferException() {
+ super();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ShortBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ShortBuffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ShortBuffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/ShortBuffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,571 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+import com.ibm.io.nio.BufferFactory;
+
+/**
+ * A buffer of <code>short</code>s.
+ * <p>
+ * A short buffer can be created in either of the following ways:
+ * <ul>
+ * <li>{@link #allocate(int) Allocate} a new short array and create a buffer
+ * based on it;</li>
+ * <li>{@link #wrap(short[]) Wrap} an existing short array to create a new
+ * buffer;</li>
+ * <li>Use {@link java.nio.ByteBuffer#asShortBuffer() ByteBuffer.asShortBuffer}
+ * to create a short buffer based on a byte buffer.</li>
+ * </ul>
+ * </p>
+ *
+ */
+public abstract class ShortBuffer extends Buffer implements Comparable {
+
+ /**
+ * Creates a short buffer based on a new allocated short array.
+ *
+ * @param capacity
+ * The capacity of the new buffer
+ * @return The created short buffer
+ * @throws IllegalArgumentException
+ * If <code>capacity</code> is less than zero
+ */
+ public static ShortBuffer allocate(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newShortBuffer(capacity);
+ }
+
+ /**
+ * Creates a new short buffer by wrapping the given short array.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>wrap(array, 0, array.length)</code>.
+ * </p>
+ *
+ * @param array
+ * The short array which the new buffer will be based on
+ * @return The created short buffer
+ */
+ public static ShortBuffer wrap(short[] array) {
+ return wrap(array, 0, array.length);
+ }
+
+ /**
+ * Creates new a short buffer by wrapping the given short array.
+ * <p>
+ * The new buffer's position will be <code>start</code>, limit will be
+ * <code>start + len</code>, capacity will be the length of the array.
+ * </p>
+ *
+ * @param array
+ * The short array which the new buffer will be based on
+ * @param start
+ * The start index, must be no less than zero and no greater than
+ * <code>array.length</code>
+ * @param len
+ * The length, must be no less than zero and no greater than
+ * <code>array.length - start</code>
+ * @return The created short buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>start</code> or <code>len</code> is
+ * invalid
+ */
+ public static ShortBuffer wrap(short[] array, int start, int len) {
+ if (start < 0 || start > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || start + len > array.length) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ ShortBuffer buf = BufferFactory.newShortBuffer(array);
+ buf.position = start;
+ buf.limit = start + len;
+
+ return buf;
+ }
+
+ /**
+ * Constructs a <code>ShortBuffer</code> with given capacity.
+ *
+ * @param capacity
+ * The capacity of the buffer
+ */
+ protected ShortBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Returns the short array which this buffer is based on, if there's one.
+ *
+ * @return The short array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final short[] array() {
+ return protectedArray();
+ }
+
+ /**
+ * Returns the offset of the short array which this buffer is based on, if
+ * there's one.
+ * <p>
+ * The offset is the index of the array corresponds to the zero position of
+ * the buffer.
+ * </p>
+ *
+ * @return The offset of the short array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on an array, but it is readonly
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final int arrayOffset() {
+ return protectedArrayOffset();
+ }
+
+ /**
+ * Returns a readonly buffer that shares content with this buffer.
+ * <p>
+ * The returned buffer is guaranteed to be a new instance, even this buffer
+ * is readonly itself. The new buffer's position, limit, capacity and mark
+ * are the same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means this buffer's
+ * change of content will be visible to the new buffer. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A readonly version of this buffer.
+ */
+ public abstract ShortBuffer asReadOnlyBuffer();
+
+ /**
+ * Compacts this short buffer.
+ * <p>
+ * The remaining <code>short</code>s will be moved to the head of the
+ * buffer, staring from position zero. Then the position is set to
+ * <code>remaining()</code>; the limit is set to capacity; the mark is
+ * cleared.
+ * </p>
+ *
+ * @return This buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ShortBuffer compact();
+
+ /**
+ * Compare the remaining <code>short</code>s of this buffer to another
+ * short buffer's remaining <code>short</code>s.
+ *
+ * @param other
+ * Another short buffer
+ * @return a negative value if this is less than <code>other</code>; 0 if
+ * this equals to <code>other</code>; a positive value if this is
+ * greater than <code>other</code>
+ * @exception ClassCastException
+ * If <code>other</code> is not a short buffer
+ */
+ public int compareTo(Object other) {
+ ShortBuffer otherBuffer = (ShortBuffer) other;
+ int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+ : otherBuffer.remaining();
+ int thisPos = position;
+ int otherPos = otherBuffer.position;
+ short thisByte, otherByte;
+ while (compareRemaining > 0) {
+ thisByte = get(thisPos);
+ otherByte = otherBuffer.get(otherPos);
+ if (thisByte != otherByte) {
+ return thisByte < otherByte ? -1 : 1;
+ }
+ thisPos++;
+ otherPos++;
+ compareRemaining--;
+ }
+ return remaining() - otherBuffer.remaining();
+ }
+
+ /**
+ * Returns a duplicated buffer that shares content with this buffer.
+ * <p>
+ * The duplicated buffer's position, limit, capacity and mark are the same
+ * as this buffer. The duplicated buffer's readonly property and byte order
+ * are same as this buffer too.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A duplicated buffer that shares content with this buffer.
+ */
+ public abstract ShortBuffer duplicate();
+
+ /**
+ * Tests whether this short buffer equals to another object.
+ * <p>
+ * If <code>other</code> is not a short buffer, then false is returned.
+ * </p>
+ * <p>
+ * Two short buffers are equals if, and only if, their remaining
+ * <code>short</code>s are exactly the same. Position, limit, capacity
+ * and mark are not considered.
+ * </p>
+ *
+ * @param other
+ * the object to be compared against
+ * @return Whether this short buffer equals to another object.
+ */
+ public boolean equals(Object other) {
+ if (!(other instanceof ShortBuffer)) {
+ return false;
+ }
+ ShortBuffer otherBuffer = (ShortBuffer) other;
+
+ if (remaining() != otherBuffer.remaining()) {
+ return false;
+ }
+
+ int myPosition = position;
+ int otherPosition = otherBuffer.position;
+ boolean equalSoFar = true;
+ while (equalSoFar && (myPosition < limit)) {
+ equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
+ }
+
+ return equalSoFar;
+ }
+
+ /**
+ * Returns the short at the current position and increase the position by 1.
+ *
+ * @return The short at the current position.
+ * @exception BufferUnderflowException
+ * If the position is equal or greater than limit
+ */
+ public abstract short get();
+
+ /**
+ * Reads <code>short</code>s from the current position into the specified
+ * short array and increase the position by the number of <code>short</code>s
+ * read.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>get(dest, 0, dest.length)</code>.
+ * </p>
+ *
+ * @param dest
+ * The destination short array
+ * @return This buffer
+ * @exception BufferUnderflowException
+ * if <code>dest.length</code> is greater than
+ * <code>remaining()</code>
+ */
+ public ShortBuffer get(short[] dest) {
+ return get(dest, 0, dest.length);
+ }
+
+ /**
+ * Reads <code>short</code>s from the current position into the specified
+ * short array, starting from the specified offset, and increase the
+ * position by the number of <code>short</code>s read.
+ *
+ * @param dest
+ * The target short array
+ * @param off
+ * The offset of the short array, must be no less than zero and
+ * no greater than <code>dest.length</code>
+ * @param len
+ * The number of <code>short</code>s to read, must be no less
+ * than zero and no greater than <code>dest.length - off</code>
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception BufferUnderflowException
+ * If <code>len</code> is greater than
+ * <code>remaining()</code>
+ */
+ public ShortBuffer get(short[] dest, int off, int len) {
+ if (off < 0 || off > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if ((len < 0) || off + len > dest.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferUnderflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ dest[i] = get();
+ }
+ return this;
+ }
+
+ /**
+ * Returns a short at the specifed index, and the position is not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than limit
+ * @return A short at the specifed index.
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ */
+ public abstract short get(int index);
+
+ /**
+ * Returns whether this buffer is based on a short array and is read/write.
+ * <p>
+ * If this buffer is readonly, then false is returned.
+ * </p>
+ *
+ * @return Whether this buffer is based on a short array and is read/write.
+ */
+ public final boolean hasArray() {
+ return protectedHasArray();
+ }
+
+ /**
+ * Hash code is calculated from the remaining <code>short</code>s.
+ * <p>
+ * Position, limit, capacity and mark don't affect the hash code.
+ * </p>
+ *
+ * @return The hash code calculated from the remaining <code>short</code>s.
+ */
+ public int hashCode() {
+ int myPosition = position;
+ int hash = 0;
+ while (myPosition < limit) {
+ hash = hash + get(myPosition++);
+ }
+ return hash;
+ }
+
+ /**
+ * Returns true if this buffer is direct.
+ * <p>
+ * A direct buffer will try its best to take advantage of native memory APIs
+ * and it may not stay in java heap, thus not affected by GC.
+ * </p>
+ * <p>
+ * A short buffer is direct, if it is based on a byte buffer and the byte
+ * buffer is direct.
+ * </p>
+ *
+ * @return True if this buffer is direct.
+ */
+ public abstract boolean isDirect();
+
+ /**
+ * Returns the byte order used by this buffer when converting
+ * <code>short</code>s from/to <code>byte</code>s.
+ * <p>
+ * If this buffer is not based on a byte buffer, then always return the
+ * platform's native byte order.
+ * </p>
+ *
+ * @return The byte order used by this buffer when converting
+ * <code>short</code>s from/to <code>byte</code>s.
+ */
+ public abstract ByteOrder order();
+
+ /**
+ * Child class implements this method to realize <code>array()</code>.
+ *
+ * @return see <code>array()</code>
+ */
+ protected abstract short[] protectedArray();
+
+ /**
+ * Child class implements this method to realize <code>arrayOffset()</code>.
+ *
+ * @return see <code>arrayOffset()</code>
+ */
+ protected abstract int protectedArrayOffset();
+
+ /**
+ * Child class implements this method to realize <code>hasArray()</code>.
+ *
+ * @return see <code>hasArray()</code>
+ */
+ protected abstract boolean protectedHasArray();
+
+ /**
+ * Writes the given short to the current position and increase the position
+ * by 1.
+ *
+ * @param s
+ * The short to write
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If position is equal or greater than limit
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ShortBuffer put(short s);
+
+ /**
+ * Writes <code>short</code>s in the given short array to the current
+ * position and increase the position by the number of <code>short</code>s
+ * writtern.
+ * <p>
+ * Calling this method has the same effect as
+ * <code>put(src, 0, src.length)</code>.
+ * </p>
+ *
+ * @param src
+ * The source short array
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>src.length</code>
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public final ShortBuffer put(short[] src) {
+ return put(src, 0, src.length);
+ }
+
+ /**
+ * Writes <code>short</code>s in the given short array, starting from the
+ * specified offset, to the current position and increase the position by
+ * the number of <code>short</code>s writtern.
+ *
+ * @param src
+ * The source short array
+ * @param off
+ * The offset of short array, must be no less than zero and no
+ * greater than <code>src.length</code>
+ * @param len
+ * The number of <code>short</code>s to write, must be no less
+ * than zero and no greater than <code>src.length - off</code>
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>remaining()</code> is less than
+ * <code>len</code>
+ * @exception IndexOutOfBoundsException
+ * If either <code>off</code> or <code>len</code> is
+ * invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public ShortBuffer put(short[] src, int off, int len) {
+ if (off < 0 || off > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < 0 || off + len > src.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len > remaining()) {
+ throw new BufferOverflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ put(src[i]);
+ }
+ return this;
+ }
+
+ /**
+ * Writes all the remaining <code>short</code>s of the <code>src</code>
+ * short buffer to this buffer's current position, and increase both
+ * buffers' position by the number of <code>short</code>s copied.
+ *
+ * @param src
+ * The source short buffer
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If <code>src.remaining()</code> is greater than this
+ * buffer's <code>remaining()</code>
+ * @exception IllegalArgumentException
+ * If <code>src</code> is this buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public ShortBuffer put(ShortBuffer src) {
+ if (src == this) {
+ throw new IllegalArgumentException();
+ }
+ if (src.remaining() > remaining()) {
+ throw new BufferOverflowException();
+ }
+ while (src.hasRemaining()) {
+ put(src.get());
+ }
+ return this;
+ }
+
+ /**
+ * Write a short to the specified index of this buffer and the position is
+ * not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than the limit
+ * @param s
+ * The short to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ShortBuffer put(int index, short s);
+
+ /**
+ * Returns a sliced buffer that shares content with this buffer.
+ * <p>
+ * The sliced buffer's capacity will be this buffer's
+ * <code>remaining()</code>, and its zero position will correspond to
+ * this buffer's current position. The new buffer's position will be 0,
+ * limit will be its capacity, and its mark is unset. The new buffer's
+ * readonly property and byte order are same as this buffer.
+ * </p>
+ * <p>
+ * The new buffer shares content with this buffer, which means either
+ * buffer's change of content will be visible to the other. The two buffer's
+ * position, limit and mark are independent.
+ * </p>
+ *
+ * @return A sliced buffer that shares content with this buffer.
+ */
+ public abstract ShortBuffer slice();
+
+ /**
+ * Returns a string represents the state of this short buffer.
+ *
+ * @return A string represents the state of this short buffer.
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append(getClass().getName());
+ buf.append(", status: capacity="); //$NON-NLS-1$
+ buf.append(capacity());
+ buf.append(" position="); //$NON-NLS-1$
+ buf.append(position());
+ buf.append(" limit="); //$NON-NLS-1$
+ buf.append(limit());
+ return buf.toString();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/AlreadyConnectedException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/AlreadyConnectedException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/AlreadyConnectedException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/AlreadyConnectedException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,33 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio.channels;
+
+
+/**
+ * Thrown when an attempt is made to connect a SocketChannel that is already connected.
+ *
+ */
+public class AlreadyConnectedException extends IllegalStateException {
+
+ static final long serialVersionUID = -7331895245053773357L;
+
+ /**
+ * Default constructor.
+ */
+ public AlreadyConnectedException() {
+ super();
+ }
+}