harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r578091 [3/8] - /harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/
Date Fri, 21 Sep 2007 13:06:01 GMT
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java?rev=578091&r1=578090&r2=578091&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java Fri Sep 21 06:05:59 2007
@@ -36,722 +36,724 @@
  * </p>
  * 
  */
-public abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>,
-		CharSequence, Appendable, Readable {
+public abstract class CharBuffer extends Buffer implements
+        Comparable<CharBuffer>, CharSequence, Appendable, Readable {
 
-	/**
-	 * Creates a char buffer based on a new allocated char array.
-	 * 
-	 * @param capacity
-	 *            The capacity of the new buffer
-	 * @return The created char buffer
-	 * @throws IllegalArgumentException
-	 *             If <code>capacity</code> is less than zero
-	 */
-	public static CharBuffer allocate(int capacity) {
-		if (capacity < 0) {
-			throw new IllegalArgumentException();
-		}
-		return BufferFactory.newCharBuffer(capacity);
-	}
-
-	/**
-	 * Creates a new char buffer by wrapping the given char array.
-	 * <p>
-	 * Calling this method has the same effect as
-	 * <code>wrap(array, 0, array.length)</code>.
-	 * </p>
-	 * 
-	 * @param array
-	 *            The char array which the new buffer will be based on
-	 * @return The created char buffer
-	 */
-	public static CharBuffer wrap(char[] array) {
-		return wrap(array, 0, array.length);
-	}
-
-	/**
-	 * Creates new a char buffer by wrapping the given char 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 char 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 char buffer
-	 * @exception IndexOutOfBoundsException
-	 *                If either <code>start</code> or <code>len</code> is
-	 *                invalid
-	 */
-	public static CharBuffer wrap(char[] array, int start, int len) {
+    /**
+     * Creates a char buffer based on a new allocated char array.
+     * 
+     * @param capacity
+     *            The capacity of the new buffer
+     * @return The created char buffer
+     * @throws IllegalArgumentException
+     *             If <code>capacity</code> is less than zero
+     */
+    public static CharBuffer allocate(int capacity) {
+        if (capacity < 0) {
+            throw new IllegalArgumentException();
+        }
+        return BufferFactory.newCharBuffer(capacity);
+    }
+
+    /**
+     * Creates a new char buffer by wrapping the given char array.
+     * <p>
+     * Calling this method has the same effect as
+     * <code>wrap(array, 0, array.length)</code>.
+     * </p>
+     * 
+     * @param array
+     *            The char array which the new buffer will be based on
+     * @return The created char buffer
+     */
+    public static CharBuffer wrap(char[] array) {
+        return wrap(array, 0, array.length);
+    }
+
+    /**
+     * Creates new a char buffer by wrapping the given char 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 char 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 char buffer
+     * @exception IndexOutOfBoundsException
+     *                If either <code>start</code> or <code>len</code> is
+     *                invalid
+     */
+    public static CharBuffer wrap(char[] array, int start, int len) {
         int length = array.length;
-        if ((start < 0) || (len < 0)
-                || (long) start + (long) len > length) {
+        if ((start < 0) || (len < 0) || (long) start + (long) len > length) {
             throw new IndexOutOfBoundsException();
         }
 
-		CharBuffer buf = BufferFactory.newCharBuffer(array);
-		buf.position = start;
-		buf.limit = start + len;
-
-		return buf;
-	}
-
-	/**
-	 * Creates a new char buffer by wrapping the given char sequence.
-	 * <p>
-	 * Calling this method has the same effect as
-	 * <code>wrap(chseq, 0, chseq.length())</code>.
-	 * </p>
-	 * 
-	 * @param chseq
-	 *            The char sequence which the new buffer will be based on
-	 * @return The created char buffer
-	 */
-	public static CharBuffer wrap(CharSequence chseq) {
-		return BufferFactory.newCharBuffer(chseq);
-	}
-
-	/**
-	 * Creates a new char buffer by wrapping the given char sequence.
-	 * <p>
-	 * The new buffer's position will be <code>start</code>, limit will be
-	 * <code>end</code>, capacity will be the length of the char sequence.
-	 * The new buffer is readonly.
-	 * </p>
-	 * 
-	 * @param chseq
-	 *            The char sequence which the new buffer will be based on
-	 * @param start
-	 *            The start index, must be no less than zero and no greater than
-	 *            <code>chseq.length()</code>
-	 * @param end
-	 *            The end index, must be no less than <code>start</code> and
-	 *            no greater than <code>chseq.length()</code>
-	 * @return The created char buffer
-	 * @exception IndexOutOfBoundsException
-	 *                If either <code>start</code> or <code>end</code> is
-	 *                invalid
-	 */
-	public static CharBuffer wrap(CharSequence chseq, int start, int end) {
-	    if (chseq == null) {
+        CharBuffer buf = BufferFactory.newCharBuffer(array);
+        buf.position = start;
+        buf.limit = start + len;
+
+        return buf;
+    }
+
+    /**
+     * Creates a new char buffer by wrapping the given char sequence.
+     * <p>
+     * Calling this method has the same effect as
+     * <code>wrap(chseq, 0, chseq.length())</code>.
+     * </p>
+     * 
+     * @param chseq
+     *            The char sequence which the new buffer will be based on
+     * @return The created char buffer
+     */
+    public static CharBuffer wrap(CharSequence chseq) {
+        return BufferFactory.newCharBuffer(chseq);
+    }
+
+    /**
+     * Creates a new char buffer by wrapping the given char sequence.
+     * <p>
+     * The new buffer's position will be <code>start</code>, limit will be
+     * <code>end</code>, capacity will be the length of the char sequence.
+     * The new buffer is readonly.
+     * </p>
+     * 
+     * @param chseq
+     *            The char sequence which the new buffer will be based on
+     * @param start
+     *            The start index, must be no less than zero and no greater than
+     *            <code>chseq.length()</code>
+     * @param end
+     *            The end index, must be no less than <code>start</code> and
+     *            no greater than <code>chseq.length()</code>
+     * @return The created char buffer
+     * @exception IndexOutOfBoundsException
+     *                If either <code>start</code> or <code>end</code> is
+     *                invalid
+     */
+    public static CharBuffer wrap(CharSequence chseq, int start, int end) {
+        if (chseq == null) {
             throw new NullPointerException();
         }
-	    if (start < 0 || end < start || end > chseq.length()) {
-	        throw new IndexOutOfBoundsException();
-	    }
-        
-		CharBuffer result = BufferFactory.newCharBuffer(chseq);
-		result.position = start;
-		result.limit = end;
-		return result;
-	}
-
-	/**
-	 * Constructs a <code>CharBuffer</code> with given capacity.
-	 * 
-	 * @param capacity
-	 *            The capacity of the buffer
-	 */
-	CharBuffer(int capacity) {
-		super(capacity);
-	}
-
-	/**
-	 * Returns the char array which this buffer is based on, if there's one.
-	 * 
-	 * @return The char 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 char[] array() {
-		return protectedArray();
-	}
-
-	/**
-	 * Returns the offset of the char 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 char 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 CharBuffer asReadOnlyBuffer();
-
-	/**
-	 * Returns the character located at the specified index in the buffer. The
-	 * index value is referenced from the current buffer position.
-	 * 
-	 * @param index
-	 *            The index referenced from the current buffer position. It must
-	 *            not be less than zero but less than the value obtained from a
-	 *            call to <code>remaining()</code>
-	 * @return the character located at the specified index (referenced from the
-	 *         current position) in the buffer.
-	 * @exception IndexOutOfBoundsException
-	 *                If the index is invalid
-	 */
-	public final char charAt(int index) {
-		if (index < 0 || index >= remaining()) {
-			throw new IndexOutOfBoundsException();
-		}
-		return get(position + index);
-	}
-
-	/**
-	 * Compacts this char buffer.
-	 * <p>
-	 * The remaining <code>char</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 CharBuffer compact();
-
-	/**
-	 * Compare the remaining <code>char</code>s of this buffer to another
-	 * char buffer's remaining <code>char</code>s.
-	 * 
-	 * @param otherBuffer
-	 *            Another char 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 char buffer
-	 */
-	public int compareTo(CharBuffer otherBuffer) {
-		int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
-				: otherBuffer.remaining();
-		int thisPos = position;
-		int otherPos = otherBuffer.position;
-		char 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 CharBuffer duplicate();
-
-	/**
-	 * Tests whether this char buffer equals to another object.
-	 * <p>
-	 * If <code>other</code> is not a char buffer, then false is returned.
-	 * </p>
-	 * <p>
-	 * Two char buffers are equals if, and only if, their remaining
-	 * <code>char</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 char buffer equals to another object.
-	 */
-	public boolean equals(Object other) {
-		if (!(other instanceof CharBuffer)) {
-			return false;
-		}
-		CharBuffer otherBuffer = (CharBuffer) 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 char at the current position and increase the position by 1.
-	 * 
-	 * @return The char at the current position.
-	 * @exception BufferUnderflowException
-	 *                If the position is equal or greater than limit
-	 */
-	public abstract char get();
-
-	/**
-	 * Reads <code>char</code>s from the current position into the specified
-	 * char array and increase the position by the number of <code>char</code>s
-	 * read.
-	 * <p>
-	 * Calling this method has the same effect as
-	 * <code>get(dest, 0, dest.length)</code>.
-	 * </p>
-	 * 
-	 * @param dest
-	 *            The destination char array
-	 * @return This buffer
-	 * @exception BufferUnderflowException
-	 *                if <code>dest.length</code> is greater than
-	 *                <code>remaining()</code>
-	 */
-	public CharBuffer get(char[] dest) {
-		return get(dest, 0, dest.length);
-	}
-
-	/**
-	 * Reads <code>char</code>s from the current position into the specified
-	 * char array, starting from the specified offset, and increase the position
-	 * by the number of <code>char</code>s read.
-	 * 
-	 * @param dest
-	 *            The target char array
-	 * @param off
-	 *            The offset of the char array, must be no less than zero and no
-	 *            greater than <code>dest.length</code>
-	 * @param len
-	 *            The number of <code>char</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 CharBuffer get(char[] dest, int off, int len) {
+        if (start < 0 || end < start || end > chseq.length()) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        CharBuffer result = BufferFactory.newCharBuffer(chseq);
+        result.position = start;
+        result.limit = end;
+        return result;
+    }
+
+    /**
+     * Constructs a <code>CharBuffer</code> with given capacity.
+     * 
+     * @param capacity
+     *            The capacity of the buffer
+     */
+    CharBuffer(int capacity) {
+        super(capacity);
+    }
+
+    /**
+     * Returns the char array which this buffer is based on, if there's one.
+     * 
+     * @return The char 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 char[] array() {
+        return protectedArray();
+    }
+
+    /**
+     * Returns the offset of the char 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 char 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 CharBuffer asReadOnlyBuffer();
+
+    /**
+     * Returns the character located at the specified index in the buffer. The
+     * index value is referenced from the current buffer position.
+     * 
+     * @param index
+     *            The index referenced from the current buffer position. It must
+     *            not be less than zero but less than the value obtained from a
+     *            call to <code>remaining()</code>
+     * @return the character located at the specified index (referenced from the
+     *         current position) in the buffer.
+     * @exception IndexOutOfBoundsException
+     *                If the index is invalid
+     */
+    public final char charAt(int index) {
+        if (index < 0 || index >= remaining()) {
+            throw new IndexOutOfBoundsException();
+        }
+        return get(position + index);
+    }
+
+    /**
+     * Compacts this char buffer.
+     * <p>
+     * The remaining <code>char</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 CharBuffer compact();
+
+    /**
+     * Compare the remaining <code>char</code>s of this buffer to another
+     * char buffer's remaining <code>char</code>s.
+     * 
+     * @param otherBuffer
+     *            Another char 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 char buffer
+     */
+    public int compareTo(CharBuffer otherBuffer) {
+        int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+                : otherBuffer.remaining();
+        int thisPos = position;
+        int otherPos = otherBuffer.position;
+        char 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 CharBuffer duplicate();
+
+    /**
+     * Tests whether this char buffer equals to another object.
+     * <p>
+     * If <code>other</code> is not a char buffer, then false is returned.
+     * </p>
+     * <p>
+     * Two char buffers are equals if, and only if, their remaining
+     * <code>char</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 char buffer equals to another object.
+     */
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof CharBuffer)) {
+            return false;
+        }
+        CharBuffer otherBuffer = (CharBuffer) 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 char at the current position and increase the position by 1.
+     * 
+     * @return The char at the current position.
+     * @exception BufferUnderflowException
+     *                If the position is equal or greater than limit
+     */
+    public abstract char get();
+
+    /**
+     * Reads <code>char</code>s from the current position into the specified
+     * char array and increase the position by the number of <code>char</code>s
+     * read.
+     * <p>
+     * Calling this method has the same effect as
+     * <code>get(dest, 0, dest.length)</code>.
+     * </p>
+     * 
+     * @param dest
+     *            The destination char array
+     * @return This buffer
+     * @exception BufferUnderflowException
+     *                if <code>dest.length</code> is greater than
+     *                <code>remaining()</code>
+     */
+    public CharBuffer get(char[] dest) {
+        return get(dest, 0, dest.length);
+    }
+
+    /**
+     * Reads <code>char</code>s from the current position into the specified
+     * char array, starting from the specified offset, and increase the position
+     * by the number of <code>char</code>s read.
+     * 
+     * @param dest
+     *            The target char array
+     * @param off
+     *            The offset of the char array, must be no less than zero and no
+     *            greater than <code>dest.length</code>
+     * @param len
+     *            The number of <code>char</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 CharBuffer get(char[] dest, int off, int len) {
         int length = dest.length;
-        if ((off < 0 ) || (len < 0) || (long)off + (long)len > length) {
+        if ((off < 0) || (len < 0) || (long) off + (long) len > 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 char at the specified index, and the position is not changed.
-	 * 
-	 * @param index
-	 *            The index, must be no less than zero and less than limit
-	 * @return A char at the specified index.
-	 * @exception IndexOutOfBoundsException
-	 *                If index is invalid
-	 */
-	public abstract char get(int index);
-
-	/**
-	 * Returns whether this buffer is based on a char array and is read/write.
-	 * <p>
-	 * If this buffer is readonly, then false is returned.
-	 * </p>
-	 * 
-	 * @return Whether this buffer is based on a char array and is read/write.
-	 */
-	public final boolean hasArray() {
-		return protectedHasArray();
-	}
-
-	/**
-	 * Hash code is calculated from the remaining <code>char</code>s.
-	 * <p>
-	 * Position, limit, capacity and mark don't affect the hash code.
-	 * </p>
-	 * 
-	 * @return The hash code calculated from the remaining <code>char</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 char 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 number of remaining <code>char</code>s.
-	 * 
-	 * @return The number of remaining <code>char</code>s.
-	 */
-	public final int length() {
-		return remaining();
-	}
-
-	/**
-	 * Returns the byte order used by this buffer when converting
-	 * <code>char</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>char</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>
-	 */
-	abstract char[] protectedArray();
-
-	/**
-	 * Child class implements this method to realize <code>arrayOffset()</code>.
-	 * 
-	 * @return see <code>arrayOffset()</code>
-	 */
-	abstract int protectedArrayOffset();
-
-	/**
-	 * Child class implements this method to realize <code>hasArray()</code>.
-	 * 
-	 * @return see <code>hasArray()</code>
-	 */
-	abstract boolean protectedHasArray();
-
-	/**
-	 * Writes the given char to the current position and increase the position
-	 * by 1.
-	 * 
-	 * @param c
-	 *            The char 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 CharBuffer put(char c);
-
-	/**
-	 * Writes <code>char</code>s in the given char array to the current
-	 * position and increase the position by the number of <code>char</code>s
-	 * written.
-	 * <p>
-	 * Calling this method has the same effect as
-	 * <code>put(src, 0, src.length)</code>.
-	 * </p>
-	 * 
-	 * @param src
-	 *            The source char 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 CharBuffer put(char[] src) {
-		return put(src, 0, src.length);
-	}
-
-	/**
-	 * Writes <code>char</code>s in the given char array, starting from the
-	 * specified offset, to the current position and increase the position by
-	 * the number of <code>char</code>s written.
-	 * 
-	 * @param src
-	 *            The source char array
-	 * @param off
-	 *            The offset of char array, must be no less than zero and no
-	 *            greater than <code>src.length</code>
-	 * @param len
-	 *            The number of <code>char</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 CharBuffer put(char[] src, int off, int len) {
+
+        if (len > remaining()) {
+            throw new BufferUnderflowException();
+        }
+        for (int i = off; i < off + len; i++) {
+            dest[i] = get();
+        }
+        return this;
+    }
+
+    /**
+     * Returns a char at the specified index, and the position is not changed.
+     * 
+     * @param index
+     *            The index, must be no less than zero and less than limit
+     * @return A char at the specified index.
+     * @exception IndexOutOfBoundsException
+     *                If index is invalid
+     */
+    public abstract char get(int index);
+
+    /**
+     * Returns whether this buffer is based on a char array and is read/write.
+     * <p>
+     * If this buffer is readonly, then false is returned.
+     * </p>
+     * 
+     * @return Whether this buffer is based on a char array and is read/write.
+     */
+    public final boolean hasArray() {
+        return protectedHasArray();
+    }
+
+    /**
+     * Hash code is calculated from the remaining <code>char</code>s.
+     * <p>
+     * Position, limit, capacity and mark don't affect the hash code.
+     * </p>
+     * 
+     * @return The hash code calculated from the remaining <code>char</code>s.
+     */
+    @Override
+    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 char 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 number of remaining <code>char</code>s.
+     * 
+     * @return The number of remaining <code>char</code>s.
+     */
+    public final int length() {
+        return remaining();
+    }
+
+    /**
+     * Returns the byte order used by this buffer when converting
+     * <code>char</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>char</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>
+     */
+    abstract char[] protectedArray();
+
+    /**
+     * Child class implements this method to realize <code>arrayOffset()</code>.
+     * 
+     * @return see <code>arrayOffset()</code>
+     */
+    abstract int protectedArrayOffset();
+
+    /**
+     * Child class implements this method to realize <code>hasArray()</code>.
+     * 
+     * @return see <code>hasArray()</code>
+     */
+    abstract boolean protectedHasArray();
+
+    /**
+     * Writes the given char to the current position and increase the position
+     * by 1.
+     * 
+     * @param c
+     *            The char 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 CharBuffer put(char c);
+
+    /**
+     * Writes <code>char</code>s in the given char array to the current
+     * position and increase the position by the number of <code>char</code>s
+     * written.
+     * <p>
+     * Calling this method has the same effect as
+     * <code>put(src, 0, src.length)</code>.
+     * </p>
+     * 
+     * @param src
+     *            The source char 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 CharBuffer put(char[] src) {
+        return put(src, 0, src.length);
+    }
+
+    /**
+     * Writes <code>char</code>s in the given char array, starting from the
+     * specified offset, to the current position and increase the position by
+     * the number of <code>char</code>s written.
+     * 
+     * @param src
+     *            The source char array
+     * @param off
+     *            The offset of char array, must be no less than zero and no
+     *            greater than <code>src.length</code>
+     * @param len
+     *            The number of <code>char</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 CharBuffer put(char[] src, int off, int len) {
         int length = src.length;
-        if ((off < 0 ) || (len < 0) || (long)off + (long)len > length) {
+        if ((off < 0) || (len < 0) || (long) off + (long) len > 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>char</code>s of the <code>src</code>
-	 * char buffer to this buffer's current position, and increase both buffers'
-	 * position by the number of <code>char</code>s copied.
-	 * 
-	 * @param src
-	 *            The source char 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 CharBuffer put(CharBuffer src) {
-		if (src == this) {
-			throw new IllegalArgumentException();
-		}
-		if (src.remaining() > remaining()) {
-			throw new BufferOverflowException();
-		}
-        
+
+        if (len > remaining()) {
+            throw new BufferOverflowException();
+        }
+        for (int i = off; i < off + len; i++) {
+            put(src[i]);
+        }
+        return this;
+    }
+
+    /**
+     * Writes all the remaining <code>char</code>s of the <code>src</code>
+     * char buffer to this buffer's current position, and increase both buffers'
+     * position by the number of <code>char</code>s copied.
+     * 
+     * @param src
+     *            The source char 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 CharBuffer put(CharBuffer src) {
+        if (src == this) {
+            throw new IllegalArgumentException();
+        }
+        if (src.remaining() > remaining()) {
+            throw new BufferOverflowException();
+        }
+
         char[] contents = new char[src.remaining()];
         src.get(contents);
         put(contents);
-		return this;
-	}
+        return this;
+    }
+
+    /**
+     * Write a char 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 c
+     *            The char 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 CharBuffer put(int index, char c);
+
+    /**
+     * Write all <code>char</code>s of the give string to the current
+     * position of this buffer, and increase the position by the length of
+     * string.
+     * <p>
+     * Calling this method has the same effect as
+     * <code>put(str, 0, str.length())</code>.
+     * </p>
+     * 
+     * @param str
+     *            The string to write
+     * @return This buffer
+     * @exception BufferOverflowException
+     *                If <code>remaining()</code> is less than the length of
+     *                string
+     * @exception ReadOnlyBufferException
+     *                If no changes may be made to the contents of this buffer
+     */
+    public final CharBuffer put(String str) {
+        return put(str, 0, str.length());
+    }
 
-	/**
-	 * Write a char 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 c
-	 *            The char 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 CharBuffer put(int index, char c);
-
-	/**
-	 * Write all <code>char</code>s of the give string to the current
-	 * position of this buffer, and increase the position by the length of
-	 * string.
-	 * <p>
-	 * Calling this method has the same effect as
-	 * <code>put(str, 0, str.length())</code>.
-	 * </p>
-	 * 
-	 * @param str
-	 *            The string to write
-	 * @return This buffer
-	 * @exception BufferOverflowException
-	 *                If <code>remaining()</code> is less than the length of
-	 *                string
-	 * @exception ReadOnlyBufferException
-	 *                If no changes may be made to the contents of this buffer
-	 */
-	public final CharBuffer put(String str) {
-		return put(str, 0, str.length());
-	}
-
-	/**
-	 * Write <code>char</code>s of the given string to the current position
-	 * of this buffer, and increase the position by the number of
-	 * <code>char</code>s written.
-	 * 
-	 * @param str
-	 *            The string to write
-	 * @param start
-	 *            The first char to write, must be no less than zero and no
-	 *            greater than <code>str.length()</code>
-	 * @param end
-	 *            The last char to write (excluding), must be less than
-	 *            <code>start</code> and no greater than
-	 *            <code>str.length()</code>
-	 * @return This buffer
-	 * @exception BufferOverflowException
-	 *                If <code>remaining</code> is less than
-	 *                <code>end - start</code>
-	 * @exception IndexOutOfBoundsException
-	 *                If either <code>start</code> or <code>end</code> is
-	 *                invalid
-	 * @exception ReadOnlyBufferException
-	 *                If no changes may be made to the contents of this buffer
-	 */
-	public CharBuffer put(String str, int start, int end) {
+    /**
+     * Write <code>char</code>s of the given string to the current position
+     * of this buffer, and increase the position by the number of
+     * <code>char</code>s written.
+     * 
+     * @param str
+     *            The string to write
+     * @param start
+     *            The first char to write, must be no less than zero and no
+     *            greater than <code>str.length()</code>
+     * @param end
+     *            The last char to write (excluding), must be less than
+     *            <code>start</code> and no greater than
+     *            <code>str.length()</code>
+     * @return This buffer
+     * @exception BufferOverflowException
+     *                If <code>remaining</code> is less than
+     *                <code>end - start</code>
+     * @exception IndexOutOfBoundsException
+     *                If either <code>start</code> or <code>end</code> is
+     *                invalid
+     * @exception ReadOnlyBufferException
+     *                If no changes may be made to the contents of this buffer
+     */
+    public CharBuffer put(String str, int start, int end) {
         int length = str.length();
         if (start < 0 || end < start || end > length) {
             throw new IndexOutOfBoundsException();
         }
-        
-		if (end - start > remaining()) {
-			throw new BufferOverflowException();
-		}
-		for (int i = start; i < end; i++) {
-			put(str.charAt(i));
-		}
-		return this;
-	}
-
-	/**
-	 * 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 CharBuffer slice();
-
-	/**
-	 * Returns a new char buffer represents a sub-sequence of this buffer's
-	 * current remaining content.
-	 * <p>
-	 * The new buffer's position will be <code>position() + start</code>,
-	 * limit will be <code>position() + end</code>, capacity will be same as
-	 * this buffer. 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>
-	 * 
-	 * @param start
-	 *            The start index of the sub-sequence, referenced from the
-	 *            current buffer position. Must not be less than zero and not
-	 *            greater than the value obtained from a call to
-	 *            <code>remaining()</code>.
-	 * @param end
-	 *            The end index of the sub-sequence, referenced from the current
-	 *            buffer position. Must not be less than <code>start</code>
-	 *            and not be greater than the value obtained from a call to
-	 *            <code>remaining()</code>
-	 * @return A new char buffer represents a sub-sequence of this buffer's
-	 *         current remaining content.
-	 * @exception IndexOutOfBoundsException
-	 *                If either <code>start</code> or <code>end</code> is
-	 *                invalid
-	 */
-	public abstract CharSequence subSequence(int start, int end);
-
-	/**
-	 * Returns a string represents the current remaining <code>char</code>s
-	 * of this buffer.
-	 * 
-	 * @return A string represents the current remaining <code>char</code>s
-	 *         of this buffer.
-	 */
-	public String toString() {
-		StringBuffer strbuf = new StringBuffer();
-		for (int i = position; i < limit; i++) {
-			strbuf.append(get(i));
-		}
-		return strbuf.toString();
-	}
-    
+
+        if (end - start > remaining()) {
+            throw new BufferOverflowException();
+        }
+        for (int i = start; i < end; i++) {
+            put(str.charAt(i));
+        }
+        return this;
+    }
+
+    /**
+     * 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 CharBuffer slice();
+
+    /**
+     * Returns a new char buffer represents a sub-sequence of this buffer's
+     * current remaining content.
+     * <p>
+     * The new buffer's position will be <code>position() + start</code>,
+     * limit will be <code>position() + end</code>, capacity will be same as
+     * this buffer. 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>
+     * 
+     * @param start
+     *            The start index of the sub-sequence, referenced from the
+     *            current buffer position. Must not be less than zero and not
+     *            greater than the value obtained from a call to
+     *            <code>remaining()</code>.
+     * @param end
+     *            The end index of the sub-sequence, referenced from the current
+     *            buffer position. Must not be less than <code>start</code>
+     *            and not be greater than the value obtained from a call to
+     *            <code>remaining()</code>
+     * @return A new char buffer represents a sub-sequence of this buffer's
+     *         current remaining content.
+     * @exception IndexOutOfBoundsException
+     *                If either <code>start</code> or <code>end</code> is
+     *                invalid
+     */
+    public abstract CharSequence subSequence(int start, int end);
+
+    /**
+     * Returns a string represents the current remaining <code>char</code>s
+     * of this buffer.
+     * 
+     * @return A string represents the current remaining <code>char</code>s
+     *         of this buffer.
+     */
+    @Override
+    public String toString() {
+        StringBuffer strbuf = new StringBuffer();
+        for (int i = position; i < limit; i++) {
+            strbuf.append(get(i));
+        }
+        return strbuf.toString();
+    }
+
     /**
      * @see Appendable#append(char)
      */
-    public CharBuffer append(char c){
+    public CharBuffer append(char c) {
         return put(c);
     }
 
     /**
      * @see Appendable#append(CharSequence)
      */
-    public CharBuffer append(CharSequence csq){
+    public CharBuffer append(CharSequence csq) {
         if (csq != null) {
             return put(csq.toString());
         }
@@ -761,7 +763,7 @@
     /**
      * @see Appendable#append(CharSequence, int, int)
      */
-    public CharBuffer append(CharSequence csq, int start, int end){
+    public CharBuffer append(CharSequence csq, int start, int end) {
         if (csq == null) {
             csq = "null"; //$NON-NLS-1$
         }
@@ -776,16 +778,16 @@
      * @see Readable#read(CharBuffer)
      */
     public int read(CharBuffer target) throws IOException {
-        if(target == this){
+        if (target == this) {
             throw new IllegalArgumentException();
         }
         if (remaining() == 0) {
-            return target.remaining()==0?0:-1;
+            return target.remaining() == 0 ? 0 : -1;
         }
         int result = Math.min(target.remaining(), remaining());
         char[] chars = new char[result];
         get(chars);
         target.put(chars);
         return result;
-    }    
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java?rev=578091&r1=578090&r2=578091&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java Fri Sep 21 06:05:59 2007
@@ -29,50 +29,56 @@
  */
 final class CharSequenceAdapter extends CharBuffer {
 
-	static CharSequenceAdapter copy(CharSequenceAdapter other) {
-		CharSequenceAdapter buf = new CharSequenceAdapter(other.sequence);
-		buf.limit = other.limit;
-		buf.position = other.position;
-		buf.mark = other.mark;
-		return buf;
-	}
-
-	final CharSequence sequence;
-
-	CharSequenceAdapter(CharSequence chseq) {
-		super(chseq.length());
-		sequence = chseq;
-	}
-
-	public CharBuffer asReadOnlyBuffer() {
-		return duplicate();
-	}
-
-	public CharBuffer compact() {
-		throw new ReadOnlyBufferException();
-	}
-
-	public CharBuffer duplicate() {
-		return copy(this);
-	}
-
-	public char get() {
-		if (position == limit) {
-			throw new BufferUnderflowException();
-		}
-		return sequence.charAt(position++);
-	}
-
-	public char get(int index) {
-		if (index < 0 || index >= limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return sequence.charAt(index);
-	}
+    static CharSequenceAdapter copy(CharSequenceAdapter other) {
+        CharSequenceAdapter buf = new CharSequenceAdapter(other.sequence);
+        buf.limit = other.limit;
+        buf.position = other.position;
+        buf.mark = other.mark;
+        return buf;
+    }
+
+    final CharSequence sequence;
+
+    CharSequenceAdapter(CharSequence chseq) {
+        super(chseq.length());
+        sequence = chseq;
+    }
+
+    @Override
+    public CharBuffer asReadOnlyBuffer() {
+        return duplicate();
+    }
+
+    @Override
+    public CharBuffer compact() {
+        throw new ReadOnlyBufferException();
+    }
+
+    @Override
+    public CharBuffer duplicate() {
+        return copy(this);
+    }
 
+    @Override
+    public char get() {
+        if (position == limit) {
+            throw new BufferUnderflowException();
+        }
+        return sequence.charAt(position++);
+    }
+
+    @Override
+    public char get(int index) {
+        if (index < 0 || index >= limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return sequence.charAt(index);
+    }
+
+    @Override
     public final CharBuffer get(char[] dest, int off, int len) {
         int length = dest.length;
-        if ((off < 0 ) || (len < 0) || (long)off + (long)len > length) {
+        if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
             throw new IndexOutOfBoundsException();
         }
         if (len > remaining()) {
@@ -84,69 +90,82 @@
         return this;
     }
 
-	public boolean isDirect() {
-		return false;
-	}
-
-	public boolean isReadOnly() {
-		return true;
-	}
-
-	public ByteOrder order() {
-		return ByteOrder.nativeOrder();
-	}
-
-	protected char[] protectedArray() {
-		throw new UnsupportedOperationException();
-	}
-
-	protected int protectedArrayOffset() {
-		throw new UnsupportedOperationException();
-	}
-
-	protected boolean protectedHasArray() {
-		return false;
-	}
-
-	public CharBuffer put(char c) {
-		throw new ReadOnlyBufferException();
-	}
-
-	public CharBuffer put(int index, char c) {
-		throw new ReadOnlyBufferException();
-	}
+    @Override
+    public boolean isDirect() {
+        return false;
+    }
+
+    @Override
+    public boolean isReadOnly() {
+        return true;
+    }
+
+    @Override
+    public ByteOrder order() {
+        return ByteOrder.nativeOrder();
+    }
 
+    @Override
+    protected char[] protectedArray() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected int protectedArrayOffset() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected boolean protectedHasArray() {
+        return false;
+    }
+
+    @Override
+    public CharBuffer put(char c) {
+        throw new ReadOnlyBufferException();
+    }
+
+    @Override
+    public CharBuffer put(int index, char c) {
+        throw new ReadOnlyBufferException();
+    }
+
+    @Override
     public final CharBuffer put(char[] src, int off, int len) {
-        if ((off < 0 ) || (len < 0) || (long)off + (long)len > src.length) {
+        if ((off < 0) || (len < 0) || (long) off + (long) len > src.length) {
             throw new IndexOutOfBoundsException();
         }
-        
+
         if (len > remaining()) {
             throw new BufferOverflowException();
         }
-        
+
         throw new ReadOnlyBufferException();
     }
 
+    @Override
     public CharBuffer put(String src, int start, int end) {
-        if ((start < 0 ) || (end < 0) || (long)start + (long)end > src.length()) {
+        if ((start < 0) || (end < 0)
+                || (long) start + (long) end > src.length()) {
             throw new IndexOutOfBoundsException();
         }
         throw new ReadOnlyBufferException();
-    }  
+    }
 
-	public CharBuffer slice() {
-		return new CharSequenceAdapter(sequence.subSequence(position, limit));
-	}
+    @Override
+    public CharBuffer slice() {
+        return new CharSequenceAdapter(sequence.subSequence(position, limit));
+    }
 
-	public CharSequence subSequence(int start, int end) {
+    @Override
+    public CharSequence subSequence(int start, int end) {
         if (end < start || start < 0 || end > remaining()) {
             throw new IndexOutOfBoundsException();
         }
-        
-		CharSequenceAdapter result = copy(this);
-		result.position = position + start;
-		result.limit = position + end;
-		return result;
-	}
+
+        CharSequenceAdapter result = copy(this);
+        result.position = position + start;
+        result.limit = position + end;
+        return result;
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharToByteBufferAdapter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharToByteBufferAdapter.java?rev=578091&r1=578090&r2=578091&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharToByteBufferAdapter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharToByteBufferAdapter.java Fri Sep 21 06:05:59 2007
@@ -19,7 +19,6 @@
 import org.apache.harmony.nio.internal.DirectBuffer;
 import org.apache.harmony.luni.platform.PlatformAddress;
 
-
 /**
  * This class wraps a byte buffer to be a char buffer.
  * <p>
@@ -35,172 +34,183 @@
  */
 final class CharToByteBufferAdapter extends CharBuffer implements DirectBuffer {
 
-	static CharBuffer wrap(ByteBuffer byteBuffer) {
-		return new CharToByteBufferAdapter(byteBuffer.slice());
-	}
-
-	private final ByteBuffer byteBuffer;
-
-	CharToByteBufferAdapter(ByteBuffer byteBuffer) {
-		super((byteBuffer.capacity() >> 1));
-		this.byteBuffer = byteBuffer;
-		this.byteBuffer.clear();
-	}
-        
-        public int getByteCapacity() {
-            if (byteBuffer instanceof DirectBuffer) {
-                return ((DirectBuffer)byteBuffer).getByteCapacity();
-            } else {
-                assert false : byteBuffer;
-                return -1;
-            }            
-        }
-        
-        public PlatformAddress getEffectiveAddress() {
-            if (byteBuffer instanceof DirectBuffer) {
-                return ((DirectBuffer)byteBuffer).getEffectiveAddress();
-            } else {
-                assert false : byteBuffer;
-                return null;
-            }
-        }
-
-        public PlatformAddress getBaseAddress() {
-            if (byteBuffer instanceof DirectBuffer) {
-                return ((DirectBuffer)byteBuffer).getBaseAddress();
-            } else {
-                assert false : byteBuffer;
-                return null;
-            }
-        }
-            
-        public boolean isAddressValid() {
-            if (byteBuffer instanceof DirectBuffer) {
-                return ((DirectBuffer)byteBuffer).isAddressValid();
-            } else {
-                assert false : byteBuffer;
-                return false;
-            }
-        }
-
-        public void addressValidityCheck() {
-            if (byteBuffer instanceof DirectBuffer) {
-                ((DirectBuffer)byteBuffer).addressValidityCheck();
-            } else {
-                assert false : byteBuffer;
-            }
-        }
-            
-        public void free() {
-            if (byteBuffer instanceof DirectBuffer) {
-                ((DirectBuffer)byteBuffer).free();
-            } else {
-                assert false : byteBuffer;
-            }   
-        }
-
-	public CharBuffer asReadOnlyBuffer() {
-		CharToByteBufferAdapter buf = new CharToByteBufferAdapter(byteBuffer
-				.asReadOnlyBuffer());
-		buf.limit = limit;
-		buf.position = position;
-		buf.mark = mark;
-		return buf;
-	}
-
-	public CharBuffer compact() {
-		if (byteBuffer.isReadOnly()) {
-			throw new ReadOnlyBufferException();
-		}
-		byteBuffer.limit(limit << 1);
-		byteBuffer.position(position << 1);
-		byteBuffer.compact();
-		byteBuffer.clear();
-		position = limit - position;
-		limit = capacity;
-		mark = UNSET_MARK;
-		return this;
-	}
-
-	public CharBuffer duplicate() {
-		CharToByteBufferAdapter buf = new CharToByteBufferAdapter(byteBuffer
-				.duplicate());
-		buf.limit = limit;
-		buf.position = position;
-		buf.mark = mark;
-		return buf;
-	}
-
-	public char get() {
-		if (position == limit) {
-			throw new BufferUnderflowException();
-		}
-		return byteBuffer.getChar(position++ << 1);
-	}
-
-	public char get(int index) {
-		if (index < 0 || index >= limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return byteBuffer.getChar(index << 1);
-	}
-
-	public boolean isDirect() {
-		return byteBuffer.isDirect();
-	}
-
-	public boolean isReadOnly() {
-		return byteBuffer.isReadOnly();
-	}
-
-	public ByteOrder order() {
-		return byteBuffer.order();
-	}
-
-	protected char[] protectedArray() {
-		throw new UnsupportedOperationException();
-	}
-
-	protected int protectedArrayOffset() {
-		throw new UnsupportedOperationException();
-	}
-
-	protected boolean protectedHasArray() {
-		return false;
-	}
-
-	public CharBuffer put(char c) {
-		if (position == limit) {
-			throw new BufferOverflowException();
-		}
-		byteBuffer.putChar(position++ << 1, c);
-		return this;
-	}
-
-	public CharBuffer put(int index, char c) {
-		if (index < 0 || index >= limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		byteBuffer.putChar(index << 1, c);
-		return this;
-	}
-
-	public CharBuffer slice() {
-		byteBuffer.limit(limit << 1);
-		byteBuffer.position(position << 1);
-		CharBuffer result = new CharToByteBufferAdapter(byteBuffer.slice());
-		byteBuffer.clear();
-		return result;
-	}
+    static CharBuffer wrap(ByteBuffer byteBuffer) {
+        return new CharToByteBufferAdapter(byteBuffer.slice());
+    }
+
+    private final ByteBuffer byteBuffer;
+
+    CharToByteBufferAdapter(ByteBuffer byteBuffer) {
+        super((byteBuffer.capacity() >> 1));
+        this.byteBuffer = byteBuffer;
+        this.byteBuffer.clear();
+    }
+
+    public int getByteCapacity() {
+        if (byteBuffer instanceof DirectBuffer) {
+            return ((DirectBuffer) byteBuffer).getByteCapacity();
+        }
+        assert false : byteBuffer;
+        return -1;
+    }
+
+    public PlatformAddress getEffectiveAddress() {
+        if (byteBuffer instanceof DirectBuffer) {
+            return ((DirectBuffer) byteBuffer).getEffectiveAddress();
+        }
+        assert false : byteBuffer;
+        return null;
+    }
+
+    public PlatformAddress getBaseAddress() {
+        if (byteBuffer instanceof DirectBuffer) {
+            return ((DirectBuffer) byteBuffer).getBaseAddress();
+        }
+        assert false : byteBuffer;
+        return null;
+    }
+
+    public boolean isAddressValid() {
+        if (byteBuffer instanceof DirectBuffer) {
+            return ((DirectBuffer) byteBuffer).isAddressValid();
+        }
+        assert false : byteBuffer;
+        return false;
+    }
+
+    public void addressValidityCheck() {
+        if (byteBuffer instanceof DirectBuffer) {
+            ((DirectBuffer) byteBuffer).addressValidityCheck();
+        } else {
+            assert false : byteBuffer;
+        }
+    }
+
+    public void free() {
+        if (byteBuffer instanceof DirectBuffer) {
+            ((DirectBuffer) byteBuffer).free();
+        } else {
+            assert false : byteBuffer;
+        }
+    }
+
+    @Override
+    public CharBuffer asReadOnlyBuffer() {
+        CharToByteBufferAdapter buf = new CharToByteBufferAdapter(byteBuffer
+                .asReadOnlyBuffer());
+        buf.limit = limit;
+        buf.position = position;
+        buf.mark = mark;
+        return buf;
+    }
+
+    @Override
+    public CharBuffer compact() {
+        if (byteBuffer.isReadOnly()) {
+            throw new ReadOnlyBufferException();
+        }
+        byteBuffer.limit(limit << 1);
+        byteBuffer.position(position << 1);
+        byteBuffer.compact();
+        byteBuffer.clear();
+        position = limit - position;
+        limit = capacity;
+        mark = UNSET_MARK;
+        return this;
+    }
+
+    @Override
+    public CharBuffer duplicate() {
+        CharToByteBufferAdapter buf = new CharToByteBufferAdapter(byteBuffer
+                .duplicate());
+        buf.limit = limit;
+        buf.position = position;
+        buf.mark = mark;
+        return buf;
+    }
+
+    @Override
+    public char get() {
+        if (position == limit) {
+            throw new BufferUnderflowException();
+        }
+        return byteBuffer.getChar(position++ << 1);
+    }
+
+    @Override
+    public char get(int index) {
+        if (index < 0 || index >= limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return byteBuffer.getChar(index << 1);
+    }
 
-	public CharSequence subSequence(int start, int end) {
+    @Override
+    public boolean isDirect() {
+        return byteBuffer.isDirect();
+    }
+
+    @Override
+    public boolean isReadOnly() {
+        return byteBuffer.isReadOnly();
+    }
+
+    @Override
+    public ByteOrder order() {
+        return byteBuffer.order();
+    }
+
+    @Override
+    protected char[] protectedArray() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected int protectedArrayOffset() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected boolean protectedHasArray() {
+        return false;
+    }
+
+    @Override
+    public CharBuffer put(char c) {
+        if (position == limit) {
+            throw new BufferOverflowException();
+        }
+        byteBuffer.putChar(position++ << 1, c);
+        return this;
+    }
+
+    @Override
+    public CharBuffer put(int index, char c) {
+        if (index < 0 || index >= limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        byteBuffer.putChar(index << 1, c);
+        return this;
+    }
+
+    @Override
+    public CharBuffer slice() {
+        byteBuffer.limit(limit << 1);
+        byteBuffer.position(position << 1);
+        CharBuffer result = new CharToByteBufferAdapter(byteBuffer.slice());
+        byteBuffer.clear();
+        return result;
+    }
+
+    @Override
+    public CharSequence subSequence(int start, int end) {
         if (start < 0 || end < start || end > remaining()) {
             throw new IndexOutOfBoundsException();
         }
-        
-		CharBuffer result = duplicate();
-		result.limit(position + end);
-		result.position(position + start);
-		return result;
-	}
+
+        CharBuffer result = duplicate();
+        result.limit(position + end);
+        result.position(position + start);
+        return result;
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffer.java?rev=578091&r1=578090&r2=578091&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffer.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffer.java Fri Sep 21 06:05:59 2007
@@ -22,7 +22,6 @@
 import org.apache.harmony.nio.internal.DirectBuffer;
 import org.apache.harmony.nio.internal.nls.Messages;
 
-
 /**
  * DirectByteBuffer, ReadWriteDirectByteBuffer and ReadOnlyDirectByteBuffer
  * compose the implementation of platform memory based byte buffers.
@@ -37,239 +36,258 @@
  */
 abstract class DirectByteBuffer extends BaseByteBuffer implements DirectBuffer {
 
-	// This class will help us track whether the address is valid or not.
-	static final class SafeAddress {
-		protected volatile boolean isValid = true;
-
-		protected final PlatformAddress address;
-
-		protected SafeAddress(PlatformAddress address) {
-			super();
-			this.address = address;
-		}
-	}
-
-	// This is a wrapped reference to the base address of the buffer memory.
-	protected final SafeAddress safeAddress;
-
-	// This is the offset from the base address at which this buffer logically
-	// starts.
-	protected final int offset;
+    // This class will help us track whether the address is valid or not.
+    static final class SafeAddress {
+        protected volatile boolean isValid = true;
+
+        protected final PlatformAddress address;
+
+        protected SafeAddress(PlatformAddress address) {
+            super();
+            this.address = address;
+        }
+    }
+
+    // This is a wrapped reference to the base address of the buffer memory.
+    protected final SafeAddress safeAddress;
+
+    // This is the offset from the base address at which this buffer logically
+    // starts.
+    protected final int offset;
 
     /*
      * Constructs a new direct byte buffer of the given capacity on newly
-     * allocated OS memory.  The memory will have been zeroed.  When the
-     * instance is discarded the OS memory will be freed if it has not
-     * already been done so by an explicit call to #free().  Callers are
-     * encouraged to explicitly free the memory where possible.
+     * allocated OS memory. The memory will have been zeroed. When the instance
+     * is discarded the OS memory will be freed if it has not already been done
+     * so by an explicit call to #free(). Callers are encouraged to explicitly
+     * free the memory where possible.
      */
-	DirectByteBuffer(int capacity) {
-		this(new SafeAddress(PlatformAddressFactory.alloc(capacity, (byte)0)), capacity, 0);
-		safeAddress.address.autoFree();
-	}
-
-	DirectByteBuffer(SafeAddress address, int capacity, int offset) {
-		super(capacity);
-		this.safeAddress = address;
-		this.offset = offset;
-	}
+    DirectByteBuffer(int capacity) {
+        this(new SafeAddress(PlatformAddressFactory.alloc(capacity, (byte) 0)),
+                capacity, 0);
+        safeAddress.address.autoFree();
+    }
+
+    DirectByteBuffer(SafeAddress address, int capacity, int offset) {
+        super(capacity);
+        this.safeAddress = address;
+        this.offset = offset;
+    }
 
     /*
      * Override ByteBuffer.get(byte[], int, int) to improve performance.
      * 
      * (non-Javadoc)
+     * 
      * @see java.nio.ByteBuffer#get(byte[], int, int)
      */
+    @Override
     public final ByteBuffer get(byte[] dest, int off, int len) {
         int length = dest.length;
-        if ((off < 0 ) || (len < 0) || (long)off + (long)len > length) {
+        if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
             throw new IndexOutOfBoundsException();
         }
         if (len > remaining()) {
             throw new BufferUnderflowException();
         }
-        getBaseAddress().getByteArray(offset+position, dest, off, len);
+        getBaseAddress().getByteArray(offset + position, dest, off, len);
         position += len;
         return this;
     }
-    
-	public final byte get() {
-		if (position == limit) {
-			throw new BufferUnderflowException();
-		}
-		return getBaseAddress().getByte(offset + position++);
-	}
-
-	public final byte get(int index) {
-		if (index < 0 || index >= limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getByte(offset + index);
-	}
-
-	public final double getDouble() {
-		int newPosition = position + 8;
-		if (newPosition > limit) {
-			throw new BufferUnderflowException();
-		}
-		double result = getBaseAddress().getDouble(offset + position, order);
-		position = newPosition;
-		return result;
-	}
-
-	public final double getDouble(int index) {
-		if (index < 0 || (long)index + 8 > limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getDouble(offset + index, order);
-	}
-
-	public final float getFloat() {
-		int newPosition = position + 4;
-		if (newPosition > limit) {
-			throw new BufferUnderflowException();
-		}
-		float result = getBaseAddress().getFloat(offset + position, order);
-		position = newPosition;
-		return result;
-	}
-
-	public final float getFloat(int index) {
-		if (index < 0 || (long)index + 4 > limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getFloat(offset + index, order);
-	}
-
-	public final int getInt() {
-		int newPosition = position + 4;
-		if (newPosition > limit) {
-			throw new BufferUnderflowException();
-		}
-		int result = getBaseAddress().getInt(offset + position, order);
-		position = newPosition;
-		return result;
-	}
-
-	public final int getInt(int index) {
-		if (index < 0 || (long)index + 4 > limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getInt(offset + index, order);
-	}
-
-	public final long getLong() {
-		int newPosition = position + 8;
-		if (newPosition > limit) {
-			throw new BufferUnderflowException();
-		}
-		long result = getBaseAddress().getLong(offset + position, order);
-		position = newPosition;
-		return result;
-	}
-
-	public final long getLong(int index) {
-		if (index < 0 || (long)index + 8 > limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getLong(offset + index, order);
-	}
-
-	public final short getShort() {
-		int newPosition = position + 2;
-		if (newPosition > limit) {
-			throw new BufferUnderflowException();
-		}
-		short result = getBaseAddress().getShort(offset + position, order);
-		position = newPosition;
-		return result;
-	}
-
-	public final short getShort(int index) {
-		if (index < 0 || (long)index + 2 > limit) {
-			throw new IndexOutOfBoundsException();
-		}
-		return getBaseAddress().getShort(offset + index, order);
-	}
-
-	public final boolean isDirect() {
-		return true;
-	}
-
-	public final boolean isAddressValid() {
-		return safeAddress.isValid;
-	}
-
-	public final void addressValidityCheck() {
-		if (!isAddressValid()) {
-            // nio.08=Cannot use the direct byte buffer after it has been explicitly freed.
-			throw new IllegalStateException(
-					Messages.getString("nio.08"));  //$NON-NLS-1$
-		}
-	}
-
-	private void markAddressInvalid() {
-		safeAddress.isValid = false;
-	}
-
-	/*
-	 * Answers the base address of the buffer (i.e. before offset).
-	 */
-	public final PlatformAddress getBaseAddress() {
-		addressValidityCheck();
-		return safeAddress.address;
-	}
-
-	/**
-	 * Answers the platform address of the start of this buffer instance.
-	 * <em>You must not attempt to free the returned address!!</em> It may not
-	 * be an address that was explicitly malloc'ed (i.e. if this buffer is the
-	 * result of a split); and it may be memory shared by multiple buffers.
-	 * <p>
-	 * If you can guarantee that you want to free the underlying memory call the
-	 * #free() method on this instance -- generally applications will rely on
-	 * the garbage collector to autofree this memory.
-	 * </p>
-	 * 
-	 * @return the effective address of the start of the buffer.
-	 * @throws IllegalStateException
-	 *             if this buffer address is known to have been freed
-	 *             previously.
-	 */
-	public final PlatformAddress getEffectiveAddress() {
-		return getBaseAddress().offsetBytes(offset);
-	}
-
-	/**
-	 * Explicitly free the memory used by this direct byte buffer. If the memory
-	 * has already been freed then this is a no-op. Once the memory has been
-	 * freed then operations requiring access to the memory will throw an
-	 * <code>IllegalStateException</code>.
-	 * <p>
-	 * Note this is is possible that the memory is freed by code that reaches
-	 * into the address and explicitly frees it 'beneith' us -- this is bad
-	 * form.
-	 * </p>
-	 */
-	public final void free() {
-		if (isAddressValid()) {
-			markAddressInvalid();
-			safeAddress.address.free();
-		}
-	}
-    
+
+    @Override
+    public final byte get() {
+        if (position == limit) {
+            throw new BufferUnderflowException();
+        }
+        return getBaseAddress().getByte(offset + position++);
+    }
+
+    @Override
+    public final byte get(int index) {
+        if (index < 0 || index >= limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getByte(offset + index);
+    }
+
+    @Override
+    public final double getDouble() {
+        int newPosition = position + 8;
+        if (newPosition > limit) {
+            throw new BufferUnderflowException();
+        }
+        double result = getBaseAddress().getDouble(offset + position, order);
+        position = newPosition;
+        return result;
+    }
+
+    @Override
+    public final double getDouble(int index) {
+        if (index < 0 || (long) index + 8 > limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getDouble(offset + index, order);
+    }
+
+    @Override
+    public final float getFloat() {
+        int newPosition = position + 4;
+        if (newPosition > limit) {
+            throw new BufferUnderflowException();
+        }
+        float result = getBaseAddress().getFloat(offset + position, order);
+        position = newPosition;
+        return result;
+    }
+
+    @Override
+    public final float getFloat(int index) {
+        if (index < 0 || (long) index + 4 > limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getFloat(offset + index, order);
+    }
+
+    @Override
+    public final int getInt() {
+        int newPosition = position + 4;
+        if (newPosition > limit) {
+            throw new BufferUnderflowException();
+        }
+        int result = getBaseAddress().getInt(offset + position, order);
+        position = newPosition;
+        return result;
+    }
+
+    @Override
+    public final int getInt(int index) {
+        if (index < 0 || (long) index + 4 > limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getInt(offset + index, order);
+    }
+
+    @Override
+    public final long getLong() {
+        int newPosition = position + 8;
+        if (newPosition > limit) {
+            throw new BufferUnderflowException();
+        }
+        long result = getBaseAddress().getLong(offset + position, order);
+        position = newPosition;
+        return result;
+    }
+
+    @Override
+    public final long getLong(int index) {
+        if (index < 0 || (long) index + 8 > limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getLong(offset + index, order);
+    }
+
+    @Override
+    public final short getShort() {
+        int newPosition = position + 2;
+        if (newPosition > limit) {
+            throw new BufferUnderflowException();
+        }
+        short result = getBaseAddress().getShort(offset + position, order);
+        position = newPosition;
+        return result;
+    }
+
+    @Override
+    public final short getShort(int index) {
+        if (index < 0 || (long) index + 2 > limit) {
+            throw new IndexOutOfBoundsException();
+        }
+        return getBaseAddress().getShort(offset + index, order);
+    }
+
+    @Override
+    public final boolean isDirect() {
+        return true;
+    }
+
+    public final boolean isAddressValid() {
+        return safeAddress.isValid;
+    }
+
+    public final void addressValidityCheck() {
+        if (!isAddressValid()) {
+            // nio.08=Cannot use the direct byte buffer after it has been
+            // explicitly freed.
+            throw new IllegalStateException(Messages.getString("nio.08")); //$NON-NLS-1$
+        }
+    }
+
+    private void markAddressInvalid() {
+        safeAddress.isValid = false;
+    }
+
+    /*
+     * Answers the base address of the buffer (i.e. before offset).
+     */
+    public final PlatformAddress getBaseAddress() {
+        addressValidityCheck();
+        return safeAddress.address;
+    }
+
+    /**
+     * Answers the platform address of the start of this buffer instance.
+     * <em>You must not attempt to free the returned address!!</em> It may not
+     * be an address that was explicitly malloc'ed (i.e. if this buffer is the
+     * result of a split); and it may be memory shared by multiple buffers.
+     * <p>
+     * If you can guarantee that you want to free the underlying memory call the
+     * #free() method on this instance -- generally applications will rely on
+     * the garbage collector to autofree this memory.
+     * </p>
+     * 
+     * @return the effective address of the start of the buffer.
+     * @throws IllegalStateException
+     *             if this buffer address is known to have been freed
+     *             previously.
+     */
+    public final PlatformAddress getEffectiveAddress() {
+        return getBaseAddress().offsetBytes(offset);
+    }
+
+    /**
+     * Explicitly free the memory used by this direct byte buffer. If the memory
+     * has already been freed then this is a no-op. Once the memory has been
+     * freed then operations requiring access to the memory will throw an
+     * <code>IllegalStateException</code>.
+     * <p>
+     * Note this is is possible that the memory is freed by code that reaches
+     * into the address and explicitly frees it 'beneith' us -- this is bad
+     * form.
+     * </p>
+     */
+    public final void free() {
+        if (isAddressValid()) {
+            markAddressInvalid();
+            safeAddress.address.free();
+        }
+    }
+
+    @Override
     final protected byte[] protectedArray() {
         throw new UnsupportedOperationException();
     }
 
+    @Override
     final protected int protectedArrayOffset() {
         throw new UnsupportedOperationException();
     }
 
+    @Override
     final protected boolean protectedHasArray() {
         return false;
     }
-    
+
     public final int getByteCapacity() {
         return capacity;
     }

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffers.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffers.java?rev=578091&r1=578090&r2=578091&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffers.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/DirectByteBuffers.java Fri Sep 21 06:05:59 2007
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package java.nio; 
+package java.nio;
 
 import org.apache.harmony.luni.platform.PlatformAddress;
 
@@ -25,60 +25,60 @@
  */
 class DirectByteBuffers {
 
-	/**
-	 * Explicitly frees the memory used by the given direct byte buffer.
-	 * <p>
-	 * If the memory is known to already have been freed then this is a no-op.
-	 * Once the memory has been freed then operations requiring access to the
-	 * memory will throw an <code>IllegalStateException</code>.
-	 * </p>
-	 * <p>
-	 * Note this is is possible that the memory is freed by code that reaches
-	 * into the address and explicitly frees it 'beneith' us -- this is bad
-	 * form.
-	 * </p>
-	 * 
-	 * @param directBuffer
-	 *            the direct byte buffer memory to free
-	 * @throws IllegalArgumentException
-	 *             if the buffer is <code>null</code> or is not a
-	 *             <em>direct</em> byte buffer.
-	 */
-	public static void free(ByteBuffer directBuffer) {
-		if ((directBuffer == null) || (!directBuffer.isDirect())) {
-			throw new IllegalArgumentException();
-		}
-		DirectByteBuffer buf = (DirectByteBuffer) directBuffer;
-		buf.free();
-	}
+    /**
+     * Explicitly frees the memory used by the given direct byte buffer.
+     * <p>
+     * If the memory is known to already have been freed then this is a no-op.
+     * Once the memory has been freed then operations requiring access to the
+     * memory will throw an <code>IllegalStateException</code>.
+     * </p>
+     * <p>
+     * Note this is is possible that the memory is freed by code that reaches
+     * into the address and explicitly frees it 'beneith' us -- this is bad
+     * form.
+     * </p>
+     * 
+     * @param directBuffer
+     *            the direct byte buffer memory to free
+     * @throws IllegalArgumentException
+     *             if the buffer is <code>null</code> or is not a
+     *             <em>direct</em> byte buffer.
+     */
+    public static void free(ByteBuffer directBuffer) {
+        if ((directBuffer == null) || (!directBuffer.isDirect())) {
+            throw new IllegalArgumentException();
+        }
+        DirectByteBuffer buf = (DirectByteBuffer) directBuffer;
+        buf.free();
+    }
 
-	/**
-	 * Answers the platform address of the start of this buffer instance.
-	 * <em>You must not attempt to free the returned address!!</em> It may not
-	 * be an address that was explicitly malloc'ed (i.e. if this buffer is the
-	 * result of a split); and it may be memory shared by multiple buffers.
-	 * <p>
-	 * If you can guarantee that you want to free the underlying memory call the
-	 * #free() method on this instance -- generally applications will rely on
-	 * the garbage collector to autofree this memory.
-	 * </p>
-	 * 
-	 * @param directBuffer
-	 *            the direct byte buffer
-	 * @return the effective address of the start of the buffer.
-	 * @throws IllegalStateException
-	 *             if this buffer address is known to have been freed
-	 *             previously.
-	 */
-	public static PlatformAddress getEffectiveAddress(ByteBuffer directBuffer) {
-		return toDirectBuffer(directBuffer).getEffectiveAddress();
-	}
+    /**
+     * Answers the platform address of the start of this buffer instance.
+     * <em>You must not attempt to free the returned address!!</em> It may not
+     * be an address that was explicitly malloc'ed (i.e. if this buffer is the
+     * result of a split); and it may be memory shared by multiple buffers.
+     * <p>
+     * If you can guarantee that you want to free the underlying memory call the
+     * #free() method on this instance -- generally applications will rely on
+     * the garbage collector to autofree this memory.
+     * </p>
+     * 
+     * @param directBuffer
+     *            the direct byte buffer
+     * @return the effective address of the start of the buffer.
+     * @throws IllegalStateException
+     *             if this buffer address is known to have been freed
+     *             previously.
+     */
+    public static PlatformAddress getEffectiveAddress(ByteBuffer directBuffer) {
+        return toDirectBuffer(directBuffer).getEffectiveAddress();
+    }
 
-	private static DirectByteBuffer toDirectBuffer(ByteBuffer directBuffer) {
-		if ((directBuffer == null) || (!directBuffer.isDirect())) {
-			throw new IllegalArgumentException();
-		}
-		return (DirectByteBuffer) directBuffer;
-	}
+    private static DirectByteBuffer toDirectBuffer(ByteBuffer directBuffer) {
+        if ((directBuffer == null) || (!directBuffer.isDirect())) {
+            throw new IllegalArgumentException();
+        }
+        return (DirectByteBuffer) directBuffer;
+    }
 
 }



Mime
View raw message