diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /libjava/classpath/java/nio | |
download | cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2 cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'libjava/classpath/java/nio')
88 files changed, 13862 insertions, 0 deletions
diff --git a/libjava/classpath/java/nio/Buffer.java b/libjava/classpath/java/nio/Buffer.java new file mode 100644 index 000000000..023ab0e1d --- /dev/null +++ b/libjava/classpath/java/nio/Buffer.java @@ -0,0 +1,429 @@ +/* Buffer.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +import gnu.classpath.Pointer; + +/** + * @since 1.4 + */ +public abstract class Buffer +{ + private final int cap; + int limit; + int pos; + int mark; + final Pointer address; + + /** + * Creates a new Buffer. + * + * Should be package private. + */ + Buffer (int capacity, int limit, int position, int mark, + Pointer address) + { + this.address = address; + + if (capacity < 0) + throw new IllegalArgumentException (); + + cap = capacity; + limit (limit); + position (position); + + if (mark >= 0) + { + if (mark > pos) + throw new IllegalArgumentException (); + + this.mark = mark; + } + else + { + this.mark = -1; + } + } + + /** + * Retrieves the capacity of the buffer. + * + * @return the capacity of the buffer + */ + public final int capacity () + { + return cap; + } + + /** + * Clears the buffer. + * + * @return this buffer + */ + public final Buffer clear () + { + limit = cap; + pos = 0; + mark = -1; + return this; + } + + /** + * Flips the buffer. + * + * @return this buffer + */ + public final Buffer flip () + { + limit = pos; + pos = 0; + mark = -1; + return this; + } + + /** + * Tells whether the buffer has remaining data to read or not. + * + * @return true if the buffer contains remaining data to read, + * false otherwise + */ + public final boolean hasRemaining () + { + return remaining() > 0; + } + + /** + * Tells whether this buffer is read only or not. + * + * @return true if the buffer is read only, false otherwise + */ + public abstract boolean isReadOnly (); + + /** + * Retrieves the current limit of the buffer. + * + * @return the limit of the buffer + */ + public final int limit () + { + return limit; + } + + /** + * Sets this buffer's limit. + * + * @param newLimit The new limit value; must be non-negative and no larger + * than this buffer's capacity. + * + * @return this buffer + * + * @exception IllegalArgumentException If the preconditions on newLimit + * do not hold. + */ + public final Buffer limit (int newLimit) + { + if ((newLimit < 0) || (newLimit > cap)) + throw new IllegalArgumentException (); + + if (newLimit < mark) + mark = -1; + + if (pos > newLimit) + pos = newLimit; + + limit = newLimit; + return this; + } + + /** + * Sets this buffer's mark at its position. + * + * @return this buffer + */ + public final Buffer mark () + { + mark = pos; + return this; + } + + /** + * Retrieves the current position of this buffer. + * + * @return the current position of this buffer + */ + public final int position () + { + return pos; + } + + /** + * Sets this buffer's position. If the mark is defined and larger than the + * new position then it is discarded. + * + * @param newPosition The new position value; must be non-negative and no + * larger than the current limit. + * + * @return this buffer + * + * @exception IllegalArgumentException If the preconditions on newPosition + * do not hold + */ + public final Buffer position (int newPosition) + { + if ((newPosition < 0) || (newPosition > limit)) + throw new IllegalArgumentException (); + + if (newPosition <= mark) + mark = -1; + + pos = newPosition; + return this; + } + + /** + * Returns the number of elements between the current position and the limit. + * + * @return the number of remaining elements + */ + public final int remaining() + { + return limit - pos; + } + + /** + * Resets this buffer's position to the previously-marked position. + * + * @return this buffer + * + * @exception InvalidMarkException If the mark has not been set. + */ + public final Buffer reset() + { + if (mark == -1) + throw new InvalidMarkException (); + + pos = mark; + return this; + } + + /** + * Rewinds this buffer. The position is set to zero and the mark + * is discarded. + * + * @return this buffer + */ + public final Buffer rewind() + { + pos = 0; + mark = -1; + return this; + } + + /** + * Checks for underflow. This method is used internally to check + * whether a buffer has enough elements left to satisfy a read + * request. + * + * @exception BufferUnderflowException If there are no remaining + * elements in this buffer. + */ + final void checkForUnderflow() + { + if (!hasRemaining()) + throw new BufferUnderflowException(); + } + + /** + * Checks for underflow. This method is used internally to check + * whether a buffer has enough elements left to satisfy a read + * request for a given number of elements. + * + * @param length The length of a sequence of elements. + * + * @exception BufferUnderflowException If there are not enough + * remaining elements in this buffer. + */ + final void checkForUnderflow(int length) + { + if (remaining() < length) + throw new BufferUnderflowException(); + } + + /** + * Checks for overflow. This method is used internally to check + * whether a buffer has enough space left to satisfy a write + * request. + * + * @exception BufferOverflowException If there is no remaining + * space in this buffer. + */ + final void checkForOverflow() + { + if (!hasRemaining()) + throw new BufferOverflowException(); + } + + /** + * Checks for overflow. This method is used internally to check + * whether a buffer has enough space left to satisfy a write + * request for a given number of elements. + * + * @param length The length of a sequence of elements. + * + * @exception BufferUnderflowException If there is not enough + * remaining space in this buffer. + */ + final void checkForOverflow(int length) + { + if (remaining() < length) + throw new BufferOverflowException(); + } + + /** + * Checks if index is negative or not smaller than the buffer's + * limit. This method is used internally to check whether + * an indexed request can be fulfilled. + * + * @param index The requested position in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + final void checkIndex(int index) + { + if (index < 0 + || index >= limit ()) + throw new IndexOutOfBoundsException (); + } + + /** + * Checks if buffer is read-only. This method is used internally to + * check if elements can be put into a buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + final void checkIfReadOnly() + { + if (isReadOnly()) + throw new ReadOnlyBufferException (); + } + + /** + * Checks whether an array is large enough to hold the given number of + * elements at the given offset. This method is used internally to + * check if an array is big enough. + * + * @param arraylength The length of the array. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than arraylength. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than arraylength - offset. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + static final void checkArraySize(int arraylength, int offset, int length) + { + if ((offset < 0) || + (length < 0) || + (arraylength < length + offset)) + throw new IndexOutOfBoundsException (); + } + + /** + * Returns the backing array of this buffer, if this buffer has one. + * Modification to the array are directly visible in this buffer and vice + * versa. + * + * <p> + * If this is a read-only buffer, then a {@link ReadOnlyBufferException} is + * thrown because exposing the array would allow to circumvent the read-only + * property. If this buffer doesn't have an array, then an + * {@link UnsupportedOperationException} is thrown. Applications should check + * if this buffer supports a backing array by calling {@link #hasArray} + * first.</p> + * + * @return the backing array of this buffer + * + * @throws ReadOnlyBufferException when this buffer is read only + * @throws UnsupportedOperationException when this buffer does not provide + * a backing array + * + * @since 1.6 + */ + public abstract Object array(); + + /** + * Returns <code>true</code> if this buffer can provide a backing array, + * <code>false</code> otherwise. When <code>true</code>, application code + * can call {@link #array()} to access this backing array. + * + * @return <code>true</code> if this buffer can provide a backing array, + * <code>false</code> otherwise + * + * @since 1.6 + */ + public abstract boolean hasArray(); + + /** + * For buffers that are backed by a Java array, this returns the offset + * into that array at which the buffer content starts. + * + * @return the offset into the backing array at which the buffer content + * starts + * @throws ReadOnlyBufferException when this buffer is read only + * @throws UnsupportedOperationException when this buffer does not provide + * a backing array + * + * @since 1.6 + */ + public abstract int arrayOffset(); + + /** + * Returns <code>true</code> when this buffer is direct, <code>false</code> + * otherwise. A direct buffer is usually backed by a raw memory area instead + * of a Java array. + * + * @return <code>true</code> when this buffer is direct, <code>false</code> + * otherwise + * + * @since 1.6 + */ + public abstract boolean isDirect(); +} diff --git a/libjava/classpath/java/nio/BufferOverflowException.java b/libjava/classpath/java/nio/BufferOverflowException.java new file mode 100644 index 000000000..eac79ed75 --- /dev/null +++ b/libjava/classpath/java/nio/BufferOverflowException.java @@ -0,0 +1,53 @@ +/* BufferOverflowException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio; + +/** + * @since 1.4 + */ +public class BufferOverflowException extends RuntimeException +{ + private static final long serialVersionUID = - 5484897634319144535L; + + /** + * Creates the exception + */ + public BufferOverflowException () + { + } +} diff --git a/libjava/classpath/java/nio/BufferUnderflowException.java b/libjava/classpath/java/nio/BufferUnderflowException.java new file mode 100644 index 000000000..76581371b --- /dev/null +++ b/libjava/classpath/java/nio/BufferUnderflowException.java @@ -0,0 +1,53 @@ +/* BufferUnderflowException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio; + +/** + * @since 1.4 + */ +public class BufferUnderflowException extends RuntimeException +{ + private static final long serialVersionUID = - 1713313658691622206L; + + /** + * Creates the exception + */ + public BufferUnderflowException () + { + } +} diff --git a/libjava/classpath/java/nio/ByteBuffer.java b/libjava/classpath/java/nio/ByteBuffer.java new file mode 100644 index 000000000..00db5bf4f --- /dev/null +++ b/libjava/classpath/java/nio/ByteBuffer.java @@ -0,0 +1,655 @@ +/* ByteBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Change gnu.classpath.Pointer to RawData +import gnu.gcj.RawData; +import gnu.classpath.Pointer; + +/** + * @since 1.4 + */ +public abstract class ByteBuffer extends Buffer + implements Comparable<ByteBuffer> +{ + ByteOrder endian = ByteOrder.BIG_ENDIAN; + final byte[] backing_buffer; + final int array_offset; + + ByteBuffer (int capacity, int limit, int position, int mark, + RawData address, byte[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new direct byte buffer. + */ + public static ByteBuffer allocateDirect (int capacity) + { + return DirectByteBufferImpl.allocate (capacity); + } + + /** + * Allocates a new <code>ByteBuffer</code> object with a given capacity. + */ + public static ByteBuffer allocate (int capacity) + { + return wrap(new byte[capacity], 0, capacity); + } + + /** + * Wraps a <code>byte</code> array into a <code>ByteBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final ByteBuffer wrap (byte[] array, int offset, int length) + { + // FIXME: In GCJ and other implementations where arrays may not + // move we might consider, at least when offset==0: + // return new DirectByteBufferImpl(array, + // address_of_data(array) + offset, + // length, length, 0, false); + // This may be more efficient, mainly because we can then use the + // same logic for all ByteBuffers. + + return new ByteBufferImpl (array, 0, array.length, offset + length, offset, -1, false); + } + + /** + * Wraps a <code>byte</code> array into a <code>ByteBuffer</code> + * object. + */ + public static final ByteBuffer wrap (byte[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>byte</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>byte</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>byte</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>byte</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public ByteBuffer get (byte[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>byte</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>byte</code>s remaining in this buffer. + */ + public ByteBuffer get (byte[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>ByteBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>byte</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ByteBuffer put (ByteBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining()); + + if (src.remaining () > 0) + { + byte[] toPut = new byte [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>byte array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>byte</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ByteBuffer put (byte[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>byte array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>byte</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final ByteBuffer put (byte[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>byte</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>byte</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final byte[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>int</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code + */ + public int hashCode () + { + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof ByteBuffer) + { + return compareTo ((ByteBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>ByteBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>ByteBuffer</code>. + */ + public int compareTo (ByteBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + byte a = get(pos_this++); + byte b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public final ByteOrder order () + { + return endian; + } + + /** + * Modifies this buffer's byte order. + */ + public final ByteBuffer order (ByteOrder endian) + { + this.endian = endian; + return this; + } + + /** + * Reads the <code>byte</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>byte</code>s in this buffer. + */ + public abstract byte get (); + + /** + * Writes the <code>byte</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>byte</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ByteBuffer put (byte b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract byte get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ByteBuffer put (int index, byte b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ByteBuffer compact (); + + void shiftDown (int dst_offset, int src_offset, int count) + { + for (int i = 0; i < count; i++) + put(dst_offset + i, get(src_offset + i)); + } + + /** + * Tells whether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>ByteBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract ByteBuffer slice (); + + /** + * Creates a new <code>ByteBuffer</code> that shares this buffer's + * content. + */ + public abstract ByteBuffer duplicate (); + + /** + * Creates a new read-only <code>ByteBuffer</code> that shares this + * buffer's content. + */ + public abstract ByteBuffer asReadOnlyBuffer (); + + /** + * Creates a view of this byte buffer as a short buffer. + */ + public abstract ShortBuffer asShortBuffer (); + + /** + * Creates a view of this byte buffer as a char buffer. + */ + public abstract CharBuffer asCharBuffer (); + + /** + * Creates a view of this byte buffer as an integer buffer. + */ + public abstract IntBuffer asIntBuffer (); + + /** + * Creates a view of this byte buffer as a long buffer. + */ + public abstract LongBuffer asLongBuffer (); + + /** + * Creates a view of this byte buffer as a float buffer. + */ + public abstract FloatBuffer asFloatBuffer (); + + /** + * Creates a view of this byte buffer as a double buffer. + */ + public abstract DoubleBuffer asDoubleBuffer (); + + /** + * Relative get method for reading a character value. + * + * @exception BufferUnderflowException If there are fewer than two bytes + * remaining in this buffer. + */ + public abstract char getChar (); + + /** + * Relative put method for writing a character value. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + */ + public abstract ByteBuffer putChar (char value); + + /** + * Absolute get method for reading a character value. + * + * @exception IndexOutOfBoundsException If there are fewer than two bytes + * remaining in this buffer + */ + public abstract char getChar (int index); + + /** + * Absolute put method for writing a character value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus one. + */ + public abstract ByteBuffer putChar (int index, char value); + + /** + * Relative get method for reading a short value. + * + * @exception BufferUnderflowException If index is negative or not smaller + * than the buffer's limit, minus one. + */ + public abstract short getShort (); + + /** + * Relative put method for writing a short value. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + */ + public abstract ByteBuffer putShort (short value); + + /** + * Absolute get method for reading a short value. + * + * @exception IndexOutOfBoundsException If there are fewer than two bytes + * remaining in this buffer + */ + public abstract short getShort (int index); + + /** + * Absolute put method for writing a short value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus one. + */ + public abstract ByteBuffer putShort (int index, short value); + + /** + * Relative get method for reading an integer value. + * + * @exception BufferUnderflowException If there are fewer than four bytes + * remaining in this buffer. + */ + public abstract int getInt (); + + /** + * Relative put method for writing an integer value. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + */ + public abstract ByteBuffer putInt (int value); + + /** + * Absolute get method for reading an integer value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus three. + */ + public abstract int getInt (int index); + + /** + * Absolute put method for writing an integer value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus three. + */ + public abstract ByteBuffer putInt (int index, int value); + + /** + * Relative get method for reading a long value. + * + * @exception BufferUnderflowException If there are fewer than eight bytes + * remaining in this buffer. + */ + public abstract long getLong (); + + /** + * Relative put method for writing a long value. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + */ + public abstract ByteBuffer putLong (long value); + + /** + * Absolute get method for reading a long value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus seven. + */ + public abstract long getLong (int index); + + /** + * Absolute put method for writing a float value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus seven. + */ + public abstract ByteBuffer putLong (int index, long value); + + /** + * Relative get method for reading a float value. + * + * @exception BufferUnderflowException If there are fewer than four bytes + * remaining in this buffer. + */ + public abstract float getFloat (); + + /** + * Relative put method for writing a float value. + * + * @exception BufferOverflowException If there are fewer than four bytes + * remaining in this buffer. + */ + public abstract ByteBuffer putFloat (float value); + + /** + * Absolute get method for reading a float value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus three. + */ + public abstract float getFloat (int index); + + /** + * Relative put method for writing a float value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus three. + */ + public abstract ByteBuffer putFloat (int index, float value); + + /** + * Relative get method for reading a double value. + * + * @exception BufferUnderflowException If there are fewer than eight bytes + * remaining in this buffer. + */ + public abstract double getDouble (); + + /** + * Relative put method for writing a double value. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + */ + public abstract ByteBuffer putDouble (double value); + + /** + * Absolute get method for reading a double value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus seven. + */ + public abstract double getDouble (int index); + + /** + * Absolute put method for writing a double value. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit, minus seven. + */ + public abstract ByteBuffer putDouble (int index, double value); + + /** + * Returns a string summarizing the state of this buffer. + */ + public String toString () + { + return getClass ().getName () + + "[pos=" + position () + + " lim=" + limit () + + " cap=" + capacity () + "]"; + } +} diff --git a/libjava/classpath/java/nio/ByteBufferHelper.java b/libjava/classpath/java/nio/ByteBufferHelper.java new file mode 100644 index 000000000..aee007b50 --- /dev/null +++ b/libjava/classpath/java/nio/ByteBufferHelper.java @@ -0,0 +1,343 @@ +/* ByteBufferImpl.java -- + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio; + +/** + * @author Michael Koch (konqueror@gmx.de) + */ +final class ByteBufferHelper +{ + public static char getChar (ByteBuffer buffer, ByteOrder order) + { + return (char) getShort (buffer, order); + } + + public static void putChar (ByteBuffer buffer, char value, ByteOrder order) + { + putShort (buffer, (short) value, order); + } + + public static char getChar (ByteBuffer buffer, int index, ByteOrder order) + { + return (char) getShort (buffer, index, order); + } + + public static void putChar (ByteBuffer buffer, int index, + char value, ByteOrder order) + { + putShort (buffer, index, (short) value, order); + } + + public static short getShort (ByteBuffer buffer, ByteOrder order) + { + buffer.checkForUnderflow(2); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + return (short) ((buffer.get() & 0xff) + + (buffer.get() << 8)); + } + + return (short) ((buffer.get() << 8) + + (buffer.get() & 0xff)); + } + + public static void putShort (ByteBuffer buffer, short value, ByteOrder order) + { + buffer.checkForOverflow(2); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put ((byte) value); + buffer.put ((byte) (value >> 8)); + } + else + { + buffer.put ((byte) (value >> 8)); + buffer.put ((byte) value); + } + } + + public static short getShort (ByteBuffer buffer, + int index, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + return (short) ((buffer.get (index) & 0xff) + + (buffer.get (++index) << 8)); + } + + return (short) ((buffer.get (index) << 8) + + (buffer.get (++index) & 0xff)); + } + + public static void putShort (ByteBuffer buffer, int index, + short value, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put (index, (byte) value); + buffer.put (++index, (byte) (value >> 8)); + } + else + { + buffer.put (index, (byte) (value >> 8)); + buffer.put (++index, (byte) value); + } + } + + public static int getInt (ByteBuffer buffer, ByteOrder order) + { + buffer.checkForUnderflow(4); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + return ((buffer.get() & 0xff) + + ((buffer.get() & 0xff) << 8) + + ((buffer.get() & 0xff) << 16) + + (buffer.get() << 24)); + } + + return (int) ((buffer.get() << 24) + + ((buffer.get() & 0xff) << 16) + + ((buffer.get() & 0xff) << 8) + + (buffer.get() & 0xff)); + } + + public static void putInt (ByteBuffer buffer, int value, ByteOrder order) + { + buffer.checkForOverflow(4); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put ((byte) value); + buffer.put ((byte) (value >> 8)); + buffer.put ((byte) (value >> 16)); + buffer.put ((byte) (value >> 24)); + } + else + { + buffer.put ((byte) (value >> 24)); + buffer.put ((byte) (value >> 16)); + buffer.put ((byte) (value >> 8)); + buffer.put ((byte) value); + } + } + + public static int getInt (ByteBuffer buffer, int index, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + return ((buffer.get (index) & 0xff) + + ((buffer.get (++index) & 0xff) << 8) + + ((buffer.get (++index) & 0xff) << 16) + + (buffer.get (++index) << 24)); + } + + return ((buffer.get (index) << 24) + + ((buffer.get (++index) & 0xff) << 16) + + ((buffer.get (++index) & 0xff) << 8) + + (buffer.get (++index) & 0xff)); + } + + public static void putInt (ByteBuffer buffer, int index, + int value, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put (index, (byte) value); + buffer.put (++index, (byte) (value >> 8)); + buffer.put (++index, (byte) (value >> 16)); + buffer.put (++index, (byte) (value >> 24)); + } + else + { + buffer.put (index, (byte) (value >> 24)); + buffer.put (++index, (byte) (value >> 16)); + buffer.put (++index, (byte) (value >> 8)); + buffer.put (++index, (byte) value); + } + } + + public static long getLong (ByteBuffer buffer, ByteOrder order) + { + buffer.checkForUnderflow(8); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + return ((buffer.get() & 0xff) + + (((buffer.get() & 0xff)) << 8) + + (((buffer.get() & 0xff)) << 16) + + (((buffer.get() & 0xffL)) << 24) + + (((buffer.get() & 0xffL)) << 32) + + (((buffer.get() & 0xffL)) << 40) + + (((buffer.get() & 0xffL)) << 48) + + (((long) buffer.get()) << 56)); + } + + return ((((long) buffer.get()) << 56) + + ((buffer.get() & 0xffL) << 48) + + ((buffer.get() & 0xffL) << 40) + + ((buffer.get() & 0xffL) << 32) + + ((buffer.get() & 0xffL) << 24) + + ((buffer.get() & 0xff) << 16) + + ((buffer.get() & 0xff) << 8) + + (buffer.get() & 0xff)); + } + + public static void putLong (ByteBuffer buffer, long value, ByteOrder order) + { + buffer.checkForOverflow(8); + + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put ((byte) value); + buffer.put ((byte) (value >> 8)); + buffer.put ((byte) (value >> 16)); + buffer.put ((byte) (value >> 24)); + buffer.put ((byte) (value >> 32)); + buffer.put ((byte) (value >> 40)); + buffer.put ((byte) (value >> 48)); + buffer.put ((byte) (value >> 56)); + } + else + { + buffer.put ((byte) (value >> 56)); + buffer.put ((byte) (value >> 48)); + buffer.put ((byte) (value >> 40)); + buffer.put ((byte) (value >> 32)); + buffer.put ((byte) (value >> 24)); + buffer.put ((byte) (value >> 16)); + buffer.put ((byte) (value >> 8)); + buffer.put ((byte) value); + } + } + + public static long getLong (ByteBuffer buffer, int index, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + return ((buffer.get (index) & 0xff) + + ((buffer.get (++index) & 0xff) << 8) + + ((buffer.get (++index) & 0xff) << 16) + + ((buffer.get (++index) & 0xffL) << 24) + + ((buffer.get (++index) & 0xffL) << 32) + + ((buffer.get (++index) & 0xffL) << 40) + + ((buffer.get (++index) & 0xffL) << 48) + + (((long) buffer.get (++index)) << 56)); + } + + return ((((long) buffer.get (index)) << 56) + + ((buffer.get (++index) & 0xffL) << 48) + + ((buffer.get (++index) & 0xffL) << 40) + + ((buffer.get (++index) & 0xffL) << 32) + + ((buffer.get (++index) & 0xffL) << 24) + + ((buffer.get (++index) & 0xff) << 16) + + ((buffer.get (++index) & 0xff) << 8) + + (buffer.get (++index) & 0xff)); + } + + public static void putLong (ByteBuffer buffer, int index, + long value, ByteOrder order) + { + if (order == ByteOrder.LITTLE_ENDIAN) + { + buffer.put (index, (byte) value); + buffer.put (++index, (byte) (value >> 8)); + buffer.put (++index, (byte) (value >> 16)); + buffer.put (++index, (byte) (value >> 24)); + buffer.put (++index, (byte) (value >> 32)); + buffer.put (++index, (byte) (value >> 40)); + buffer.put (++index, (byte) (value >> 48)); + buffer.put (++index, (byte) (value >> 56)); + } + else + { + buffer.put (index, (byte) (value >> 56)); + buffer.put (++index, (byte) (value >> 48)); + buffer.put (++index, (byte) (value >> 40)); + buffer.put (++index, (byte) (value >> 32)); + buffer.put (++index, (byte) (value >> 24)); + buffer.put (++index, (byte) (value >> 16)); + buffer.put (++index, (byte) (value >> 8)); + buffer.put (++index, (byte) value); + } + } + + public static float getFloat (ByteBuffer buffer, ByteOrder order) + { + return Float.intBitsToFloat (getInt (buffer, order)); + } + + public static void putFloat (ByteBuffer buffer, float value, ByteOrder order) + { + putInt (buffer, Float.floatToRawIntBits (value), order); + } + + public static float getFloat (ByteBuffer buffer, int index, ByteOrder order) + { + return Float.intBitsToFloat (getInt (buffer, index, order)); + } + + public static void putFloat (ByteBuffer buffer, int index, + float value, ByteOrder order) + { + putInt (buffer, index, Float.floatToRawIntBits (value), order); + } + + public static double getDouble (ByteBuffer buffer, ByteOrder order) + { + return Double.longBitsToDouble (getLong (buffer, order)); + } + + public static void putDouble (ByteBuffer buffer, double value, ByteOrder order) + { + putLong (buffer, Double.doubleToRawLongBits (value), order); + } + + public static double getDouble (ByteBuffer buffer, int index, ByteOrder order) + { + return Double.longBitsToDouble (getLong (buffer, index, order)); + } + + public static void putDouble (ByteBuffer buffer, int index, + double value, ByteOrder order) + { + putLong (buffer, index, Double.doubleToRawLongBits (value), order); + } +} // ByteBufferHelper diff --git a/libjava/classpath/java/nio/ByteBufferImpl.java b/libjava/classpath/java/nio/ByteBufferImpl.java new file mode 100644 index 000000000..2bf319220 --- /dev/null +++ b/libjava/classpath/java/nio/ByteBufferImpl.java @@ -0,0 +1,374 @@ +/* ByteBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class ByteBufferImpl extends ByteBuffer +{ + private final boolean readOnly; + + ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, + int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public CharBuffer asCharBuffer () + { + return new CharViewBufferImpl (this, remaining() >> 1); + } + + public ShortBuffer asShortBuffer () + { + return new ShortViewBufferImpl (this, remaining() >> 1); + } + + public IntBuffer asIntBuffer () + { + return new IntViewBufferImpl (this, remaining() >> 2); + } + + public LongBuffer asLongBuffer () + { + return new LongViewBufferImpl (this, remaining() >> 3); + } + + public FloatBuffer asFloatBuffer () + { + return new FloatViewBufferImpl (this, remaining() >> 2); + } + + public DoubleBuffer asDoubleBuffer () + { + return new DoubleViewBufferImpl (this, remaining() >> 3); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public ByteBuffer slice () + { + return new ByteBufferImpl (backing_buffer, array_offset + position (), + remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public ByteBuffer duplicate () + { + return new ByteBufferImpl (backing_buffer, array_offset, capacity (), + limit (), position (), mark, isReadOnly ()); + } + + public ByteBuffer asReadOnlyBuffer () + { + return new ByteBufferImpl (backing_buffer, array_offset, capacity (), + limit (), position (), mark, true); + } + + void shiftDown (int dst_offset, int src_offset, int count) + { + System.arraycopy(backing_buffer, array_offset + src_offset, + backing_buffer, array_offset + dst_offset, + count); + } + + public ByteBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int pos = position(); + int n = limit() - pos; + if (n > 0) + shiftDown(0, pos, n); + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>byte</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>bytes</code> in this buffer. + */ + public byte get () + { + if (pos >= limit) + throw new BufferUnderflowException(); + + return backing_buffer [(pos++) + array_offset]; + } + + /** + * Bulk get + */ + public ByteBuffer get (byte[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + if ( (limit - pos) < length) // check for overflow + throw new BufferUnderflowException(); + + System.arraycopy(backing_buffer, pos + array_offset, + dst, offset, length); + pos += length; + + return this; + } + + /** + * Relative bulk put(), overloads the ByteBuffer impl. + */ + public ByteBuffer put (byte[] src, int offset, int length) + { + if ( (limit - pos) < length) // check for overflow + throw new BufferOverflowException(); + checkArraySize(src.length, offset, length); + + System.arraycopy(src, offset, backing_buffer, pos + array_offset, length); + pos += length; + + return this; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there is no remaining + * space in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ByteBuffer put (byte value) + { + if (readOnly) + throw new ReadOnlyBufferException(); + if (pos >= limit) + throw new BufferOverflowException(); + + backing_buffer [(pos++) + array_offset] = value; + return this; + } + + /** + * Absolute get method. Reads the <code>byte</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public byte get (int index) + { + checkIndex(index); + + return backing_buffer [index + array_offset]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ByteBuffer put (int index, byte value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index + array_offset] = value; + return this; + } + + public char getChar () + { + return ByteBufferHelper.getChar(this, order()); + } + + public ByteBuffer putChar (char value) + { + if (readOnly) + throw new ReadOnlyBufferException (); + if ( (limit-pos) < 2) + throw new BufferOverflowException(); + + if (endian == ByteOrder.LITTLE_ENDIAN) + { + backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF); + backing_buffer [(pos++) + array_offset] = (byte)(value>>8); + } + else + { + backing_buffer [(pos++) + array_offset] = (byte)(value>>8); + backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF); + } + return this; + } + + public char getChar (int index) + { + return ByteBufferHelper.getChar(this, index, order()); + } + + public ByteBuffer putChar (int index, char value) + { + ByteBufferHelper.putChar(this, index, value, order()); + return this; + } + + public short getShort () + { + return ByteBufferHelper.getShort(this, order()); + } + + public ByteBuffer putShort (short value) + { + ByteBufferHelper.putShort(this, value, order()); + return this; + } + + public short getShort (int index) + { + return ByteBufferHelper.getShort(this, index, order()); + } + + public ByteBuffer putShort (int index, short value) + { + ByteBufferHelper.putShort(this, index, value, order()); + return this; + } + + public int getInt () + { + return ByteBufferHelper.getInt(this, order()); + } + + public ByteBuffer putInt (int value) + { + ByteBufferHelper.putInt(this, value, order()); + return this; + } + + public int getInt (int index) + { + return ByteBufferHelper.getInt(this, index, order()); + } + + public ByteBuffer putInt (int index, int value) + { + ByteBufferHelper.putInt(this, index, value, order()); + return this; + } + + public long getLong () + { + return ByteBufferHelper.getLong(this, order()); + } + + public ByteBuffer putLong (long value) + { + ByteBufferHelper.putLong (this, value, order()); + return this; + } + + public long getLong (int index) + { + return ByteBufferHelper.getLong (this, index, order()); + } + + public ByteBuffer putLong (int index, long value) + { + ByteBufferHelper.putLong (this, index, value, order()); + return this; + } + + public float getFloat () + { + return ByteBufferHelper.getFloat (this, order()); + } + + public ByteBuffer putFloat (float value) + { + ByteBufferHelper.putFloat (this, value, order()); + return this; + } + + public float getFloat (int index) + { + return ByteBufferHelper.getFloat (this, index, order()); + } + + public ByteBuffer putFloat (int index, float value) + { + ByteBufferHelper.putFloat (this, index, value, order()); + return this; + } + + public double getDouble () + { + return ByteBufferHelper.getDouble (this, order()); + } + + public ByteBuffer putDouble (double value) + { + ByteBufferHelper.putDouble (this, value, order()); + return this; + } + + public double getDouble (int index) + { + return ByteBufferHelper.getDouble (this, index, order()); + } + + public ByteBuffer putDouble (int index, double value) + { + ByteBufferHelper.putDouble (this, index, value, order()); + return this; + } +} diff --git a/libjava/classpath/java/nio/ByteOrder.java b/libjava/classpath/java/nio/ByteOrder.java new file mode 100644 index 000000000..364aa1829 --- /dev/null +++ b/libjava/classpath/java/nio/ByteOrder.java @@ -0,0 +1,84 @@ +/* ByteOrder.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * @author Michael Koch (konqueror@gmx.de) + * @since 1.4 + */ +public final class ByteOrder +{ + /** + * Constant indicating big endian byte order. + */ + public static final ByteOrder BIG_ENDIAN = new ByteOrder(); + + /** + * Constant indicating little endian byte order. + */ + public static final ByteOrder LITTLE_ENDIAN = new ByteOrder(); + + /** + * Returns the native byte order of the platform currently running. + * + * @return the native byte order + */ + public static ByteOrder nativeOrder() + { + // Let this fail with an NPE when the property is not set correctly. + // Otherwise we risk that NIO is silently working wrong. + return (System.getProperty("gnu.cpu.endian").equals("big") + ? BIG_ENDIAN : LITTLE_ENDIAN); + } + + /** + * Returns a string representation of the byte order. + * + * @return the string + */ + public String toString() + { + return this == BIG_ENDIAN ? "BIG_ENDIAN" : "LITTLE_ENDIAN"; + } + + // This class can only be instantiated here. + private ByteOrder() + { + } +} diff --git a/libjava/classpath/java/nio/CharBuffer.java b/libjava/classpath/java/nio/CharBuffer.java new file mode 100644 index 000000000..a1729e6e7 --- /dev/null +++ b/libjava/classpath/java/nio/CharBuffer.java @@ -0,0 +1,532 @@ +/* CharBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer +import gnu.gcj.RawData; + +import java.io.IOException; + +/** + * @since 1.4 + */ +public abstract class CharBuffer extends Buffer + implements Comparable<CharBuffer>, CharSequence, Readable, Appendable +{ + final int array_offset; + final char[] backing_buffer; + + CharBuffer (int capacity, int limit, int position, int mark, + RawData address, char[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>CharBuffer</code> object with a given capacity. + */ + public static CharBuffer allocate (int capacity) + { + return new CharBufferImpl (capacity); + } + + /** + * Wraps a <code>char</code> array into a <code>CharBuffer</code> + * object. + * + * @param array the array to wrap + * @param offset the offset of the region in the array to wrap + * @param length the length of the region in the array to wrap + * + * @return a new <code>CharBuffer</code> object + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final CharBuffer wrap(char[] array, int offset, int length) + { + return new CharBufferImpl(array, 0, array.length, offset + length, offset, + -1, false); + } + + /** + * Wraps a character sequence into a <code>CharBuffer</code> object. + * + * @param seq the sequence to wrap + * + * @return a new <code>CharBuffer</code> object + */ + public static final CharBuffer wrap(CharSequence seq) + { + return wrap(seq, 0, seq.length()); + } + + /** + * Wraps a character sequence into a <code>CharBuffer</code> object. + * + * @param seq the sequence to wrap + * @param start the index of the first character to wrap + * @param end the index of the first character not to wrap + * + * @return a new <code>CharBuffer</code> object + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final CharBuffer wrap(CharSequence seq, int start, int end) + { + return new CharSequenceBuffer(seq, start, end); + } + + /** + * Wraps a <code>char</code> array into a <code>CharBuffer</code> + * object. + * + * @param array the array to wrap + * + * @return a new <code>CharBuffer</code> object + */ + public static final CharBuffer wrap(char[] array) + { + return wrap(array, 0, array.length); + } + + /** + * This method transfers <code>char</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>char</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>char</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>char</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public CharBuffer get (char[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** @since 1.5 */ + public int read(CharBuffer buffer) throws IOException + { + // We want to call put(), so we don't manipulate the CharBuffer + // directly. + int rem = Math.min(buffer.remaining(), remaining()); + char[] buf = new char[rem]; + get(buf); + buffer.put(buf); + return rem; + } + + /** + * This method transfers <code>char</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>char</code>s remaining in this buffer. + */ + public CharBuffer get (char[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>CharBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>char</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public CharBuffer put (CharBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining()); + + if (src.remaining () > 0) + { + char[] toPut = new char [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>char array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>char</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public CharBuffer put (char[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>char array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>char</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final CharBuffer put (char[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>char</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>char</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final char[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + */ + public int hashCode () + { + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof CharBuffer) + { + return compareTo ((CharBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>CharBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>CharBuffer</code>. + */ + public int compareTo (CharBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + char a = get(pos_this++); + char b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>char</code>s in this buffer. + */ + public abstract char get (); + + /** + * Writes the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>char</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract CharBuffer put (char b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract char get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract CharBuffer put (int index, char b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract CharBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>CharBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract CharBuffer slice (); + + /** + * Creates a new <code>CharBuffer</code> that shares this buffer's + * content. + */ + public abstract CharBuffer duplicate (); + + /** + * Creates a new read-only <code>CharBuffer</code> that shares this + * buffer's content. + */ + public abstract CharBuffer asReadOnlyBuffer (); + + /** + * Returns the remaining content of the buffer as a string. + */ + public String toString () + { + if (hasArray ()) + return new String (array (), position (), length ()); + + char[] buf = new char [length ()]; + int pos = position (); + get (buf, 0, buf.length); + position (pos); + return new String (buf); + } + + /** + * Returns the length of the remaining chars in this buffer. + */ + public final int length () + { + return remaining (); + } + + /** + * Creates a new character buffer that represents the specified subsequence + * of this buffer, relative to the current position. + * + * @exception IndexOutOfBoundsException If the preconditions on start and + * end do not hold. + */ + public abstract CharSequence subSequence (int start, int length); + + /** + * Relative put method. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. + * @exception IndexOutOfBoundsException If the preconditions on the start + * and end parameters do not hold. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public CharBuffer put (String str, int start, int length) + { + return put (str.toCharArray (), start, length); + } + + /** + * Relative put method. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final CharBuffer put (String str) + { + return put (str.toCharArray (), 0, str.length ()); + } + + /** + * Returns the character at <code>position() + index</code>. + * + * @exception IndexOutOfBoundsException If index is negative not smaller than + * <code>remaining()</code>. + */ + public final char charAt (int index) + { + if (index < 0 + || index >= remaining ()) + throw new IndexOutOfBoundsException (); + + return get (position () + index); + } + + /** @since 1.5 */ + public CharBuffer append(char c) + { + put(c); + return this; + } + + /** @since 1.5 */ + public CharBuffer append(CharSequence cs) + { + put(cs == null ? "null" : cs.toString()); + return this; + } + + /** @since 1.5 */ + public CharBuffer append(CharSequence cs, int start, int end) + { + put(cs == null ? "null" : cs.subSequence(start, end).toString()); + return this; + } +} diff --git a/libjava/classpath/java/nio/CharBufferImpl.java b/libjava/classpath/java/nio/CharBufferImpl.java new file mode 100644 index 000000000..8df9dbbea --- /dev/null +++ b/libjava/classpath/java/nio/CharBufferImpl.java @@ -0,0 +1,214 @@ +/* CharBufferImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class CharBufferImpl extends CharBuffer +{ + private final boolean readOnly; + + CharBufferImpl (int capacity) + { + this (new char [capacity], 0, capacity, capacity, 0, -1, false); + } + + CharBufferImpl (char[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public CharBufferImpl (CharBufferImpl copy) + { + super (copy.capacity (), copy.limit (), copy.position (), 0, null, copy.backing_buffer, copy.array_offset); + this.readOnly = copy.isReadOnly (); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public CharBuffer slice () + { + return new CharBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public CharBuffer duplicate () + { + return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ()); + } + + public CharBuffer asReadOnlyBuffer () + { + return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true); + } + + public CharBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + public CharSequence subSequence (int start, int end) + { + if (start < 0 + || start > length () + || end < start + || end > length ()) + throw new IndexOutOfBoundsException (); + + return new CharBufferImpl (backing_buffer, array_offset, capacity (), position () + end, position () + start, -1, isReadOnly ()); + } + + /** + * Reads the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>char</code>s in this buffer. + */ + public char get () + { + if (pos >= limit) + throw new BufferUnderflowException(); + + return backing_buffer [(pos++) + array_offset]; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public CharBuffer put (char value) + { + if (readOnly) + throw new ReadOnlyBufferException(); + if (pos >= limit) + throw new BufferOverflowException(); + + backing_buffer [(pos++) + array_offset] = value; + return this; + } + + /** + * Absolute get method. Reads the <code>char</code> at position + * <code>index</code>. + * + * @param index Position to read the <code>char</code> from. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public char get (int index) + { + checkIndex(index); + + return backing_buffer [index + array_offset]; + } + + /** + * Bulk get, overloaded for speed. + */ + public CharBuffer get (char[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + System.arraycopy(backing_buffer, pos + array_offset, + dst, offset, length); + pos += length; + return this; + } + + /** + * Bulk put, overloaded for speed. + */ + public CharBuffer put (char[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + System.arraycopy(src, offset, + backing_buffer, pos + array_offset, length); + pos += length; + return this; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public CharBuffer put (int index, char value) + { + checkIndex(index); + checkIfReadOnly(); + + backing_buffer [index + array_offset] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/CharSequenceBuffer.java b/libjava/classpath/java/nio/CharSequenceBuffer.java new file mode 100644 index 000000000..95c37cc97 --- /dev/null +++ b/libjava/classpath/java/nio/CharSequenceBuffer.java @@ -0,0 +1,207 @@ +/* CharBuffer.java -- + Copyright (C) 2007 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * A CharBuffer that wraps a {@link CharSequence}. + */ +final class CharSequenceBuffer + extends CharBuffer +{ + + /** + * The wrapped char sequence. + */ + private final CharSequence charSequence; + + /** + * Creates a new CharSequenceBuffer. + * + * @param charSeq the CharSequence to wrap + * @param capacity the capacity + * @param limit the limit + * @param position the position + * @param mark the mark + * @param offs the offset + */ + CharSequenceBuffer(CharSequence charSeq, int capacity, int limit, + int position, int mark, int offs) + { + super(capacity, limit, position, mark, null, null, offs); + this.charSequence = charSeq; + } + + /** + * Creates a new instance of CharSequenceBuffer, wrapping the specified + * {@link CharSequence}. + * + * @param charSeq the char sequence to wrap + * @param start the start index in the char sequence + * @param end the end index in the char sequence + */ + CharSequenceBuffer(CharSequence charSeq, int start, int end) + { + this(charSeq, charSeq.length(), end, start, -1, 0); + } + + /** + * Returns a read-only view of this buffer. + */ + public CharBuffer asReadOnlyBuffer() + { + return duplicate(); + } + + /** + * This buffer class is not writable by definition and thus throws + * a ReadOnlyBufferException here. + */ + public CharBuffer compact() + { + throw new ReadOnlyBufferException(); + } + + /** + * Returns a duplicate of this buffer. + * + * @return a duplicate of this buffer + */ + public CharBuffer duplicate() + { + return new CharSequenceBuffer(charSequence, capacity(), limit, pos, mark, 0); + } + + /** + * Returns the character at the current position. + * + * @return the character at the current position + */ + public char get() + { + if (pos >= limit) + throw new BufferUnderflowException(); + + return charSequence.charAt(array_offset + pos++); + } + + /** + * Returns the character at the specified position. + * + * @return the character at the specified position + */ + public char get(int index) + { + if (index < 0 || index >= limit) + throw new IndexOutOfBoundsException(); + + return charSequence.charAt(array_offset + index); + } + + /** + * Cannot be direct, return <code>false</code> here. + * + * @return false + */ + public boolean isDirect() + { + return false; + } + + /** + * Returns the byte order of this buffer. This is always the native byte + * order. + * + * @return the byte order of this buffer + */ + public ByteOrder order() + { + return ByteOrder.nativeOrder(); + } + + /** + * This buffer class is not writable by definition and thus throws + * a ReadOnlyBufferException here. + */ + public CharBuffer put(char b) + { + throw new ReadOnlyBufferException(); + } + + /** + * This buffer class is not writable by definition and thus throws + * a ReadOnlyBufferException here. + */ + public CharBuffer put(int index, char b) + { + throw new ReadOnlyBufferException(); + } + + /** + * Returns a slice of this buffer, exposing the current position and limit. + */ + public CharBuffer slice() + { + int newCapacity = limit - pos; + return new CharSequenceBuffer(charSequence, newCapacity, newCapacity, 0, + -1, pos); + } + + /** + * Returns a sub sequence from the specified start index and with the + * specified length. + * + * @param start the start index + * @param length the length of the sub sequence + */ + public CharSequence subSequence(int start, int length) + { + int begin = array_offset + start + pos; + return charSequence.subSequence(begin, begin + length); + } + + /** + * This kind of CharBuffer is read-only, so we return <code>true</code> + * here. + */ + public boolean isReadOnly() + { + return true; + } + +} diff --git a/libjava/classpath/java/nio/CharViewBufferImpl.java b/libjava/classpath/java/nio/CharViewBufferImpl.java new file mode 100644 index 000000000..72e344d66 --- /dev/null +++ b/libjava/classpath/java/nio/CharViewBufferImpl.java @@ -0,0 +1,187 @@ +/* CharViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +class CharViewBufferImpl extends CharBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + CharViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()) : null, + null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public CharViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, offset) : null, + null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>char</code>s in this buffer. + */ + public char get () + { + int p = position(); + char result = ByteBufferHelper.getChar(bb, (p << 1) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>char</code> at position + * <code>index</code>. + * + * @param index Position to read the <code>char</code> from. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public char get (int index) + { + return ByteBufferHelper.getChar(bb, (index << 1) + offset, endian); + } + + public CharBuffer put (char value) + { + int p = position(); + ByteBufferHelper.putChar(bb, (p << 1) + offset, value, endian); + position(p + 1); + return this; + } + + public CharBuffer put (int index, char value) + { + ByteBufferHelper.putChar(bb, (index << 1) + offset, value, endian); + return this; + } + + public CharBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 2 * position(), 2 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public CharBuffer slice () + { + // Create a sliced copy of this object that shares its content. + return new CharViewBufferImpl (bb, (position () << 1) + offset, + remaining (), remaining (), 0, -1, + isReadOnly (), endian); + } + + CharBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new CharViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public CharBuffer duplicate () + { + return duplicate(readOnly); + } + + public CharBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public CharSequence subSequence (int start, int end) + { + if (start < 0 + || end < start + || end > length ()) + throw new IndexOutOfBoundsException (); + + return new CharViewBufferImpl (bb, array_offset, capacity (), + position () + end, position () + start, + -1, isReadOnly (), endian); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/DirectByteBufferImpl.java b/libjava/classpath/java/nio/DirectByteBufferImpl.java new file mode 100644 index 000000000..4f34732a8 --- /dev/null +++ b/libjava/classpath/java/nio/DirectByteBufferImpl.java @@ -0,0 +1,441 @@ +/* DirectByteBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +import gnu.classpath.Pointer; + +abstract class DirectByteBufferImpl extends ByteBuffer +{ + /** + * The owner is used to keep alive the object that actually owns the + * memory. There are three possibilities: + * 1) owner == this: We allocated the memory and we should free it, + * but *only* in finalize (if we've been sliced + * other objects will also have access to the + * memory). + * 2) owner == null: The byte buffer was created thru + * JNI.NewDirectByteBuffer. The JNI code is + * responsible for freeing the memory. + * 3) owner == some other object: The other object allocated the + * memory and should free it. + */ + private final Object owner; + + static final class ReadOnly extends DirectByteBufferImpl + { + ReadOnly(Object owner, Pointer address, + int capacity, int limit, + int position) + { + super(owner, address, capacity, limit, position); + } + + public ByteBuffer put(byte value) + { + throw new ReadOnlyBufferException (); + } + + public ByteBuffer put(int index, byte value) + { + throw new ReadOnlyBufferException (); + } + + public boolean isReadOnly() + { + return true; + } + } + + static final class ReadWrite extends DirectByteBufferImpl + { + ReadWrite(int capacity) + { + super(capacity); + } + + ReadWrite(Pointer address, int capacity) + { + super(address, capacity); + } + + ReadWrite(Object owner, Pointer address, + int capacity, int limit, + int position) + { + super(owner, address, capacity, limit, position); + } + + public boolean isReadOnly() + { + return false; + } + } + + DirectByteBufferImpl(int capacity) + { + super(capacity, capacity, 0, -1, + VMDirectByteBuffer.allocate(capacity), null, 0); + this.owner = this; + } + + DirectByteBufferImpl(Pointer address, int capacity) + { + super(capacity, capacity, 0, -1, address, null, 0); + this.owner = null; + } + + DirectByteBufferImpl(Object owner, Pointer address, + int capacity, int limit, + int position) + { + super(capacity, limit, position, -1, address, null, 0); + this.owner = owner; + } + + /** + * Allocates a new direct byte buffer. + */ + public static ByteBuffer allocate(int capacity) + { + return new DirectByteBufferImpl.ReadWrite(capacity); + } + + protected void finalize() throws Throwable + { + if (owner == this) + VMDirectByteBuffer.free(address); + } + + public byte get() + { + checkForUnderflow(); + + int pos = position(); + byte result = VMDirectByteBuffer.get(address, pos); + position(pos + 1); + return result; + } + + public byte get(int index) + { + checkIndex(index); + + return VMDirectByteBuffer.get(address, index); + } + + public ByteBuffer get(byte[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + int index = position(); + VMDirectByteBuffer.get(address, index, dst, offset, length); + position(index+length); + + return this; + } + + public ByteBuffer put(byte value) + { + checkForOverflow(); + + int pos = position(); + VMDirectByteBuffer.put(address, pos, value); + position(pos + 1); + return this; + } + + public ByteBuffer put(int index, byte value) + { + checkIndex(index); + + VMDirectByteBuffer.put(address, index, value); + return this; + } + + public ByteBuffer put (byte[] src, int offset, int length) + { + checkArraySize (src.length, offset, length); + checkForUnderflow (length); + int index = position (); + VMDirectByteBuffer.put (address, index, src, offset, length); + position (index + length); + + return this; + } + + void shiftDown(int dst_offset, int src_offset, int count) + { + VMDirectByteBuffer.shiftDown(address, dst_offset, src_offset, count); + } + + public ByteBuffer compact() + { + checkIfReadOnly(); + mark = -1; + int pos = position(); + if (pos > 0) + { + int count = remaining(); + VMDirectByteBuffer.shiftDown(address, 0, pos, count); + position(count); + limit(capacity()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public ByteBuffer slice() + { + int rem = remaining(); + if (isReadOnly()) + return new DirectByteBufferImpl.ReadOnly + (owner, VMDirectByteBuffer.adjustAddress(address, position()), + rem, rem, 0); + else + return new DirectByteBufferImpl.ReadWrite + (owner, VMDirectByteBuffer.adjustAddress(address, position()), + rem, rem, 0); + } + + private ByteBuffer duplicate(boolean readOnly) + { + int pos = position(); + if (this.mark != -1) + reset(); + int mark = position(); + position(pos); + DirectByteBufferImpl result; + if (readOnly) + result = new DirectByteBufferImpl.ReadOnly(owner, address, capacity(), + limit(), pos); + else + result = new DirectByteBufferImpl.ReadWrite(owner, address, capacity(), + limit(), pos); + + if (mark != pos) + { + result.position(mark); + result.mark(); + result.position(pos); + } + return result; + } + + public ByteBuffer duplicate() + { + return duplicate(isReadOnly()); + } + + public ByteBuffer asReadOnlyBuffer() + { + return duplicate(true); + } + + public boolean isDirect() + { + return true; + } + + public CharBuffer asCharBuffer() + { + return new CharViewBufferImpl(this, remaining() >> 1); + } + + public ShortBuffer asShortBuffer() + { + return new ShortViewBufferImpl(this, remaining() >> 1); + } + + public IntBuffer asIntBuffer() + { + return new IntViewBufferImpl(this, remaining() >> 2); + } + + public LongBuffer asLongBuffer() + { + return new LongViewBufferImpl(this, remaining() >> 3); + } + + public FloatBuffer asFloatBuffer() + { + return new FloatViewBufferImpl(this, remaining() >> 2); + } + + public DoubleBuffer asDoubleBuffer() + { + return new DoubleViewBufferImpl(this, remaining() >> 3); + } + + public char getChar() + { + return ByteBufferHelper.getChar(this, order()); + } + + public ByteBuffer putChar(char value) + { + ByteBufferHelper.putChar(this, value, order()); + return this; + } + + public char getChar(int index) + { + return ByteBufferHelper.getChar(this, index, order()); + } + + public ByteBuffer putChar(int index, char value) + { + ByteBufferHelper.putChar(this, index, value, order()); + return this; + } + + public short getShort() + { + return ByteBufferHelper.getShort(this, order()); + } + + public ByteBuffer putShort(short value) + { + ByteBufferHelper.putShort(this, value, order()); + return this; + } + + public short getShort(int index) + { + return ByteBufferHelper.getShort(this, index, order()); + } + + public ByteBuffer putShort(int index, short value) + { + ByteBufferHelper.putShort(this, index, value, order()); + return this; + } + + public int getInt() + { + return ByteBufferHelper.getInt(this, order()); + } + + public ByteBuffer putInt(int value) + { + ByteBufferHelper.putInt(this, value, order()); + return this; + } + + public int getInt(int index) + { + return ByteBufferHelper.getInt(this, index, order()); + } + + public ByteBuffer putInt(int index, int value) + { + ByteBufferHelper.putInt(this, index, value, order()); + return this; + } + + public long getLong() + { + return ByteBufferHelper.getLong(this, order()); + } + + public ByteBuffer putLong(long value) + { + ByteBufferHelper.putLong(this, value, order()); + return this; + } + + public long getLong(int index) + { + return ByteBufferHelper.getLong(this, index, order()); + } + + public ByteBuffer putLong(int index, long value) + { + ByteBufferHelper.putLong(this, index, value, order()); + return this; + } + + public float getFloat() + { + return ByteBufferHelper.getFloat(this, order()); + } + + public ByteBuffer putFloat(float value) + { + ByteBufferHelper.putFloat(this, value, order()); + return this; + } + + public float getFloat(int index) + { + return ByteBufferHelper.getFloat(this, index, order()); + } + + public ByteBuffer putFloat(int index, float value) + { + ByteBufferHelper.putFloat(this, index, value, order()); + return this; + } + + public double getDouble() + { + return ByteBufferHelper.getDouble(this, order()); + } + + public ByteBuffer putDouble(double value) + { + ByteBufferHelper.putDouble(this, value, order()); + return this; + } + + public double getDouble(int index) + { + return ByteBufferHelper.getDouble(this, index, order()); + } + + public ByteBuffer putDouble(int index, double value) + { + ByteBufferHelper.putDouble(this, index, value, order()); + return this; + } +} diff --git a/libjava/classpath/java/nio/DoubleBuffer.java b/libjava/classpath/java/nio/DoubleBuffer.java new file mode 100644 index 000000000..c17058c44 --- /dev/null +++ b/libjava/classpath/java/nio/DoubleBuffer.java @@ -0,0 +1,386 @@ +/* DoubleBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Change gnu.classpath.Pointer to RawData +import gnu.gcj.RawData; + +/** + * @since 1.4 + */ +public abstract class DoubleBuffer extends Buffer + implements Comparable<DoubleBuffer> +{ + final int array_offset; + final double[] backing_buffer; + + DoubleBuffer (int capacity, int limit, int position, int mark, + RawData address, double[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>DoubleBuffer</code> object with a given capacity. + */ + public static DoubleBuffer allocate (int capacity) + { + return new DoubleBufferImpl (capacity); + } + + /** + * Wraps a <code>double</code> array into a <code>DoubleBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final DoubleBuffer wrap (double[] array, int offset, int length) + { + return new DoubleBufferImpl (array, 0, array.length, offset + length, offset, -1, false); + } + + /** + * Wraps a <code>double</code> array into a <code>DoubleBuffer</code> + * object. + */ + public static final DoubleBuffer wrap (double[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>double</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>double</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>double</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>double</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public DoubleBuffer get (double[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>double</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>double</code>s remaining in this buffer. + */ + public DoubleBuffer get (double[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>DoubleBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>double</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public DoubleBuffer put (DoubleBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining ()); + + if (src.remaining () > 0) + { + double[] toPut = new double [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>double array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>double</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public DoubleBuffer put (double[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>double array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>double</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final DoubleBuffer put (double[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>double</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>double</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final double[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>long</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data, in Double.doubleToLongBits() form + * Note that the hashcode is dependent on buffer content, + * and therefore is not useful if the buffer content may change. + * + * @return the hash code (casted to int) + */ + public int hashCode () + { + long hashCode = Double.doubleToLongBits(get(position())) + 31; + long multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (Double.doubleToLongBits(get(i)) + 30)*multiplier; + } + return ((int)hashCode); + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof DoubleBuffer) + { + return compareTo ((DoubleBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>DoubleBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>DoubleBuffer</code>. + */ + public int compareTo (DoubleBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + double a = get(pos_this++); + double b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>double</code>s in this buffer. + */ + public abstract double get (); + + /** + * Writes the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>double</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract DoubleBuffer put (double b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract double get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract DoubleBuffer put (int index, double b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract DoubleBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>DoubleBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract DoubleBuffer slice (); + + /** + * Creates a new <code>DoubleBuffer</code> that shares this buffer's + * content. + */ + public abstract DoubleBuffer duplicate (); + + /** + * Creates a new read-only <code>DoubleBuffer</code> that shares this + * buffer's content. + */ + public abstract DoubleBuffer asReadOnlyBuffer (); +} diff --git a/libjava/classpath/java/nio/DoubleBufferImpl.java b/libjava/classpath/java/nio/DoubleBufferImpl.java new file mode 100644 index 000000000..160841353 --- /dev/null +++ b/libjava/classpath/java/nio/DoubleBufferImpl.java @@ -0,0 +1,173 @@ +/* DoubleBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class DoubleBufferImpl extends DoubleBuffer +{ + private final boolean readOnly; + + DoubleBufferImpl (int capacity) + { + this (new double [capacity], 0, capacity, capacity, 0, -1, false); + } + + DoubleBufferImpl (double[] buffer, int offset, int capacity, int limit, + int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public boolean isReadOnly () + { + return readOnly; + } + + public DoubleBuffer slice () + { + return new DoubleBufferImpl (backing_buffer, array_offset + position (), + remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public DoubleBuffer duplicate () + { + return new DoubleBufferImpl (backing_buffer, array_offset, capacity (), + limit (), position (), mark, isReadOnly ()); + } + + public DoubleBuffer asReadOnlyBuffer () + { + return new DoubleBufferImpl (backing_buffer, array_offset, capacity (), + limit (), position (), mark, true); + } + + public DoubleBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>double</code>s in this buffer. + */ + public double get () + { + checkForUnderflow(); + + double result = backing_buffer [position ()]; + position (position () + 1); + return result; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public DoubleBuffer put (double value) + { + checkIfReadOnly(); + checkForOverflow(); + + backing_buffer [position ()] = value; + position (position () + 1); + return this; + } + + /** + * Absolute get method. Reads the <code>double</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public double get (int index) + { + checkIndex(index); + + return backing_buffer [index]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public DoubleBuffer put (int index, double value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/DoubleViewBufferImpl.java b/libjava/classpath/java/nio/DoubleViewBufferImpl.java new file mode 100644 index 000000000..a78bf2738 --- /dev/null +++ b/libjava/classpath/java/nio/DoubleViewBufferImpl.java @@ -0,0 +1,170 @@ +/* DoubleViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +final class DoubleViewBufferImpl extends DoubleBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + DoubleViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()) : null, null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public DoubleViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()) : null, null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>double</code>s in this buffer. + */ + public double get () + { + int p = position(); + double result = ByteBufferHelper.getDouble(bb, (p << 3) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>double</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public double get (int index) + { + return ByteBufferHelper.getDouble(bb, (index << 3) + offset, endian); + } + + public DoubleBuffer put (double value) + { + int p = position(); + ByteBufferHelper.putDouble(bb, (p << 3) + offset, value, endian); + position(p + 1); + return this; + } + + public DoubleBuffer put (int index, double value) + { + ByteBufferHelper.putDouble(bb, (index << 3) + offset, value, endian); + return this; + } + + public DoubleBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 8 * position(), 8 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public DoubleBuffer slice () + { + return new DoubleViewBufferImpl (bb, (position () << 3) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); + } + + DoubleBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new DoubleViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public DoubleBuffer duplicate () + { + return duplicate(readOnly); + } + + public DoubleBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/FloatBuffer.java b/libjava/classpath/java/nio/FloatBuffer.java new file mode 100644 index 000000000..3459cf12b --- /dev/null +++ b/libjava/classpath/java/nio/FloatBuffer.java @@ -0,0 +1,386 @@ +/* FloatBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Change gnu.classpath.Pointer to RawData +import gnu.gcj.RawData; + +/** + * @since 1.4 + */ +public abstract class FloatBuffer extends Buffer + implements Comparable<FloatBuffer> +{ + final int array_offset; + final float[] backing_buffer; + + FloatBuffer (int capacity, int limit, int position, int mark, + RawData address, float[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>FloatBuffer</code> object with a given capacity. + */ + public static FloatBuffer allocate (int capacity) + { + return new FloatBufferImpl (capacity); + } + + /** + * Wraps a <code>float</code> array into a <code>FloatBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final FloatBuffer wrap (float[] array, int offset, int length) + { + return new FloatBufferImpl (array, 0, array.length, offset + length, offset, -1, false); + } + + /** + * Wraps a <code>float</code> array into a <code>FloatBuffer</code> + * object. + */ + public static final FloatBuffer wrap (float[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>float</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>float</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>float</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>float</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public FloatBuffer get (float[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>float</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>float</code>s remaining in this buffer. + */ + public FloatBuffer get (float[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>FloatBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>float</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public FloatBuffer put (FloatBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining()); + + if (src.remaining () > 0) + { + float[] toPut = new float [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>float array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>float</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public FloatBuffer put (float[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>float array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>float</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final FloatBuffer put (float[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>float</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>float</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final float[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>int</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data, in Float.floatToIntBits() form + * Note that the hashcode is dependent on buffer content, + * and therefore is not useful if the buffer content may change. + * + * @return the hash code + */ + public int hashCode () + { + int hashCode = Float.floatToIntBits(get(position())) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (Float.floatToIntBits(get(i)) + 30)*multiplier; + } + return hashCode; + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof FloatBuffer) + { + return compareTo ((FloatBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>FloatBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>FloatBuffer</code>. + */ + public int compareTo (FloatBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + float a = get(pos_this++); + float b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>float</code>s in this buffer. + */ + public abstract float get (); + + /** + * Writes the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>float</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract FloatBuffer put (float b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract float get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract FloatBuffer put (int index, float b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract FloatBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>FloatBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract FloatBuffer slice (); + + /** + * Creates a new <code>FloatBuffer</code> that shares this buffer's + * content. + */ + public abstract FloatBuffer duplicate (); + + /** + * Creates a new read-only <code>FloatBuffer</code> that shares this + * buffer's content. + */ + public abstract FloatBuffer asReadOnlyBuffer (); +} diff --git a/libjava/classpath/java/nio/FloatBufferImpl.java b/libjava/classpath/java/nio/FloatBufferImpl.java new file mode 100644 index 000000000..603832d7a --- /dev/null +++ b/libjava/classpath/java/nio/FloatBufferImpl.java @@ -0,0 +1,170 @@ +/* FloatBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class FloatBufferImpl extends FloatBuffer +{ + private final boolean readOnly; + + FloatBufferImpl (int capacity) + { + this (new float [capacity], 0, capacity, capacity, 0, -1, false); + } + + FloatBufferImpl (float[] buffer, int offset, int capacity, int limit, + int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public boolean isReadOnly () + { + return readOnly; + } + + public FloatBuffer slice () + { + return new FloatBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public FloatBuffer duplicate () + { + return new FloatBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ()); + } + + public FloatBuffer asReadOnlyBuffer () + { + return new FloatBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true); + } + + public FloatBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>floats</code> in this buffer. + */ + public float get () + { + checkForUnderflow(); + + float result = backing_buffer [position ()]; + position (position () + 1); + return result; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public FloatBuffer put (float value) + { + checkIfReadOnly(); + checkForOverflow(); + + backing_buffer [position ()] = value; + position (position () + 1); + return this; + } + + /** + * Absolute get method. Reads the <code>float</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public float get (int index) + { + checkIndex(index); + + return backing_buffer [index]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public FloatBuffer put (int index, float value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/FloatViewBufferImpl.java b/libjava/classpath/java/nio/FloatViewBufferImpl.java new file mode 100644 index 000000000..663b65c4e --- /dev/null +++ b/libjava/classpath/java/nio/FloatViewBufferImpl.java @@ -0,0 +1,171 @@ +/* FloatViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +final class FloatViewBufferImpl extends FloatBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + FloatViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()):null, null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public FloatViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, offset):null, null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>floats</code> in this buffer. + */ + public float get () + { + int p = position(); + float result = ByteBufferHelper.getFloat(bb, (p << 2) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>float</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public float get (int index) + { + return ByteBufferHelper.getFloat(bb, (index << 2) + offset, endian); + } + + public FloatBuffer put (float value) + { + int p = position(); + ByteBufferHelper.putFloat(bb, (p << 2) + offset, value, endian); + position(p + 1); + return this; + } + + public FloatBuffer put (int index, float value) + { + ByteBufferHelper.putFloat(bb, (index << 2) + offset, value, endian); + return this; + } + + public FloatBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 4 * position(), 4 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public FloatBuffer slice () + { + // Create a sliced copy of this object that shares its content. + return new FloatViewBufferImpl (bb, (position () << 2) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); + } + + FloatBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new FloatViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public FloatBuffer duplicate () + { + return duplicate(readOnly); + } + + public FloatBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/IntBuffer.java b/libjava/classpath/java/nio/IntBuffer.java new file mode 100644 index 000000000..597886d9a --- /dev/null +++ b/libjava/classpath/java/nio/IntBuffer.java @@ -0,0 +1,387 @@ +/* IntBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Change gnu.classpath.Pointer to RawData +import gnu.gcj.RawData; + +/** + * @since 1.4 + */ +public abstract class IntBuffer extends Buffer + implements Comparable<IntBuffer> +{ + final int array_offset; + final int[] backing_buffer; + + IntBuffer (int capacity, int limit, int position, int mark, + RawData address, int[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>IntBuffer</code> object with a given capacity. + */ + public static IntBuffer allocate (int capacity) + { + return new IntBufferImpl (capacity); + } + + /** + * Wraps a <code>int</code> array into a <code>IntBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final IntBuffer wrap (int[] array, int offset, int length) + { + return new IntBufferImpl (array, 0, array.length, offset + length, offset, + -1, false); + } + + /** + * Wraps a <code>int</code> array into a <code>IntBuffer</code> + * object. + */ + public static final IntBuffer wrap (int[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>int</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>int</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>int</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>int</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public IntBuffer get (int[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>int</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>int</code>s remaining in this buffer. + */ + public IntBuffer get (int[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>IntBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>int</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public IntBuffer put (IntBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining ()); + + if (src.remaining () > 0) + { + int[] toPut = new int [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>int array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>int</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public IntBuffer put (int[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>int array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>int</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final IntBuffer put (int[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>int</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>int</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>int</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code + */ + public int hashCode () + { + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof IntBuffer) + { + return compareTo ((IntBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>IntBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>IntBuffer</code>. + */ + public int compareTo (IntBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + int a = get(pos_this++); + int b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>int</code>s in this buffer. + */ + public abstract int get (); + + /** + * Writes the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>int</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract IntBuffer put (int b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract int get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract IntBuffer put (int index, int b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract IntBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>IntBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract IntBuffer slice (); + + /** + * Creates a new <code>IntBuffer</code> that shares this buffer's + * content. + */ + public abstract IntBuffer duplicate (); + + /** + * Creates a new read-only <code>IntBuffer</code> that shares this + * buffer's content. + */ + public abstract IntBuffer asReadOnlyBuffer (); +} diff --git a/libjava/classpath/java/nio/IntBufferImpl.java b/libjava/classpath/java/nio/IntBufferImpl.java new file mode 100644 index 000000000..1b31b0795 --- /dev/null +++ b/libjava/classpath/java/nio/IntBufferImpl.java @@ -0,0 +1,169 @@ +/* IntBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class IntBufferImpl extends IntBuffer +{ + private final boolean readOnly; + + IntBufferImpl (int capacity) + { + this (new int [capacity], 0, capacity, capacity, 0, -1, false); + } + + IntBufferImpl (int[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public boolean isReadOnly () + { + return readOnly; + } + + public IntBuffer slice () + { + return new IntBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public IntBuffer duplicate () + { + return new IntBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ()); + } + + public IntBuffer asReadOnlyBuffer () + { + return new IntBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true); + } + + public IntBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>ints</code> in this buffer. + */ + public int get () + { + checkForUnderflow(); + + int result = backing_buffer [position ()]; + position (position () + 1); + return result; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public IntBuffer put (int value) + { + checkIfReadOnly(); + checkForOverflow(); + + backing_buffer [position ()] = value; + position (position () + 1); + return this; + } + + /** + * Absolute get method. Reads the <code>int</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public int get (int index) + { + checkIndex(index); + + return backing_buffer [index]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public IntBuffer put (int index, int value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/IntViewBufferImpl.java b/libjava/classpath/java/nio/IntViewBufferImpl.java new file mode 100644 index 000000000..dd3d6941a --- /dev/null +++ b/libjava/classpath/java/nio/IntViewBufferImpl.java @@ -0,0 +1,171 @@ +/* IntViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +final class IntViewBufferImpl extends IntBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + IntViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()):null, null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public IntViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, offset):null, null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>ints</code> in this buffer. + */ + public int get () + { + int p = position(); + int result = ByteBufferHelper.getInt(bb, (p << 2) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>int</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public int get (int index) + { + return ByteBufferHelper.getInt(bb, (index << 2) + offset, endian); + } + + public IntBuffer put (int value) + { + int p = position(); + ByteBufferHelper.putInt(bb, (p << 2) + offset, value, endian); + position(p + 1); + return this; + } + + public IntBuffer put (int index, int value) + { + ByteBufferHelper.putInt(bb, (index << 2) + offset, value, endian); + return this; + } + + public IntBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 4 * position(), 4 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public IntBuffer slice () + { + // Create a sliced copy of this object that shares its content. + return new IntViewBufferImpl (bb, (position () << 2) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); + } + + IntBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new IntViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public IntBuffer duplicate () + { + return duplicate(readOnly); + } + + public IntBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/InvalidMarkException.java b/libjava/classpath/java/nio/InvalidMarkException.java new file mode 100644 index 000000000..e13d76f1f --- /dev/null +++ b/libjava/classpath/java/nio/InvalidMarkException.java @@ -0,0 +1,54 @@ +/* InvalidMarkException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio; + +/** + * @author Michael Koch + * @since 1.4 + */ +public class InvalidMarkException extends IllegalStateException +{ + private static final long serialVersionUID = 1698329710438510774L; + + /** + * Creates the exception + */ + public InvalidMarkException () + { + } +} diff --git a/libjava/classpath/java/nio/LongBuffer.java b/libjava/classpath/java/nio/LongBuffer.java new file mode 100644 index 000000000..b6e94e8a8 --- /dev/null +++ b/libjava/classpath/java/nio/LongBuffer.java @@ -0,0 +1,386 @@ +/* LongBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Change gnu.classpath.Pointer to RawData +import gnu.gcj.RawData; + +/** + * @since 1.4 + */ +public abstract class LongBuffer extends Buffer + implements Comparable<LongBuffer> +{ + final int array_offset; + final long[] backing_buffer; + + LongBuffer (int capacity, int limit, int position, int mark, + RawData address, long[] backing_buffer, int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>LongBuffer</code> object with a given capacity. + */ + public static LongBuffer allocate (int capacity) + { + return new LongBufferImpl (capacity); + } + + /** + * Wraps a <code>long</code> array into a <code>LongBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final LongBuffer wrap (long[] array, int offset, int length) + { + return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false); + } + + /** + * Wraps a <code>long</code> array into a <code>LongBuffer</code> + * object. + */ + public static final LongBuffer wrap (long[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>long</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>long</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>long</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>long</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public LongBuffer get (long[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>long</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>long</code>s remaining in this buffer. + */ + public LongBuffer get (long[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>LongBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>long</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public LongBuffer put (LongBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining ()); + + if (src.remaining () > 0) + { + long[] toPut = new long [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>long array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>long</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public LongBuffer put (long[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>long array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>long</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final LongBuffer put (long[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>long</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>long</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final long[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>long</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code (casted to int) + */ + public int hashCode () + { + long hashCode = get(position()) + 31; + long multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return ((int)hashCode); + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof LongBuffer) + { + return compareTo ((LongBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>LongBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>LongBuffer</code>. + */ + public int compareTo (LongBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + long a = get(pos_this++); + long b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>long</code>s in this buffer. + */ + public abstract long get (); + + /** + * Writes the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>long</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract LongBuffer put (long b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract long get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract LongBuffer put (int index, long b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract LongBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>LongBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract LongBuffer slice (); + + /** + * Creates a new <code>LongBuffer</code> that shares this buffer's + * content. + */ + public abstract LongBuffer duplicate (); + + /** + * Creates a new read-only <code>LongBuffer</code> that shares this + * buffer's content. + */ + public abstract LongBuffer asReadOnlyBuffer (); +} diff --git a/libjava/classpath/java/nio/LongBufferImpl.java b/libjava/classpath/java/nio/LongBufferImpl.java new file mode 100644 index 000000000..85502c194 --- /dev/null +++ b/libjava/classpath/java/nio/LongBufferImpl.java @@ -0,0 +1,173 @@ +/* LongBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class LongBufferImpl extends LongBuffer +{ + private final boolean readOnly; + + LongBufferImpl (int capacity) + { + this (new long [capacity], 0, capacity, capacity, 0, -1, false); + } + + LongBufferImpl (long[] buffer, int offset, int capacity, int limit, + int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public boolean isReadOnly () + { + return readOnly; + } + + public LongBuffer slice () + { + return new LongBufferImpl (backing_buffer, array_offset + position (), + remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public LongBuffer duplicate () + { + return new LongBufferImpl (backing_buffer, array_offset, capacity (), limit (), + position (), mark, isReadOnly ()); + } + + public LongBuffer asReadOnlyBuffer () + { + return new LongBufferImpl (backing_buffer, array_offset, capacity (), limit (), + position (), mark, true); + } + + public LongBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>longs</code> in this buffer. + */ + public long get () + { + checkForUnderflow(); + + long result = backing_buffer [position ()]; + position (position () + 1); + return result; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public LongBuffer put (long value) + { + checkIfReadOnly(); + checkForOverflow(); + + backing_buffer [position ()] = value; + position (position () + 1); + return this; + } + + /** + * Absolute get method. Reads the <code>long</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public long get (int index) + { + checkIndex(index); + + return backing_buffer [index]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public LongBuffer put (int index, long value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/LongViewBufferImpl.java b/libjava/classpath/java/nio/LongViewBufferImpl.java new file mode 100644 index 000000000..a0a7cd36a --- /dev/null +++ b/libjava/classpath/java/nio/LongViewBufferImpl.java @@ -0,0 +1,171 @@ +/* LongViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +final class LongViewBufferImpl extends LongBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + LongViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()):null, null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public LongViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, offset):null, null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>longs</code> in this buffer. + */ + public long get () + { + int p = position(); + long result = ByteBufferHelper.getLong(bb, (p << 3) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>long</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public long get (int index) + { + return ByteBufferHelper.getLong(bb, (index << 3) + offset, endian); + } + + public LongBuffer put (long value) + { + int p = position(); + ByteBufferHelper.putLong(bb, (p << 3) + offset, value, endian); + position(p + 1); + return this; + } + + public LongBuffer put (int index, long value) + { + ByteBufferHelper.putLong(bb, (index << 3) + offset, value, endian); + return this; + } + + public LongBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 8 * position(), 8 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public LongBuffer slice () + { + // Create a sliced copy of this object that shares its content. + return new LongViewBufferImpl (bb, (position () << 3) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); + } + + LongBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new LongViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public LongBuffer duplicate () + { + return duplicate(readOnly); + } + + public LongBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/MappedByteBuffer.java b/libjava/classpath/java/nio/MappedByteBuffer.java new file mode 100644 index 000000000..f71e63051 --- /dev/null +++ b/libjava/classpath/java/nio/MappedByteBuffer.java @@ -0,0 +1,97 @@ +/* MappedByteBuffer.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer +import gnu.gcj.RawData; + +/** + * @author Michael Koch (konqueror@gmx.de) + * @since 1.4 + */ +public abstract class MappedByteBuffer extends ByteBuffer +{ + MappedByteBuffer (int capacity, int limit, int position, int mark, + RawData address) + { + super (capacity, limit, position, mark, address, null, 0); + } + + void forceImpl() + { + } + + public final MappedByteBuffer force () + { + forceImpl(); + return this; + } + + boolean isLoadedImpl() + { + load(); + return true; + } + + public final boolean isLoaded () + { + return isLoadedImpl(); + } + + void loadImpl() + { + } + + public final MappedByteBuffer load () + { + loadImpl(); + return this; + } + + void unmapImpl () + { + forceImpl(); + } + + protected void finalize() + throws Throwable + { + unmapImpl(); + } +} diff --git a/libjava/classpath/java/nio/MappedByteBufferImpl.java b/libjava/classpath/java/nio/MappedByteBufferImpl.java new file mode 100644 index 000000000..58ea635cd --- /dev/null +++ b/libjava/classpath/java/nio/MappedByteBufferImpl.java @@ -0,0 +1,359 @@ +/* MappedByteBufferImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +import gnu.classpath.Pointer; + +import java.io.IOException; + +final class MappedByteBufferImpl extends MappedByteBuffer +{ + private final boolean readOnly; + + /** Posix uses this for the pointer returned by mmap; + * Win32 uses it for the pointer returned by MapViewOfFile. */ + public Pointer implPtr; + /** Posix uses this for the actual length passed to mmap; + * Win32 uses it for the pointer returned by CreateFileMapping. */ + public long implLen; + + public MappedByteBufferImpl(Pointer address, int size, boolean readOnly) + throws IOException + { + super(size, size, 0, -1, address); + this.readOnly = readOnly; + } + + public boolean isReadOnly() + { + return readOnly; + } + + public byte get() + { + checkForUnderflow(); + + int pos = position(); + byte result = VMDirectByteBuffer.get(address, pos); + position(pos + 1); + return result; + } + + public ByteBuffer put(byte value) + { + checkIfReadOnly(); + checkForOverflow(); + + int pos = position(); + VMDirectByteBuffer.put(address, pos, value); + position(pos + 1); + return this; + } + + public byte get(int index) + { + checkIndex(index); + + return VMDirectByteBuffer.get(address, index); + } + + public ByteBuffer get(byte[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + int index = position(); + VMDirectByteBuffer.get(address, index, dst, offset, length); + position(index+length); + + return this; + } + + public ByteBuffer put(int index, byte value) + { + checkIfReadOnly(); + checkIndex(index); + + VMDirectByteBuffer.put(address, index, value); + return this; + } + + public ByteBuffer compact() + { + checkIfReadOnly(); + mark = -1; + int pos = position(); + if (pos > 0) + { + int count = remaining(); + // Call shiftDown method optimized for direct buffers. + VMDirectByteBuffer.shiftDown(address, 0, pos, count); + position(count); + limit(capacity()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public boolean isDirect() + { + return true; + } + + public ByteBuffer slice() + { + int rem = remaining(); + if (isReadOnly()) + return new DirectByteBufferImpl.ReadOnly + (this, VMDirectByteBuffer.adjustAddress(address, position()), + rem, rem, 0); + else + return new DirectByteBufferImpl.ReadWrite + (this, VMDirectByteBuffer.adjustAddress(address, position()), + rem, rem, 0); + } + + private ByteBuffer duplicate(boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + DirectByteBufferImpl result; + if (readOnly) + result = new DirectByteBufferImpl.ReadOnly(this, address, capacity(), + limit(), pos); + else + result = new DirectByteBufferImpl.ReadWrite(this, address, capacity(), + limit(), pos); + + if (mark != pos) + { + result.position(mark); + result.mark(); + result.position(pos); + } + return result; + } + + public ByteBuffer duplicate() + { + return duplicate(isReadOnly()); + } + + public ByteBuffer asReadOnlyBuffer() + { + return duplicate(true); + } + + public CharBuffer asCharBuffer() + { + return new CharViewBufferImpl(this, remaining() >> 1); + } + + public ShortBuffer asShortBuffer() + { + return new ShortViewBufferImpl(this, remaining() >> 1); + } + + public IntBuffer asIntBuffer() + { + return new IntViewBufferImpl(this, remaining() >> 2); + } + + public LongBuffer asLongBuffer() + { + return new LongViewBufferImpl(this, remaining() >> 3); + } + + public FloatBuffer asFloatBuffer() + { + return new FloatViewBufferImpl(this, remaining() >> 2); + } + + public DoubleBuffer asDoubleBuffer() + { + return new DoubleViewBufferImpl(this, remaining() >> 3); + } + + public char getChar() + { + return ByteBufferHelper.getChar(this, order()); + } + + public ByteBuffer putChar(char value) + { + ByteBufferHelper.putChar(this, value, order()); + return this; + } + + public char getChar(int index) + { + return ByteBufferHelper.getChar(this, index, order()); + } + + public ByteBuffer putChar(int index, char value) + { + ByteBufferHelper.putChar(this, index, value, order()); + return this; + } + + public short getShort() + { + return ByteBufferHelper.getShort(this, order()); + } + + public ByteBuffer putShort(short value) + { + ByteBufferHelper.putShort(this, value, order()); + return this; + } + + public short getShort(int index) + { + return ByteBufferHelper.getShort(this, index, order()); + } + + public ByteBuffer putShort(int index, short value) + { + ByteBufferHelper.putShort(this, index, value, order()); + return this; + } + + public int getInt() + { + return ByteBufferHelper.getInt(this, order()); + } + + public ByteBuffer putInt(int value) + { + ByteBufferHelper.putInt(this, value, order()); + return this; + } + + public int getInt(int index) + { + return ByteBufferHelper.getInt(this, index, order()); + } + + public ByteBuffer putInt(int index, int value) + { + ByteBufferHelper.putInt(this, index, value, order()); + return this; + } + + public long getLong() + { + return ByteBufferHelper.getLong(this, order()); + } + + public ByteBuffer putLong(long value) + { + ByteBufferHelper.putLong(this, value, order()); + return this; + } + + public long getLong(int index) + { + return ByteBufferHelper.getLong(this, index, order()); + } + + public ByteBuffer putLong(int index, long value) + { + ByteBufferHelper.putLong(this, index, value, order()); + return this; + } + + public float getFloat() + { + return ByteBufferHelper.getFloat(this, order()); + } + + public ByteBuffer putFloat(float value) + { + ByteBufferHelper.putFloat(this, value, order()); + return this; + } + + public float getFloat(int index) + { + return ByteBufferHelper.getFloat(this, index, order()); + } + + public ByteBuffer putFloat(int index, float value) + { + ByteBufferHelper.putFloat(this, index, value, order()); + return this; + } + + public double getDouble() + { + return ByteBufferHelper.getDouble(this, order()); + } + + public ByteBuffer putDouble(double value) + { + ByteBufferHelper.putDouble(this, value, order()); + return this; + } + + public double getDouble(int index) + { + return ByteBufferHelper.getDouble(this, index, order()); + } + + public ByteBuffer putDouble(int index, double value) + { + ByteBufferHelper.putDouble(this, index, value, order()); + return this; + } + + // NOTE: In libgcj these methods are implemented in natFileChannelXxx.cc, + // because they're small, and to put them next to FileChannelImpl::mapImpl. + native void unmapImpl(); + native boolean isLoadedImpl(); + // FIXME: Try to load all pages into memory. + native void loadImpl(); + + native void forceImpl(); +} diff --git a/libjava/classpath/java/nio/ReadOnlyBufferException.java b/libjava/classpath/java/nio/ReadOnlyBufferException.java new file mode 100644 index 000000000..ea58c375b --- /dev/null +++ b/libjava/classpath/java/nio/ReadOnlyBufferException.java @@ -0,0 +1,54 @@ +/* ReadOnlyBufferException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio; + +/** + * @author Michael Koch + * @since 1.4 + */ +public class ReadOnlyBufferException extends UnsupportedOperationException +{ + private static final long serialVersionUID = - 1210063976496234090L; + + /** + * Creates the exception + */ + public ReadOnlyBufferException () + { + } +} diff --git a/libjava/classpath/java/nio/ShortBuffer.java b/libjava/classpath/java/nio/ShortBuffer.java new file mode 100644 index 000000000..e7cb12167 --- /dev/null +++ b/libjava/classpath/java/nio/ShortBuffer.java @@ -0,0 +1,387 @@ +/* ShortBuffer.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +// GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer +import gnu.gcj.RawData; + +/** + * @since 1.4 + */ +public abstract class ShortBuffer extends Buffer + implements Comparable<ShortBuffer> +{ + final int array_offset; + final short[] backing_buffer; + + ShortBuffer (int capacity, int limit, int position, + int mark, RawData address, short[] backing_buffer, + int array_offset) + { + super (capacity, limit, position, mark, address); + this.backing_buffer = backing_buffer; + this.array_offset = array_offset; + } + + /** + * Allocates a new <code>ShortBuffer</code> object with a given capacity. + */ + public static ShortBuffer allocate (int capacity) + { + return new ShortBufferImpl (capacity); + } + + /** + * Wraps a <code>short</code> array into a <code>ShortBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + public static final ShortBuffer wrap (short[] array, int offset, int length) + { + return new ShortBufferImpl (array, 0, array.length, offset + length, offset, -1, false); + } + + /** + * Wraps a <code>short</code> array into a <code>ShortBuffer</code> + * object. + */ + public static final ShortBuffer wrap (short[] array) + { + return wrap (array, 0, array.length); + } + + /** + * This method transfers <code>short</code>s from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length <code>short</code>s remaining in this buffer. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>short</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>short</code>s remaining in this buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + */ + public ShortBuffer get (short[] dst, int offset, int length) + { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + + for (int i = offset; i < offset + length; i++) + { + dst [i] = get (); + } + + return this; + } + + /** + * This method transfers <code>short</code>s from this buffer into the given + * destination array. + * + * @param dst The byte array to write into. + * + * @exception BufferUnderflowException If there are fewer than dst.length + * <code>short</code>s remaining in this buffer. + */ + public ShortBuffer get (short[] dst) + { + return get (dst, 0, dst.length); + } + + /** + * Writes the content of the the <code>ShortBUFFER</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * <code>src.remaining()</code> space remaining in this buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>short</code>s in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ShortBuffer put (ShortBuffer src) + { + if (src == this) + throw new IllegalArgumentException (); + + checkForOverflow(src.remaining ()); + + if (src.remaining () > 0) + { + short[] toPut = new short [src.remaining ()]; + src.get (toPut); + put (toPut); + } + + return this; + } + + /** + * Writes the content of the the <code>short array</code> src + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>short</code>s in the source array. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ShortBuffer put (short[] src, int offset, int length) + { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + + for (int i = offset; i < offset + length; i++) + put (src [i]); + + return this; + } + + /** + * Writes the content of the the <code>short array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>short</code>s in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final ShortBuffer put (short[] src) + { + return put (src, 0, src.length); + } + + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>short</code> array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !isReadOnly ()); + } + + /** + * Returns the <code>short</code> array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final short[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final int arrayOffset () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + checkIfReadOnly(); + + return array_offset; + } + + /** + * Calculates a hash code for this buffer. + * + * This is done with <code>int</code> arithmetic, + * where ** represents exponentiation, by this formula:<br> + * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1)</code>. + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code + */ + public int hashCode () + { + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; + } + + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) + { + if (obj instanceof ShortBuffer) + { + return compareTo ((ShortBuffer) obj) == 0; + } + + return false; + } + + /** + * Compares two <code>ShortBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>ShortBuffer</code>. + */ + public int compareTo (ShortBuffer other) + { + int num = Math.min(remaining(), other.remaining()); + int pos_this = position(); + int pos_other = other.position(); + + for (int count = 0; count < num; count++) + { + short a = get(pos_this++); + short b = other.get(pos_other++); + + if (a == b) + continue; + + if (a < b) + return -1; + + return 1; + } + + return remaining() - other.remaining(); + } + + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>short</code>s in this buffer. + */ + public abstract short get (); + + /** + * Writes the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>short</code>s in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ShortBuffer put (short b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract short get (int index); + + /** + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ShortBuffer put (int index, short b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ShortBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>ShortBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract ShortBuffer slice (); + + /** + * Creates a new <code>ShortBuffer</code> that shares this buffer's + * content. + */ + public abstract ShortBuffer duplicate (); + + /** + * Creates a new read-only <code>ShortBuffer</code> that shares this + * buffer's content. + */ + public abstract ShortBuffer asReadOnlyBuffer (); +} diff --git a/libjava/classpath/java/nio/ShortBufferImpl.java b/libjava/classpath/java/nio/ShortBufferImpl.java new file mode 100644 index 000000000..4d66ec907 --- /dev/null +++ b/libjava/classpath/java/nio/ShortBufferImpl.java @@ -0,0 +1,173 @@ +/* ShortBufferImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +/** + * This is a Heap memory implementation + */ +final class ShortBufferImpl extends ShortBuffer +{ + private final boolean readOnly; + + ShortBufferImpl (int capacity) + { + this (new short [capacity], 0, capacity, capacity, 0, -1, false); + } + + ShortBufferImpl (short[] buffer, int offset, int capacity, + int limit, int position, int mark, boolean readOnly) + { + super (capacity, limit, position, mark, null, buffer, offset); + this.readOnly = readOnly; + } + + public boolean isReadOnly () + { + return readOnly; + } + + public ShortBuffer slice () + { + return new ShortBufferImpl (backing_buffer, array_offset + position (), + remaining (), remaining (), 0, -1, isReadOnly ()); + } + + public ShortBuffer duplicate () + { + return new ShortBufferImpl (backing_buffer, array_offset, capacity (), + limit (), position (), mark, isReadOnly ()); + } + + public ShortBuffer asReadOnlyBuffer () + { + return new ShortBufferImpl (backing_buffer, array_offset, capacity (), limit (), + position (), mark, true); + } + + public ShortBuffer compact () + { + checkIfReadOnly(); + mark = -1; + int p = position(); + int n = limit() - p; + if (n > 0) + { + System.arraycopy(backing_buffer, array_offset + p, + backing_buffer, array_offset, n); + } + position(n); + limit(capacity()); + return this; + } + + public boolean isDirect () + { + return false; + } + + /** + * Reads the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>short</code>s in this buffer. + */ + public short get () + { + checkForUnderflow(); + + short result = backing_buffer [position ()]; + position (position () + 1); + return result; + } + + /** + * Relative put method. Writes <code>value</code> to the next position + * in the buffer. + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ShortBuffer put (short value) + { + checkIfReadOnly(); + checkForOverflow(); + + backing_buffer [position ()] = value; + position (position () + 1); + return this; + } + + /** + * Absolute get method. Reads the <code>short</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public short get (int index) + { + checkIndex(index); + + return backing_buffer [index]; + } + + /** + * Absolute put method. Writes <code>value</code> to position + * <code>index</code> in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public ShortBuffer put (int index, short value) + { + checkIfReadOnly(); + checkIndex(index); + + backing_buffer [index] = value; + return this; + } + + public ByteOrder order () + { + return ByteOrder.nativeOrder (); + } +} diff --git a/libjava/classpath/java/nio/ShortViewBufferImpl.java b/libjava/classpath/java/nio/ShortViewBufferImpl.java new file mode 100644 index 000000000..f7ef3e346 --- /dev/null +++ b/libjava/classpath/java/nio/ShortViewBufferImpl.java @@ -0,0 +1,173 @@ +/* ShortViewBufferImpl.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio; + +final class ShortViewBufferImpl extends ShortBuffer +{ + /** Position in bb (i.e. a byte offset) where this buffer starts. */ + private final int offset; + private final ByteBuffer bb; + private final boolean readOnly; + private final ByteOrder endian; + + ShortViewBufferImpl (ByteBuffer bb, int capacity) + { + super (capacity, capacity, 0, -1, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, bb.position()):null, + null, 0); + this.bb = bb; + this.offset = bb.position(); + this.readOnly = bb.isReadOnly(); + this.endian = bb.order(); + } + + public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity, + int limit, int position, int mark, + boolean readOnly, ByteOrder endian) + { + super (capacity, limit, position, mark, bb.isDirect() ? + VMDirectByteBuffer.adjustAddress(bb.address, offset):null, + null, 0); + this.bb = bb; + this.offset = offset; + this.readOnly = readOnly; + this.endian = endian; + } + + /** + * Reads the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>short</code>s in this buffer. + */ + public short get () + { + int p = position(); + short result = ByteBufferHelper.getShort(bb, (p << 1) + offset, endian); + position(p + 1); + return result; + } + + /** + * Absolute get method. Reads the <code>short</code> at position + * <code>index</code>. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public short get (int index) + { + return ByteBufferHelper.getShort(bb, (index << 1) + offset, endian); + } + + public ShortBuffer put (short value) + { + int p = position(); + ByteBufferHelper.putShort(bb, (p << 1) + offset, value, endian); + position(p + 1); + return this; + } + + public ShortBuffer put (int index, short value) + { + ByteBufferHelper.putShort(bb, (index << 1) + offset, value, endian); + return this; + } + + public ShortBuffer compact () + { + if (position () > 0) + { + int count = limit () - position (); + bb.shiftDown(offset, offset + 2 * position(), 2 * count); + position (count); + limit (capacity ()); + } + else + { + position(limit()); + limit(capacity()); + } + return this; + } + + public ShortBuffer slice () + { + // Create a sliced copy of this object that shares its content. + return new ShortViewBufferImpl (bb, (position () << 1) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); + } + + ShortBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new ShortViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public ShortBuffer duplicate () + { + return duplicate(readOnly); + } + + public ShortBuffer asReadOnlyBuffer () + { + return duplicate(true); + } + + public boolean isReadOnly () + { + return readOnly; + } + + public boolean isDirect () + { + return bb.isDirect (); + } + + public ByteOrder order () + { + return endian; + } +} diff --git a/libjava/classpath/java/nio/channels/AlreadyConnectedException.java b/libjava/classpath/java/nio/channels/AlreadyConnectedException.java new file mode 100644 index 000000000..979486f06 --- /dev/null +++ b/libjava/classpath/java/nio/channels/AlreadyConnectedException.java @@ -0,0 +1,50 @@ +/* AlreadyConnectedException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +public class AlreadyConnectedException extends IllegalStateException +{ + private static final long serialVersionUID = - 7331895245053773357L; + + /** + * Creates the exception + */ + public AlreadyConnectedException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/AsynchronousCloseException.java b/libjava/classpath/java/nio/channels/AsynchronousCloseException.java new file mode 100644 index 000000000..31e41528b --- /dev/null +++ b/libjava/classpath/java/nio/channels/AsynchronousCloseException.java @@ -0,0 +1,55 @@ +/* AsynchronousCloseException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class AsynchronousCloseException extends ClosedChannelException +{ + private static final long serialVersionUID = 6891178312432313966L; + + /** + * Creates the exception + */ + public AsynchronousCloseException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/ByteChannel.java b/libjava/classpath/java/nio/channels/ByteChannel.java new file mode 100644 index 000000000..d7d7a451b --- /dev/null +++ b/libjava/classpath/java/nio/channels/ByteChannel.java @@ -0,0 +1,43 @@ +/* ByteChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +public interface ByteChannel extends ReadableByteChannel, + WritableByteChannel +{ +} diff --git a/libjava/classpath/java/nio/channels/CancelledKeyException.java b/libjava/classpath/java/nio/channels/CancelledKeyException.java new file mode 100644 index 000000000..d94e65053 --- /dev/null +++ b/libjava/classpath/java/nio/channels/CancelledKeyException.java @@ -0,0 +1,55 @@ +/* CancelledKeyException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class CancelledKeyException extends IllegalStateException +{ + private static final long serialVersionUID = - 8438032138028814268L; + + /** + * Creates the exception + */ + public CancelledKeyException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/Channel.java b/libjava/classpath/java/nio/channels/Channel.java new file mode 100644 index 000000000..33fcf3174 --- /dev/null +++ b/libjava/classpath/java/nio/channels/Channel.java @@ -0,0 +1,60 @@ +/* Channel.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio.channels; + +import java.io.IOException; +import java.io.Closeable; + +public interface Channel extends Closeable +{ + /** + * Tells whether this channel is open or not + * + * @return <code>true</code>if channel is open, + * <code>false</code> otherwise + */ + boolean isOpen(); + + /** + * Closes this channel + * + * @exception IOException If an error occurs + */ + void close() throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/Channels.java b/libjava/classpath/java/nio/channels/Channels.java new file mode 100644 index 000000000..382a3d705 --- /dev/null +++ b/libjava/classpath/java/nio/channels/Channels.java @@ -0,0 +1,144 @@ +/* Channels.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio.channels; + +import gnu.java.nio.ChannelReader; +import gnu.java.nio.ChannelWriter; +import gnu.java.nio.InputStreamChannel; +import gnu.java.nio.OutputStreamChannel; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.UnsupportedCharsetException; + + +/** + * @since 1.4 + */ +public final class Channels +{ + /** + * This class isn't intended to be instantiated. + */ + private Channels() + { + // Do nothing here. + } + + /** + * Constructs a stream that reads bytes from the given channel. + */ + public static InputStream newInputStream(ReadableByteChannel ch) + { + return VMChannels.newInputStream(ch); + } + + /** + * Constructs a stream that writes bytes to the given channel. + */ + public static OutputStream newOutputStream(WritableByteChannel ch) + { + return VMChannels.newOutputStream(ch); + } + + /** + * Constructs a channel that reads bytes from the given stream. + */ + public static ReadableByteChannel newChannel(InputStream in) + { + return new InputStreamChannel(in); + } + + /** + * Constructs a channel that writes bytes to the given stream. + */ + public static WritableByteChannel newChannel(OutputStream out) + { + return new OutputStreamChannel(out); + } + + /** + * Constructs a reader that decodes bytes from the given channel using the + * given decoder. + */ + public static Reader newReader(ReadableByteChannel ch, CharsetDecoder dec, + int minBufferCap) + { + return new ChannelReader(ch, dec, minBufferCap); + } + + /** + * Constructs a reader that decodes bytes from the given channel according to + * the named charset. + * + * @exception UnsupportedCharsetException If no support for the named charset + * is available in this instance of the Java virtual machine. + */ + public static Reader newReader(ReadableByteChannel ch, String csName) + { + return newReader(ch, Charset.forName(csName).newDecoder(), -1); + } + + /** + * Constructs a writer that encodes characters using the given encoder and + * writes the resulting bytes to the given channel. + */ + public static Writer newWriter(WritableByteChannel ch, CharsetEncoder enc, + int minBufferCap) + { + return new ChannelWriter(ch, enc, minBufferCap); + } + + /** + * Constructs a writer that encodes characters according to the named charset + * and writes the resulting bytes to the given channel. + * + * @exception UnsupportedCharsetException If no support for the named charset + * is available in this instance of the Java virtual machine. + */ + public static Writer newWriter(WritableByteChannel ch, String csName) + { + return newWriter(ch, Charset.forName(csName).newEncoder(), -1); + } +} diff --git a/libjava/classpath/java/nio/channels/ClosedByInterruptException.java b/libjava/classpath/java/nio/channels/ClosedByInterruptException.java new file mode 100644 index 000000000..ade7e2a27 --- /dev/null +++ b/libjava/classpath/java/nio/channels/ClosedByInterruptException.java @@ -0,0 +1,55 @@ +/* ClosedByInterruptException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class ClosedByInterruptException extends AsynchronousCloseException +{ + private static final long serialVersionUID = - 4488191543534286750L; + + /** + * Creates the exception + */ + public ClosedByInterruptException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/ClosedChannelException.java b/libjava/classpath/java/nio/channels/ClosedChannelException.java new file mode 100644 index 000000000..ea896a0fd --- /dev/null +++ b/libjava/classpath/java/nio/channels/ClosedChannelException.java @@ -0,0 +1,57 @@ +/* ClosedChannelException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class ClosedChannelException extends IOException +{ + private static final long serialVersionUID = 882777185433553857L; + + /** + * Creates the exception + */ + public ClosedChannelException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/ClosedSelectorException.java b/libjava/classpath/java/nio/channels/ClosedSelectorException.java new file mode 100644 index 000000000..9dfbd7bf8 --- /dev/null +++ b/libjava/classpath/java/nio/channels/ClosedSelectorException.java @@ -0,0 +1,55 @@ +/* ClosedSelectorException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class ClosedSelectorException extends IllegalStateException +{ + private static final long serialVersionUID = 6466297122317847835L; + + /** + * Creates the exception + */ + public ClosedSelectorException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/ConnectionPendingException.java b/libjava/classpath/java/nio/channels/ConnectionPendingException.java new file mode 100644 index 000000000..2e54ed61a --- /dev/null +++ b/libjava/classpath/java/nio/channels/ConnectionPendingException.java @@ -0,0 +1,55 @@ +/* ConnectionPendingException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class ConnectionPendingException extends IllegalStateException +{ + private static final long serialVersionUID = 2008393366501760879L; + + /** + * Creates the exception + */ + public ConnectionPendingException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/DatagramChannel.java b/libjava/classpath/java/nio/channels/DatagramChannel.java new file mode 100644 index 000000000..b8815f47c --- /dev/null +++ b/libjava/classpath/java/nio/channels/DatagramChannel.java @@ -0,0 +1,208 @@ +/* DatagramChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.net.DatagramSocket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.SelectorProvider; + + +/** + * @since 1.4 + */ +public abstract class DatagramChannel extends AbstractSelectableChannel + implements ByteChannel, ScatteringByteChannel, GatheringByteChannel +{ + /** + * Initializes the channel. + */ + protected DatagramChannel(SelectorProvider provider) + { + super(provider); + } + + /** + * Opens a datagram channel. + * + * @exception IOException If an error occurs + */ + public static DatagramChannel open() throws IOException + { + return SelectorProvider.provider().openDatagramChannel(); + } + + /** + * Reads data from this channel. + */ + public final long read(ByteBuffer[] dsts) throws IOException + { + long b = 0; + + for (int i = 0; i < dsts.length; i++) + b += read(dsts[i]); + + return b; + } + + /** + * Writes data to this channel. + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public final long write(ByteBuffer[] srcs) throws IOException + { + long b = 0; + + for (int i = 0; i < srcs.length; i++) + b += write(srcs[i]); + + return b; + } + + /** + * Connects this channel's socket. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the connect operation is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the read operation is in progress, thereby closing the + * channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an error occurs. + * @exception SecurityException If a security manager has been installed and + * it does not permit datagrams to be sent to the given address. + */ + public abstract DatagramChannel connect(SocketAddress remote) + throws IOException; + + /** + * Disonnects this channel's socket. + * + * @exception IOException If an error occurs + */ + public abstract DatagramChannel disconnect() throws IOException; + + /** + * Tells whether or not this channel's socket is connected. + * + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public abstract boolean isConnected(); + + /** + * Reads data from this channel. + */ + public abstract int read(ByteBuffer dst) throws IOException; + + /** + * Reads data from this channel. + * + * @exception IOException If an error occurs. + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public abstract long read(ByteBuffer[] dsts, int offset, int length) + throws IOException; + + /** + * Receives a datagram via this channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the connect operation is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the read operation is in progress, thereby closing the + * channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an error occurs + * @exception SecurityException If a security manager has been installed and + * it does not permit datagrams to be sent to the given address. + */ + public abstract SocketAddress receive(ByteBuffer dst) + throws IOException; + + /** + * Sends a datagram via this channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the connect operation is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the read operation is in progress, thereby closing the + * channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an error occurs + * @exception SecurityException If a security manager has been installed and + * it does not permit datagrams to be sent to the given address. + */ + public abstract int send(ByteBuffer src, SocketAddress target) + throws IOException; + + /** + * Retrieves the channel's socket. + */ + public abstract DatagramSocket socket(); + + /** + * Writes data to this channel. + * + * @exception IOException If an error occurs. + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public abstract int write(ByteBuffer src) throws IOException; + + /** + * Writes data to this channel. + * + * @exception IOException If an error occurs. + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public abstract long write(ByteBuffer[] srcs, int offset, int length) + throws IOException; + + /** + * Retrieves the valid operations for this channel. + * + * @exception NotYetConnectedException The channel's socket is not connected. + */ + public final int validOps() + { + return SelectionKey.OP_READ | SelectionKey.OP_WRITE; + } +} diff --git a/libjava/classpath/java/nio/channels/FileChannel.java b/libjava/classpath/java/nio/channels/FileChannel.java new file mode 100644 index 000000000..8c8029fe7 --- /dev/null +++ b/libjava/classpath/java/nio/channels/FileChannel.java @@ -0,0 +1,357 @@ +/* FileChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.spi.AbstractInterruptibleChannel; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class FileChannel extends AbstractInterruptibleChannel + implements ByteChannel, GatheringByteChannel, ScatteringByteChannel +{ + public static class MapMode + { + int m; + public static final MapMode READ_ONLY = new MapMode(0); + public static final MapMode READ_WRITE = new MapMode(1); + public static final MapMode PRIVATE = new MapMode(2); + + /** + * Initializes the MapMode. + */ + MapMode(int a) + { + m = a; + } + + /** + * Returns a string representation of the <code>MapMode</code> object. + */ + public String toString() + { + if (this == READ_ONLY) + return "READ_ONLY"; + else if (this == READ_WRITE) + return "READ_WRITE"; + + return "PRIVATE"; + } + } + + /** + * Initializes the channel. + */ + protected FileChannel() + { + } + + /** + * Maps the file into the memory. + * + * @exception IllegalArgumentException If the preconditions on the parameters + * do not hold. + * @exception IOException If an I/O error occurs. + * @exception NonReadableChannelException If mode is READ_ONLY but this channel was + * not opened for reading. + * @exception NonWritableChannelException If mode is READ_WRITE or PRIVATE but this + * channel was not opened for writing. + */ + public abstract MappedByteBuffer map(MapMode mode, long position, long size) + throws IOException; + + /** + * Return the size of the file thus far + * + * @exception ClosedChannelException If this channel is closed. + */ + public abstract long size() throws IOException; + + /** + * Writes data to the channel. + * + * @exception IOException If an I/O error occurs. + */ + public final long write(ByteBuffer[] srcs) throws IOException + { + return write(srcs, 0, srcs.length); + } + + /** + * Writes data to the channel. + * + * @exception IOException If an I/O error occurs. + */ + public abstract int write(ByteBuffer src) throws IOException; + + /** + * Writes data to the channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the transfer is in progress, thereby closing both + * channels and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If position is negative. + * @exception IOException If an I/O error occurs. + * @exception NonWritableChannelException If this channel was not opened for + * writing. + */ + public abstract int write(ByteBuffer srcs, long position) + throws IOException; + + /** + * Writes data to the channel. + * + * @exception IOException If an I/O error occurs. + */ + public abstract long write(ByteBuffer[] srcs, int offset, int length) + throws IOException; + + /** + * Reads data from the channel. + * + * @exception IOException If an I/O error occurs. + */ + public abstract long read(ByteBuffer[] dsts, int offset, int length) + throws IOException; + + /** + * Reads data from the channel. + * + * @exception IOException If an I/O error occurs. + */ + public final long read(ByteBuffer[] dsts) throws IOException + { + return read(dsts, 0, dsts.length); + } + + /** + * Reads data from the channel. + * + * @exception IOException If an I/O error occurs. + */ + public abstract int read(ByteBuffer dst) throws IOException; + + /** + * Reads data from the channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the transfer is in progress, thereby closing both + * channels and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If position is negative. + * @exception IOException If an I/O error occurs. + * @exception NonReadableChannelException If this channel was not opened for + * reading. + */ + public abstract int read(ByteBuffer dst, long position) + throws IOException; + + /** + * Closes the channel. + * + * This is called from @see close. + * + * @exception IOException If an I/O error occurs. + */ + protected abstract void implCloseChannel() throws IOException; + + /** + * msync with the disk + * + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an I/O error occurs. + */ + public abstract void force(boolean metaData) throws IOException; + + /** + * Creates a file lock for the whole associated file. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedChannelException If this channel is closed. + * @exception FileLockInterruptionException If the invoking thread is + * interrupted while blocked in this method. + * @exception IOException If an I/O error occurs. + * @exception NonReadableChannelException If shared is true and this channel + * was not opened for reading. + * @exception NonWritableChannelException If shared is false and this channel + * was not opened for writing. + * @exception OverlappingFileLockException If a lock that overlaps the + * requested region is already held by this Java virtual machine, or if + * another thread is already blocked in this method and is attempting to lock + * an overlapping region. + */ + public final FileLock lock() throws IOException + { + return lock(0, Long.MAX_VALUE, false); + } + + /** + * Creates a file lock for a region of the associated file. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedChannelException If this channel is closed. + * @exception FileLockInterruptionException If the invoking thread is + * interrupted while blocked in this method. + * @exception IllegalArgumentException If the preconditions on the parameters + * do not hold. + * @exception IOException If an I/O error occurs. + * @exception OverlappingFileLockException If a lock that overlaps the + * requested region is already held by this Java virtual machine, or if + * another thread is already blocked in this method and is attempting to lock + * an overlapping region. + * @exception NonReadableChannelException If shared is true and this channel + * was not opened for reading. + * @exception NonWritableChannelException If shared is false and this channel + * was not opened for writing. + */ + public abstract FileLock lock(long position, long size, boolean shared) + throws IOException; + + /** + * Tries to aqquire alock on the whole associated file. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an I/O error occurs. + * @exception OverlappingFileLockException If a lock that overlaps the + * requested region is already held by this Java virtual machine, or if + * another thread is already blocked in this method and is attempting to lock + * an overlapping region. + */ + public final FileLock tryLock() throws IOException + { + return tryLock(0, Long.MAX_VALUE, false); + } + + /** + * Tries to aqquire a lock on a region of the associated file. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If the preconditions on the parameters + * do not hold. + * @exception IOException If an I/O error occurs. + * @exception OverlappingFileLockException If a lock that overlaps the + * requested region is already held by this Java virtual machine, or if + * another thread is already blocked in this method and is attempting to lock + * an overlapping region. + */ + public abstract FileLock tryLock(long position, long size, boolean shared) + throws IOException; + + /** + * Returns the current position on the file. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an I/O error occurs. + */ + public abstract long position() throws IOException; + + /** + * Sets the position of the channel on the assoziated file. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If newPosition is negative. + * @exception IOException If an I/O error occurs. + */ + public abstract FileChannel position(long newPosition) + throws IOException; + + /** + * Transfers bytes from this channel's file to the given writable byte + * channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the transfer is in progress, thereby closing both + * channels and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If the preconditions on the parameters + * do not hold. + * @exception IOException If an I/O error occurs. + * @exception NonReadableChannelException If this channel was not opened for + * reading. + * @exception NonWritableChannelException If the target channel was not + * opened for writing. + */ + public abstract long transferTo(long position, long count, + WritableByteChannel target) + throws IOException; + + /** + * Transfers bytes from the given readable channel into this channel. + * + * @exception AsynchronousCloseException If another thread closes this channel + * while the transfer is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the transfer is in progress, thereby closing both + * channels and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If the preconditions on the parameters + * do not hold. + * @exception IOException If an I/O error occurs. + * @exception NonReadableChannelException If the source channel was not + * opened for reading. + * @exception NonWritableChannelException If this channel was not opened for + * writing. + */ + public abstract long transferFrom(ReadableByteChannel src, long position, + long count) throws IOException; + + /** + * Truncates the channel's file at <code>size</code>. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If size is negative. + * @exception IOException If an I/O error occurs. + * @exception NonWritableChannelException If this channel was not opened for + * writing. + */ + public abstract FileChannel truncate(long size) throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/FileLock.java b/libjava/classpath/java/nio/channels/FileLock.java new file mode 100644 index 000000000..78210b34d --- /dev/null +++ b/libjava/classpath/java/nio/channels/FileLock.java @@ -0,0 +1,151 @@ +/* FileLock.java -- + Copyright (C) 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import gnu.java.lang.CPStringBuilder; + +import java.io.IOException; + +/** + * @since 1.4 + */ +public abstract class FileLock +{ + private final FileChannel channel; + private final long position; + private final long size; + private final boolean shared; + + /** + * Initializes the file lock. + * + * @exception IllegalArgumentException If the preconditions on the parameters do not hold + */ + protected FileLock(FileChannel channel, long position, long size, + boolean shared) + { + if (position < 0 || size < 0) + throw new IllegalArgumentException(); + + this.channel = channel; + this.position = position; + this.size = size; + this.shared = shared; + } + + /** + * Tells whether or not this lock is valid. + */ + public abstract boolean isValid(); + + /** + * Releases this lock. + * + * @exception IOException If an error occurs + * @exception ClosedChannelException If the locked channel is no longer open. + */ + public abstract void release() throws IOException; + + /** + * Returns the file channel upon whose file this lock is held. + */ + public final FileChannel channel() + { + return channel; + } + + /** + * Tells whether this lock is shared. + */ + public final boolean isShared() + { + return shared; + } + + /** + * Tells whether or not this lock overlaps the given lock range. + */ + public final boolean overlaps(long position, long size) + { + if (position > this.position + this.size) + return false; + + if (position + size < this.position) + return false; + + return true; + } + + /** + * Returns the position within the file of the first byte of the + * locked region. + */ + public final long position() + { + return position; + } + + /** + * Returns the size of the locked region in bytes. + */ + public final long size() + { + return size; + } + + /** + * Returns a string describing the range, type, and validity of this lock. + */ + public final String toString() + { + CPStringBuilder buf = new CPStringBuilder(getClass().getName()); + buf.append("["); + buf.append(position); + buf.append(":"); + buf.append(size); + if (shared) + buf.append(" shared"); + else + buf.append(" exclusive"); + if (isValid()) + buf.append(" valid]"); + else + buf.append(" invalid]"); + return buf.toString(); + } +} diff --git a/libjava/classpath/java/nio/channels/FileLockInterruptionException.java b/libjava/classpath/java/nio/channels/FileLockInterruptionException.java new file mode 100644 index 000000000..f1024331e --- /dev/null +++ b/libjava/classpath/java/nio/channels/FileLockInterruptionException.java @@ -0,0 +1,57 @@ +/* FileLockInterruptionException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class FileLockInterruptionException extends IOException +{ + private static final long serialVersionUID = 7104080643653532383L; + + /** + * Creates the exception + */ + public FileLockInterruptionException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/GatheringByteChannel.java b/libjava/classpath/java/nio/channels/GatheringByteChannel.java new file mode 100644 index 000000000..822ea232a --- /dev/null +++ b/libjava/classpath/java/nio/channels/GatheringByteChannel.java @@ -0,0 +1,79 @@ +/* GatheringByteChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.ByteBuffer; + + +public interface GatheringByteChannel extends WritableByteChannel +{ + /** + * Writes a sequence of bytes to this channel from a subsequence of + * the given buffers + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the write operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the write operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception IOException If an error occurs + * @exception NonWritableChannelException If this channel was not opened for + * writing + */ + long write(ByteBuffer[] srcs, int offset, int length) + throws IOException; + + /** + * Writes a sequence of bytes to this channel from the given buffers + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the write operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the write operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IOException If an error occurs + * @exception NonWritableChannelException If this channel was not opened for + * writing + */ + long write(ByteBuffer[] srcs) throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/IllegalBlockingModeException.java b/libjava/classpath/java/nio/channels/IllegalBlockingModeException.java new file mode 100644 index 000000000..f34d76382 --- /dev/null +++ b/libjava/classpath/java/nio/channels/IllegalBlockingModeException.java @@ -0,0 +1,59 @@ +/* IllegalBlockingModeException.java -- + Copyright (C) 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch (konqueror@gmx.de) + * @since 1.4 + * + * Written using JDK 1.4.1 Online API from Sun + * Status: JDK 1.4 complete + */ +public class IllegalBlockingModeException extends IllegalStateException +{ + private static final long serialVersionUID = - 3335774961855590474L; + + /** + * Creates the exception + */ + public IllegalBlockingModeException() + { + super(); + } +} diff --git a/libjava/classpath/java/nio/channels/IllegalSelectorException.java b/libjava/classpath/java/nio/channels/IllegalSelectorException.java new file mode 100644 index 000000000..01b22529c --- /dev/null +++ b/libjava/classpath/java/nio/channels/IllegalSelectorException.java @@ -0,0 +1,55 @@ +/* IllegalSelectorException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class IllegalSelectorException extends IllegalArgumentException +{ + private static final long serialVersionUID = - 8406323347253320987L; + + /** + * Creates the exception + */ + public IllegalSelectorException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/InterruptibleChannel.java b/libjava/classpath/java/nio/channels/InterruptibleChannel.java new file mode 100644 index 000000000..54122cead --- /dev/null +++ b/libjava/classpath/java/nio/channels/InterruptibleChannel.java @@ -0,0 +1,51 @@ +/* InterruptibleChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; + + +public interface InterruptibleChannel extends Channel +{ + /** + * Closes this channel + * + * @exception IOException If an error occurs + */ + void close() throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/NoConnectionPendingException.java b/libjava/classpath/java/nio/channels/NoConnectionPendingException.java new file mode 100644 index 000000000..8dcbdf695 --- /dev/null +++ b/libjava/classpath/java/nio/channels/NoConnectionPendingException.java @@ -0,0 +1,55 @@ +/* NoConnectionPendingException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class NoConnectionPendingException extends IllegalStateException +{ + private static final long serialVersionUID = - 8296561183633134743L; + + /** + * Creates the exception + */ + public NoConnectionPendingException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/NonReadableChannelException.java b/libjava/classpath/java/nio/channels/NonReadableChannelException.java new file mode 100644 index 000000000..bf4f4a433 --- /dev/null +++ b/libjava/classpath/java/nio/channels/NonReadableChannelException.java @@ -0,0 +1,55 @@ +/* NonReadableChannelException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class NonReadableChannelException extends IllegalStateException +{ + private static final long serialVersionUID = - 3200915679294993514L; + + /** + * Creates the exception + */ + public NonReadableChannelException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/NonWritableChannelException.java b/libjava/classpath/java/nio/channels/NonWritableChannelException.java new file mode 100644 index 000000000..98c86eae2 --- /dev/null +++ b/libjava/classpath/java/nio/channels/NonWritableChannelException.java @@ -0,0 +1,55 @@ +/* NonWritableChannelException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class NonWritableChannelException extends IllegalStateException +{ + private static final long serialVersionUID = - 7071230488279011621L; + + /** + * Creates the exception + */ + public NonWritableChannelException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/NotYetBoundException.java b/libjava/classpath/java/nio/channels/NotYetBoundException.java new file mode 100644 index 000000000..74bf1ed7f --- /dev/null +++ b/libjava/classpath/java/nio/channels/NotYetBoundException.java @@ -0,0 +1,55 @@ +/* NotYetBoundException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class NotYetBoundException extends IllegalStateException +{ + private static final long serialVersionUID = 4640999303950202242L; + + /** + * Creates the exception + */ + public NotYetBoundException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/NotYetConnectedException.java b/libjava/classpath/java/nio/channels/NotYetConnectedException.java new file mode 100644 index 000000000..083ff6c6d --- /dev/null +++ b/libjava/classpath/java/nio/channels/NotYetConnectedException.java @@ -0,0 +1,55 @@ +/* NotYetConnectedException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class NotYetConnectedException extends IllegalStateException +{ + private static final long serialVersionUID = 4697316551909513464L; + + /** + * Creates the exception + */ + public NotYetConnectedException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/OverlappingFileLockException.java b/libjava/classpath/java/nio/channels/OverlappingFileLockException.java new file mode 100644 index 000000000..aa2cedd9a --- /dev/null +++ b/libjava/classpath/java/nio/channels/OverlappingFileLockException.java @@ -0,0 +1,55 @@ +/* OverlappingFileLockException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class OverlappingFileLockException extends IllegalStateException +{ + private static final long serialVersionUID = 2047812138163068433L; + + /** + * Creates the exception + */ + public OverlappingFileLockException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/Pipe.java b/libjava/classpath/java/nio/channels/Pipe.java new file mode 100644 index 000000000..c7b04c88c --- /dev/null +++ b/libjava/classpath/java/nio/channels/Pipe.java @@ -0,0 +1,121 @@ +/* Pipe.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.SelectorProvider; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class Pipe +{ + public abstract static class SinkChannel extends AbstractSelectableChannel + implements WritableByteChannel, GatheringByteChannel + { + /** + * Initializes the channel. + */ + protected SinkChannel(SelectorProvider provider) + { + super(provider); + } + + /** + * Returns an operation set that is valid on this channel. + * + * The only valid operation on this channel is @see SelectionKey.OP_WRITE. + */ + public final int validOps() + { + return SelectionKey.OP_WRITE; + } + } + + public abstract static class SourceChannel extends AbstractSelectableChannel + implements ReadableByteChannel, ScatteringByteChannel + { + /** + * Initializes the channel. + */ + protected SourceChannel(SelectorProvider provider) + { + super(provider); + } + + /** + * Returns an operation set that is valid on this channel. + * + * The only valid operation on this channel is @see SelectionKey.OP_READ. + */ + public final int validOps() + { + return SelectionKey.OP_READ; + } + } + + /** + * Initializes the pipe. + */ + protected Pipe() + { + } + + /** + * Opens a pipe. + * + * @exception IOException If an error occurs + */ + public static Pipe open() throws IOException + { + return SelectorProvider.provider().openPipe(); + } + + /** + * Returns a pipe's sink channel. + */ + public abstract Pipe.SinkChannel sink(); + + /** + * Returns a pipe's source channel + */ + public abstract Pipe.SourceChannel source(); +} diff --git a/libjava/classpath/java/nio/channels/ReadableByteChannel.java b/libjava/classpath/java/nio/channels/ReadableByteChannel.java new file mode 100644 index 000000000..889662de0 --- /dev/null +++ b/libjava/classpath/java/nio/channels/ReadableByteChannel.java @@ -0,0 +1,64 @@ +/* ReadableByteChannel.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.ByteBuffer; + + +public interface ReadableByteChannel extends Channel +{ + /** + * Reads a sequence of bytes from this channel into the given buffer + * + * @param dst the buffer to put the read data into + * + * @return the numer of bytes read + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the read operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the read operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IOException If an error occurs + * @exception NonReadableChannelException If this channel was not opened for + * reading + */ + int read(ByteBuffer dst) throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/ScatteringByteChannel.java b/libjava/classpath/java/nio/channels/ScatteringByteChannel.java new file mode 100644 index 000000000..5437ea158 --- /dev/null +++ b/libjava/classpath/java/nio/channels/ScatteringByteChannel.java @@ -0,0 +1,79 @@ +/* ScatteringByteChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.ByteBuffer; + + +public interface ScatteringByteChannel extends ReadableByteChannel +{ + /** + * Reads a sequence of bytes from this channel into a subsequence of the + * given buffers + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the write operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the write operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + * @exception IOException If an error occurs + * @exception NonReadableChannelException If this channel was not opened for + * reading + */ + long read(ByteBuffer[] srcs, int offset, int length) + throws IOException; + + /** + * Reads a sequence of bytes from this channel into the given buffers + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the write operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the write operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IOException If an error occurs + * @exception NonReadableChannelException If this channel was not opened for + * reading + */ + long read(ByteBuffer[] srcs) throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/SelectableChannel.java b/libjava/classpath/java/nio/channels/SelectableChannel.java new file mode 100644 index 000000000..70fa785ce --- /dev/null +++ b/libjava/classpath/java/nio/channels/SelectableChannel.java @@ -0,0 +1,140 @@ +/* SelectableChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.channels.spi.AbstractInterruptibleChannel; +import java.nio.channels.spi.SelectorProvider; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class SelectableChannel extends AbstractInterruptibleChannel +{ + /** + * Initializes the channel. + */ + protected SelectableChannel() + { + } + + /** + * Returns the lock of this channel. + */ + public abstract Object blockingLock(); + + /** + * Adjusts this channel's blocking mode. + * + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalBlockingModeException If block is true and this channel + * is registered with one or more selectors. + * @exception IOException If an error occurs. + */ + public abstract SelectableChannel configureBlocking(boolean block) + throws IOException; + + /** + * Tells whether this channel is blocking or not. + */ + public abstract boolean isBlocking(); + + /** + * Tells whether or not this channel is currently registered with + * any selectors. + */ + public abstract boolean isRegistered(); + + /** + * Retrieves the key representing the channel's registration with + * the given selector. + */ + public abstract SelectionKey keyFor(Selector sel); + + /** + * Returns the provider that created this channel. + */ + public abstract SelectorProvider provider(); + + /** + * Registers this channel with the given selector, + * returning a selection key. + * + * @exception CancelledKeyException If this channel is currently registered + * with the given selector but the corresponding key has already been cancelled + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If a bit in ops does not correspond + * to an operation that is supported by this channel, that is, if + * set & ~validOps() != 0. + * @exception IllegalBlockingModeException If block is true and this channel + * is registered with one or more selectors. + * @exception IllegalSelectorException If this channel was not created by + * the same provider as the given selector. + */ + public final SelectionKey register(Selector sel, int ops) + throws ClosedChannelException + { + return register(sel, ops, null); + } + + /** + * Registers this channel with the given selector, + * returning a selection key. + * + * @exception CancelledKeyException If this channel is currently registered + * with the given selector but the corresponding key has already been + * cancelled. + * @exception ClosedChannelException If this channel is closed. + * @exception IllegalArgumentException If a bit in ops does not correspond + * to an operation that is supported by this channel, that is, if + * set & ~validOps() != 0. + * @exception IllegalBlockingModeException If block is true and this channel + * is registered with one or more selectors. + * @exception IllegalSelectorException If this channel was not created by + * the same provider as the given selector. + */ + public abstract SelectionKey register(Selector sel, int ops, Object att) + throws ClosedChannelException; + + /** + * Returns a set of valid operations on this channel. + */ + public abstract int validOps(); +} diff --git a/libjava/classpath/java/nio/channels/SelectionKey.java b/libjava/classpath/java/nio/channels/SelectionKey.java new file mode 100644 index 000000000..9723faf52 --- /dev/null +++ b/libjava/classpath/java/nio/channels/SelectionKey.java @@ -0,0 +1,164 @@ +/* SelectionKey.java -- + Copyright (C) 2002, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class SelectionKey +{ + public static final int OP_ACCEPT = 16; + public static final int OP_CONNECT = 8; + public static final int OP_READ = 1; + public static final int OP_WRITE = 4; + Object attached; + + /** + * Initializes the selection key. + */ + protected SelectionKey() + { + } + + /** + * Attaches obj to the key and returns the old attached object. + */ + public final synchronized Object attach(Object obj) + { + Object old = attached; + attached = obj; + return old; + } + + /** + * Returns the object attached to the key. + */ + public final synchronized Object attachment() + { + return attached; + } + + /** + * Tests if the channel attached to this key is ready to accept + * a new socket connection. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public final boolean isAcceptable() + { + return (readyOps() & OP_ACCEPT) != 0; + } + + /** + * Tests whether this key's channel has either finished, + * or failed to finish, its socket-connection operation. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public final boolean isConnectable() + { + return (readyOps() & OP_CONNECT) != 0; + } + + /** + * Tests if the channel attached to the key is readable. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public final boolean isReadable() + { + return (readyOps() & OP_READ) != 0; + } + + /** + * Tests if the channel attached to the key is writable. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public final boolean isWritable() + { + return (readyOps() & OP_WRITE) != 0; + } + + /** + * Requests that the registration of this key's channel with + * its selector be cancelled. + */ + public abstract void cancel(); + + /** + * return the channel attached to the key. + */ + public abstract SelectableChannel channel(); + + /** + * Returns the key's interest set. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public abstract int interestOps(); + + /** + * Sets this key's interest set to the given value. + * + * @exception CancelledKeyException If this key has been cancelled + * @exception IllegalArgumentException If a bit in the set does not + * correspond to an operation that is supported by this key's channel, + * that is, if set & ~(channel().validOps()) != 0 + */ + public abstract SelectionKey interestOps(int ops); + + /** + * Tells whether or not this key is valid. + */ + public abstract boolean isValid(); + + /** + * Retrieves this key's ready-operation set. + * + * @exception CancelledKeyException If this key has been cancelled + */ + public abstract int readyOps(); + + /** + * Returns the selector for which this key was created. + */ + public abstract Selector selector(); +} diff --git a/libjava/classpath/java/nio/channels/Selector.java b/libjava/classpath/java/nio/channels/Selector.java new file mode 100644 index 000000000..1c09db702 --- /dev/null +++ b/libjava/classpath/java/nio/channels/Selector.java @@ -0,0 +1,134 @@ +/* Selector.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.channels.spi.SelectorProvider; +import java.util.Set; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class Selector +{ + /** + * Initializes the selector. + */ + protected Selector() + { + } + + /** + * Opens a selector. + * + * @exception IOException If an error occurs + */ + public static Selector open() throws IOException + { + return SelectorProvider.provider().openSelector(); + } + + /** + * Closes the selector. + * + * @exception IOException If an error occurs + */ + public abstract void close() throws IOException; + + /** + * Tells whether the selector is open or not. + */ + public abstract boolean isOpen(); + + /** + * Returns this selector's key set. + * + * @exception ClosedSelectorException If this selector is closed. + */ + public abstract Set<SelectionKey> keys(); + + /** + * Returns the SelectorProvider that created the selector. + */ + public abstract SelectorProvider provider(); + + /** + * Selects a set of keys whose corresponding channels are ready + * for I/O operations. + * + * @exception ClosedSelectorException If this selector is closed. + * @exception IOException If an error occurs + */ + public abstract int select() throws IOException; + + /** + * Selects a set of keys whose corresponding channels are ready + * for I/O operations. + * + * @param timeout The timeout to use. + * + * @exception ClosedSelectorException If this selector is closed. + * @exception IllegalArgumentException If the timeout value is negative. + * @exception IOException If an error occurs + */ + public abstract int select(long timeout) throws IOException; + + /** + * Returns this selector's selected-key set. + * + * @exception ClosedSelectorException If this selector is closed. + */ + public abstract Set<SelectionKey> selectedKeys(); + + /** + * Selects a set of keys whose corresponding channels are ready + * for I/O operations. + * + * @exception ClosedSelectorException If this selector is closed. + * @exception IOException If an error occurs + */ + public abstract int selectNow() throws IOException; + + /** + * Causes the first selection operation that has not yet returned to + * return immediately. + */ + public abstract Selector wakeup(); +} diff --git a/libjava/classpath/java/nio/channels/ServerSocketChannel.java b/libjava/classpath/java/nio/channels/ServerSocketChannel.java new file mode 100644 index 000000000..105d17f94 --- /dev/null +++ b/libjava/classpath/java/nio/channels/ServerSocketChannel.java @@ -0,0 +1,98 @@ +/* ServerSocketChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.net.ServerSocket; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.SelectorProvider; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class ServerSocketChannel extends AbstractSelectableChannel +{ + /** + * Initializes this channel. + */ + protected ServerSocketChannel(SelectorProvider provider) + { + super(provider); + } + + /** + * Accepts a connection made to this channel's socket. + * + * @exception IOException If an error occurs + * @exception AsynchronousCloseException If another thread closes this + * channel while the accept operation is in progress. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the accept operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If the channel is closed. + * @exception NotYetBoundException If the channel's socket is not yet bound. + * @exception SecurityException If a security manager has been installed and + * it does not permit access to the remote endpoint of the new connection. + */ + public abstract SocketChannel accept() throws IOException; + + /** + * Retrieves the channels socket. + */ + public abstract ServerSocket socket(); + + /** + * Opens a server socket channel. + * + * @exception IOException If an error occurs + */ + public static ServerSocketChannel open() throws IOException + { + return SelectorProvider.provider().openServerSocketChannel(); + } + + /** + * Retrieves the valid operations for this channel. + */ + public final int validOps() + { + return SelectionKey.OP_ACCEPT; + } +} diff --git a/libjava/classpath/java/nio/channels/SocketChannel.java b/libjava/classpath/java/nio/channels/SocketChannel.java new file mode 100644 index 000000000..324c9816b --- /dev/null +++ b/libjava/classpath/java/nio/channels/SocketChannel.java @@ -0,0 +1,248 @@ +/* SocketChannel.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio.channels; + +import java.io.IOException; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.SelectorProvider; + +/** + * @author Michael Koch (konqueror@gmx.de) + * @since 1.4 + */ +public abstract class SocketChannel extends AbstractSelectableChannel + implements ByteChannel, ScatteringByteChannel, GatheringByteChannel +{ + /** + * Initializes this socket channel. + */ + protected SocketChannel(SelectorProvider provider) + { + super(provider); + } + + /** + * Opens a socket channel. + * + * @return the new <code>SocketChannel</code> object + * + * @exception IOException If an error occurs + */ + public static SocketChannel open() throws IOException + { + return SelectorProvider.provider().openSocketChannel(); + } + + /** + * Opens a channel and connects it to a remote address. + * + * @return the new <code>SocketChannel</code> object + * + * @exception AsynchronousCloseException If this channel is already connected. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the connect operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status. + * @exception IOException If an error occurs + * @exception SecurityException If a security manager has been installed and + * it does not permit access to the given remote endpoint. + * @exception UnresolvedAddressException If the given remote address is not + * fully resolved. + * @exception UnsupportedAddressTypeException If the type of the given remote + * address is not supported. + */ + public static SocketChannel open(SocketAddress remote) + throws IOException + { + SocketChannel ch = open(); + ch.connect(remote); + return ch; + } + + /** + * Reads data from the channel. + * + * @return the number of bytes read, zero is valid too, -1 if end of stream + * is reached + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public final long read(ByteBuffer[] dsts) throws IOException + { + long b = 0; + + for (int i = 0; i < dsts.length; i++) + b += read(dsts[i]); + + return b; + } + + /** + * Writes data to the channel. + * + * @return the number of bytes written, zero is valid too + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public final long write(ByteBuffer[] dsts) throws IOException + { + long b = 0; + + for (int i = 0; i < dsts.length; i++) + b += write(dsts[i]); + + return b; + } + + /** + * Retrieves the valid operations for this channel. + * + * @return the valid operations + */ + public final int validOps() + { + return SelectionKey.OP_CONNECT | SelectionKey.OP_READ + | SelectionKey.OP_WRITE; + } + + /** + * Reads data from the channel. + * + * @return the number of bytes read, zero is valid too, -1 if end of stream + * is reached + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public abstract int read(ByteBuffer dst) throws IOException; + + /** + * Connects the channel's socket to the remote address. + * + * @return <code>true</code> if the channel got successfully connected, + * <code>false</code> if the channel is in non-blocking mode and connection + * operation is still in progress. + * + * @exception AlreadyConnectedException If this channel is already connected. + * @exception AsynchronousCloseException If this channel is already connected. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the connect operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception ConnectionPendingException If a non-blocking connection + * operation is already in progress on this channel. + * @exception IOException If an error occurs + * @exception SecurityException If a security manager has been installed and + * it does not permit access to the given remote endpoint. + * @exception UnresolvedAddressException If the given remote address is not + * fully resolved. + * @exception UnsupportedAddressTypeException If the type of the given remote + * address is not supported. + */ + public abstract boolean connect(SocketAddress remote) + throws IOException; + + /** + * Finishes the process of connecting a socket channel. + * + * @exception AsynchronousCloseException If this channel is already connected. + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the connect operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status. + * @exception ClosedChannelException If this channel is closed. + * @exception IOException If an error occurs + * @exception NoConnectionPendingException If this channel is not connected + * and a connection operation has not been initiated. + */ + public abstract boolean finishConnect() throws IOException; + + /** + * Tells whether or not the channel's socket is connected. + */ + public abstract boolean isConnected(); + + /** + * Tells whether or not a connection operation is in progress on this channel. + */ + public abstract boolean isConnectionPending(); + + /** + * Reads data from the channel. + * + * @return the number of bytes read, zero is valid too, -1 if end of stream + * is reached + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public abstract long read(ByteBuffer[] dsts, int offset, int length) + throws IOException; + + /** + * Retrieves the channel's socket. + * + * @return the socket + */ + public abstract Socket socket(); + + /** + * Writes data to the channel. + * + * @return the number of bytes written, zero is valid too + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public abstract int write(ByteBuffer src) throws IOException; + + /** + * Writes data to the channel. + * + * @return the number of bytes written, zero is valid too + * + * @exception IOException If an error occurs + * @exception NotYetConnectedException If this channel is not yet connected. + */ + public abstract long write(ByteBuffer[] srcs, int offset, int length) + throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/UnresolvedAddressException.java b/libjava/classpath/java/nio/channels/UnresolvedAddressException.java new file mode 100644 index 000000000..7a591709b --- /dev/null +++ b/libjava/classpath/java/nio/channels/UnresolvedAddressException.java @@ -0,0 +1,55 @@ +/* UnresolvedAddressException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class UnresolvedAddressException extends IllegalArgumentException +{ + private static final long serialVersionUID = 6136959093620794148L; + + /** + * Creates the exception + */ + public UnresolvedAddressException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/UnsupportedAddressTypeException.java b/libjava/classpath/java/nio/channels/UnsupportedAddressTypeException.java new file mode 100644 index 000000000..759d0964c --- /dev/null +++ b/libjava/classpath/java/nio/channels/UnsupportedAddressTypeException.java @@ -0,0 +1,55 @@ +/* UnsupportedAddressTypeException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public class UnsupportedAddressTypeException extends IllegalArgumentException +{ + private static final long serialVersionUID = - 2964323842829700493L; + + /** + * Creates the exception + */ + public UnsupportedAddressTypeException() + { + } +} diff --git a/libjava/classpath/java/nio/channels/WritableByteChannel.java b/libjava/classpath/java/nio/channels/WritableByteChannel.java new file mode 100644 index 000000000..3845723bc --- /dev/null +++ b/libjava/classpath/java/nio/channels/WritableByteChannel.java @@ -0,0 +1,60 @@ +/* WritableByteChannel.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels; + +import java.io.IOException; +import java.nio.ByteBuffer; + + +public interface WritableByteChannel extends Channel +{ + /** + * Writes a sequence of bytes to this channel from the given buffer + * + * @exception AsynchronousCloseException If another thread closes this + * channel while the write operation is in progress + * @exception ClosedByInterruptException If another thread interrupts the + * current thread while the write operation is in progress, thereby closing + * the channel and setting the current thread's interrupt status + * @exception ClosedChannelException If this channel is closed + * @exception IOException If an error occurs + * @exception NonWritableChannelException If this channel was not opened for + * writing + */ + int write(ByteBuffer src) throws IOException; +} diff --git a/libjava/classpath/java/nio/channels/package.html b/libjava/classpath/java/nio/channels/package.html new file mode 100644 index 000000000..4e2bbc9f6 --- /dev/null +++ b/libjava/classpath/java/nio/channels/package.html @@ -0,0 +1,47 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in java.nio.channels package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - java.nio.channels</title></head> + +<body> +<p>Classes for creating, selecting and non-blocking reading and writing of +buffers from files and sockets.</p> + +</body> +</html> diff --git a/libjava/classpath/java/nio/channels/spi/AbstractInterruptibleChannel.java b/libjava/classpath/java/nio/channels/spi/AbstractInterruptibleChannel.java new file mode 100644 index 000000000..b1ed7c7f2 --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/AbstractInterruptibleChannel.java @@ -0,0 +1,119 @@ +/* AbstractInterruptibleChannel.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels.spi; + +import java.io.IOException; +import java.nio.channels.AsynchronousCloseException; +import java.nio.channels.Channel; +import java.nio.channels.ClosedByInterruptException; +import java.nio.channels.InterruptibleChannel; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class AbstractInterruptibleChannel + implements Channel, InterruptibleChannel +{ + private boolean closed; + + /** + * Initializes the channel. + */ + protected AbstractInterruptibleChannel() + { + } + + /** + * Marks the beginning of an I/O operation that might block indefinitely. + */ + protected final void begin() + { + } + + /** + * Closes the channel. + * + * @exception IOException If an error occurs + */ + public final void close() throws IOException + { + if (! closed) + { + closed = true; + implCloseChannel(); + } + } + + /** + * Marks the end of an I/O operation that might block indefinitely. + * + * @param completed true if the task completed successfully, + * false otherwise + * + * @exception AsynchronousCloseException If the channel was asynchronously + * closed. + * @exception ClosedByInterruptException If the thread blocked in the + * I/O operation was interrupted. + */ + protected final void end(boolean completed) + throws AsynchronousCloseException + { + // FIXME: check more here. + + if (closed) throw new AsynchronousCloseException(); + } + + /** + * Closes the channel. + * + * @exception IOException If an error occurs + */ + protected abstract void implCloseChannel() throws IOException; + + /** + * Tells whether or not this channel is open. + * + * @return true if the channel is open, false otherwise + */ + public final boolean isOpen() + { + return ! closed; + } +} diff --git a/libjava/classpath/java/nio/channels/spi/AbstractSelectableChannel.java b/libjava/classpath/java/nio/channels/spi/AbstractSelectableChannel.java new file mode 100644 index 000000000..97ed685b8 --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/AbstractSelectableChannel.java @@ -0,0 +1,271 @@ +/* AbstractSelectableChannel.java + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio.channels.spi; + +import java.io.IOException; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.IllegalBlockingModeException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.ListIterator; + +public abstract class AbstractSelectableChannel extends SelectableChannel +{ + private boolean blocking = true; + private Object LOCK = new Object(); + private SelectorProvider provider; + private LinkedList keys = new LinkedList(); + + /** + * Initializes the channel + * + * @param provider the provider that created this channel + */ + protected AbstractSelectableChannel(SelectorProvider provider) + { + this.provider = provider; + } + + /** + * Retrieves the object upon which the configureBlocking and register + * methods synchronize. + * + * @return the blocking lock + */ + public final Object blockingLock() + { + return LOCK; + } + + /** + * Adjusts this channel's blocking mode. + * + * @param blocking true if blocking should be enabled, false otherwise + * + * @return this channel + * + * @exception IOException If an error occurs + */ + public final SelectableChannel configureBlocking(boolean blocking) + throws IOException + { + synchronized (blockingLock()) + { + if (this.blocking != blocking) + { + implConfigureBlocking(blocking); + this.blocking = blocking; + } + } + + return this; + } + + /** + * Closes this channel. + * + * @exception IOException If an error occurs + */ + protected final void implCloseChannel() throws IOException + { + try + { + implCloseSelectableChannel(); + } + finally + { + for (Iterator it = keys.iterator(); it.hasNext(); ) + ((SelectionKey) it.next()).cancel(); + } + } + + /** + * Closes this selectable channel. + * + * @exception IOException If an error occurs + */ + protected abstract void implCloseSelectableChannel() + throws IOException; + + /** + * Adjusts this channel's blocking mode. + * + * @param blocking true if blocking should be enabled, false otherwise + * + * @exception IOException If an error occurs + */ + protected abstract void implConfigureBlocking(boolean blocking) + throws IOException; + + /** + * Tells whether or not every I/O operation on this channel will block + * until it completes. + * + * @return true of this channel is blocking, false otherwise + */ + public final boolean isBlocking() + { + return blocking; + } + + /** + * Tells whether or not this channel is currently registered with + * any selectors. + * + * @return true if this channel is registered, false otherwise + */ + public final boolean isRegistered() + { + return ! keys.isEmpty(); + } + + /** + * Retrieves the key representing the channel's registration with the + * given selector. + * + * @param selector the selector to get a selection key for + * + * @return the selection key this channel is registered with + */ + public final SelectionKey keyFor(Selector selector) + { + if (! isOpen()) + return null; + + try + { + synchronized (blockingLock()) + { + return locate(selector); + } + } + catch (Exception e) + { + return null; + } + } + + /** + * Returns the provider that created this channel. + * + * @return the selector provider that created this channel + */ + public final SelectorProvider provider() + { + return provider; + } + + private SelectionKey locate(Selector selector) + { + ListIterator it = keys.listIterator(); + + while (it.hasNext()) + { + SelectionKey key = (SelectionKey) it.next(); + + if (key.selector() == selector) + return key; + } + + return null; + } + + /** + * Registers this channel with the given selector, returning a selection key. + * + * @param selin the seletor to use + * @param ops the interested operations + * @param att an attachment for the returned selection key + * + * @return the registered selection key + * + * @exception ClosedChannelException If the channel is already closed. + * @exception IllegalBlockingModeException If the channel is configured in + * blocking mode. + */ + public final SelectionKey register(Selector selin, int ops, Object att) + throws ClosedChannelException + { + if (! isOpen()) + throw new ClosedChannelException(); + + if ((ops & ~validOps()) != 0) + throw new IllegalArgumentException(); + + SelectionKey key = null; + AbstractSelector selector = (AbstractSelector) selin; + + synchronized (blockingLock()) + { + if (blocking) + throw new IllegalBlockingModeException(); + + key = locate(selector); + + if (key != null && key.isValid()) + { + key.interestOps(ops); + key.attach(att); + } + else + { + key = selector.register(this, ops, att); + + if (key != null) + addSelectionKey(key); + } + } + + return key; + } + + void addSelectionKey(SelectionKey key) + { + keys.add(key); + } + + // This method gets called by AbstractSelector.deregister(). + void removeSelectionKey(SelectionKey key) + { + keys.remove(key); + } +} diff --git a/libjava/classpath/java/nio/channels/spi/AbstractSelectionKey.java b/libjava/classpath/java/nio/channels/spi/AbstractSelectionKey.java new file mode 100644 index 000000000..e0dc03f53 --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/AbstractSelectionKey.java @@ -0,0 +1,78 @@ +/* AbstractSelectionKey.java -- + Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels.spi; + +import java.nio.channels.SelectionKey; + + +/** + * @since 1.4 + */ +public abstract class AbstractSelectionKey extends SelectionKey +{ + private boolean cancelled; + + /** + * Initializes the key. + */ + protected AbstractSelectionKey() + { + } + + /** + * Cancels this key. + */ + public final synchronized void cancel() + { + if (isValid()) + { + ((AbstractSelector) selector()).cancelKey(this); + cancelled = true; + } + } + + /** + * Tells whether this key is valid or not. + * + * @return true if this key is valid, false otherwise + */ + public final synchronized boolean isValid() + { + return ! cancelled; + } +} diff --git a/libjava/classpath/java/nio/channels/spi/AbstractSelector.java b/libjava/classpath/java/nio/channels/spi/AbstractSelector.java new file mode 100644 index 000000000..b67d4aecc --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/AbstractSelector.java @@ -0,0 +1,167 @@ +/* AbstractSelector.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels.spi; + +import java.io.IOException; +import java.nio.channels.ClosedSelectorException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.util.HashSet; +import java.util.Set; + + +public abstract class AbstractSelector extends Selector +{ + private boolean closed; + private SelectorProvider provider; + private HashSet<SelectionKey> cancelledKeys; + + /** + * Initializes the slector. + * + * @param provider the provider that created this selector + */ + protected AbstractSelector(SelectorProvider provider) + { + this.provider = provider; + this.cancelledKeys = new HashSet<SelectionKey>(); + } + + /** + * Closes the channel. + * + * @exception IOException If an error occurs + */ + public final synchronized void close() throws IOException + { + if (closed) + return; + + implCloseSelector(); + closed = true; + } + + /** + * Tells whether this channel is open or not. + * + * @return true if channel is open, false otherwise. + */ + public final boolean isOpen() + { + return ! closed; + } + + /** + * Marks the beginning of an I/O operation that might block indefinitely. + */ + protected final void begin() + { + } + + /** + * Marks the end of an I/O operation that might block indefinitely. + */ + protected final void end() + { + } + + /** + * Returns the provider for this selector object. + * + * @return the SelectorProvider object that created this seletor + */ + public final SelectorProvider provider() + { + return provider; + } + + /** + * Returns the cancelled keys set. + * + * @return the cancelled keys set + */ + protected final Set<SelectionKey> cancelledKeys() + { + if (! isOpen()) + throw new ClosedSelectorException(); + + return cancelledKeys; + } + + /** + * Cancels a selection key. + */ + + // This method is only called by AbstractSelectionKey.cancel(). + final void cancelKey(AbstractSelectionKey key) + { + synchronized (cancelledKeys) + { + cancelledKeys.add(key); + } + } + + /** + * Closes the channel. + * + * @exception IOException if an error occurs + */ + protected abstract void implCloseSelector() throws IOException; + + /** + * Registers a channel for the selection process. + * + * @param ch the channel register + * @param ops the interested operations + * @param att an attachement to the selection key + * + * @return the registered selection key + */ + protected abstract SelectionKey register(AbstractSelectableChannel ch, + int ops, Object att); + + /** + * Deregisters the given selection key. + * + * @param key the key to deregister + */ + protected final void deregister(AbstractSelectionKey key) + { + ((AbstractSelectableChannel) key.channel()).removeSelectionKey(key); + } +} diff --git a/libjava/classpath/java/nio/channels/spi/SelectorProvider.java b/libjava/classpath/java/nio/channels/spi/SelectorProvider.java new file mode 100644 index 000000000..821bc4361 --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/SelectorProvider.java @@ -0,0 +1,178 @@ +/* SelectorProvider.java + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.channels.spi; + +import gnu.java.nio.SelectorProviderImpl; + +import java.io.IOException; +import java.nio.channels.Channel; +import java.nio.channels.DatagramChannel; +import java.nio.channels.Pipe; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + + +/** + * @author Michael Koch + * @since 1.4 + */ +public abstract class SelectorProvider +{ + private static SelectorProvider systemDefaultProvider; + + /** + * Initializes the selector provider. + * + * @exception SecurityException If a security manager has been installed and + * it denies @see RuntimePermission ("selectorProvider"). + */ + protected SelectorProvider() + { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkPermission(new RuntimePermission("selectorProvider")); + } + + /** + * Opens a datagram channel. + * + * @return a new datagram channel object + * + * @exception IOException if an error occurs + */ + public abstract DatagramChannel openDatagramChannel() + throws IOException; + + /** + * Opens a pipe. + * + * @return a new pipe object + * + * @exception IOException if an error occurs + */ + public abstract Pipe openPipe() throws IOException; + + /** + * Opens a selector. + * + * @return a new selector object + * + * @exception IOException if an error occurs + */ + public abstract AbstractSelector openSelector() throws IOException; + + /** + * Opens a server socket channel. + * + * @return a new server socket channel object + * + * @exception IOException if an error occurs + */ + public abstract ServerSocketChannel openServerSocketChannel() + throws IOException; + + /** + * Opens a socket channel. + * + * @return a new socket channel object + * + * @exception IOException if an error occurs + */ + public abstract SocketChannel openSocketChannel() throws IOException; + + /** + * Returns the inherited channel of the VM. + * + * @return the inherited channel of the VM + * + * @throws IOException If an I/O error occurs + * @throws SecurityException If an installed security manager denies access + * to RuntimePermission("inheritedChannel") + * + * @since 1.5 + */ + public Channel inheritedChannel() + throws IOException + { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + { + sm.checkPermission(new RuntimePermission("inheritedChannel")); + } + // Implementation note: The default implementation can't do much more + // than return null. If a VM can provide something more useful it should + // install its own SelectorProvider (maybe a subclass of this one) to + // return something more useful. + return null; + } + + /** + * Returns the system-wide default selector provider for this invocation + * of the Java virtual machine. + * + * @return the default seletor provider + */ + public static synchronized SelectorProvider provider() + { + if (systemDefaultProvider == null) + { + String propertyValue = + System.getProperty("java.nio.channels.spi.SelectorProvider"); + + if (propertyValue == null || propertyValue.equals("")) + systemDefaultProvider = new SelectorProviderImpl(); + else + { + try + { + systemDefaultProvider = + (SelectorProvider) Class.forName(propertyValue) + .newInstance(); + } + catch (Exception e) + { + System.err.println("Could not instantiate class: " + + propertyValue); + systemDefaultProvider = new SelectorProviderImpl(); + } + } + } + + return systemDefaultProvider; + } +} diff --git a/libjava/classpath/java/nio/channels/spi/package.html b/libjava/classpath/java/nio/channels/spi/package.html new file mode 100644 index 000000000..133fb04f2 --- /dev/null +++ b/libjava/classpath/java/nio/channels/spi/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in java.nio.channels.spi package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - java.nio.channels.spi</title></head> + +<body> +<p>Utility classes and interfaces for implementing channels.</p> + +</body> +</html> diff --git a/libjava/classpath/java/nio/charset/CharacterCodingException.java b/libjava/classpath/java/nio/charset/CharacterCodingException.java new file mode 100644 index 000000000..05f1e3aea --- /dev/null +++ b/libjava/classpath/java/nio/charset/CharacterCodingException.java @@ -0,0 +1,55 @@ +/* CharacterCodingException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +import java.io.IOException; + +/** + * @since 1.4 + */ +public class CharacterCodingException extends IOException +{ + private static final long serialVersionUID = 8421532232154627783L; + + /** + * Creates the exception + */ + public CharacterCodingException() + { + } +} diff --git a/libjava/classpath/java/nio/charset/Charset.java b/libjava/classpath/java/nio/charset/Charset.java new file mode 100644 index 000000000..1757b82ea --- /dev/null +++ b/libjava/classpath/java/nio/charset/Charset.java @@ -0,0 +1,402 @@ +/* Charset.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.nio.charset; + +import gnu.classpath.ServiceFactory; +import gnu.classpath.SystemProperties; +import gnu.java.nio.charset.Provider; +import gnu.java.nio.charset.iconv.IconvProvider; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.spi.CharsetProvider; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Locale; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * @author Jesse Rosenstock + * @since 1.4 + * @status updated to 1.5 + */ +public abstract class Charset implements Comparable<Charset> +{ + private CharsetEncoder cachedEncoder; + private CharsetDecoder cachedDecoder; + + /** + * Extra Charset providers. + */ + private static CharsetProvider[] providers; + + private final String canonicalName; + private final String[] aliases; + + protected Charset (String canonicalName, String[] aliases) + { + checkName (canonicalName); + if (aliases != null) + { + int n = aliases.length; + for (int i = 0; i < n; ++i) + checkName (aliases[i]); + } + + cachedEncoder = null; + cachedDecoder = null; + this.canonicalName = canonicalName; + this.aliases = aliases; + } + + /** + * @throws IllegalCharsetNameException if the name is illegal + */ + private static void checkName (String name) + { + int n = name.length (); + + if (n == 0) + throw new IllegalCharsetNameException (name); + + char ch = name.charAt (0); + if (!(('A' <= ch && ch <= 'Z') + || ('a' <= ch && ch <= 'z') + || ('0' <= ch && ch <= '9'))) + throw new IllegalCharsetNameException (name); + + for (int i = 1; i < n; ++i) + { + ch = name.charAt (i); + if (!(('A' <= ch && ch <= 'Z') + || ('a' <= ch && ch <= 'z') + || ('0' <= ch && ch <= '9') + || ch == '-' || ch == '.' || ch == ':' || ch == '_')) + throw new IllegalCharsetNameException (name); + } + } + + /** + * Returns the system default charset. + * + * This may be set by the user or VM with the file.encoding + * property. + * + * @since 1.5 + */ + public static Charset defaultCharset() + { + String encoding; + + try + { + encoding = SystemProperties.getProperty("file.encoding"); + } + catch(SecurityException e) + { + // Use fallback. + encoding = "ISO-8859-1"; + } + catch(IllegalArgumentException e) + { + // Use fallback. + encoding = "ISO-8859-1"; + } + + try + { + return forName(encoding); + } + catch(UnsupportedCharsetException e) + { + // Ignore. + } + catch(IllegalCharsetNameException e) + { + // Ignore. + } + catch(IllegalArgumentException e) + { + // Ignore. + } + + throw new IllegalStateException("Can't get default charset!"); + } + + public static boolean isSupported (String charsetName) + { + return charsetForName (charsetName) != null; + } + + /** + * Returns the Charset instance for the charset of the given name. + * + * @param charsetName + * @return the Charset instance for the indicated charset + * @throws UnsupportedCharsetException if this VM does not support + * the charset of the given name. + * @throws IllegalCharsetNameException if the given charset name is + * legal. + * @throws IllegalArgumentException if <code>charsetName</code> is null. + */ + public static Charset forName (String charsetName) + { + // Throws IllegalArgumentException as the JDK does. + if(charsetName == null) + throw new IllegalArgumentException("Charset name must not be null."); + + Charset cs = charsetForName (charsetName); + if (cs == null) + throw new UnsupportedCharsetException (charsetName); + return cs; + } + + /** + * Retrieves a charset for the given charset name. + * + * @return A charset object for the charset with the specified name, or + * <code>null</code> if no such charset exists. + * + * @throws IllegalCharsetNameException if the name is illegal + */ + private static Charset charsetForName(String charsetName) + { + checkName (charsetName); + // Try the default provider first + // (so we don't need to load external providers unless really necessary) + // if it is an exotic charset try loading the external providers. + Charset cs = provider().charsetForName(charsetName); + if (cs == null) + { + CharsetProvider[] providers = providers2(); + for (int i = 0; i < providers.length; i++) + { + cs = providers[i].charsetForName(charsetName); + if (cs != null) + break; + } + } + return cs; + } + + public static SortedMap<String, Charset> availableCharsets() + { + TreeMap<String, Charset> charsets + = new TreeMap(String.CASE_INSENSITIVE_ORDER); + for (Iterator<Charset> i = provider().charsets(); i.hasNext(); ) + { + Charset cs = i.next(); + charsets.put(cs.name(), cs); + } + + CharsetProvider[] providers = providers2(); + for (int j = 0; j < providers.length; j++) + { + for (Iterator<Charset> i = providers[j].charsets(); i.hasNext(); ) + { + Charset cs = i.next(); + charsets.put(cs.name(), cs); + } + } + + return Collections.unmodifiableSortedMap(charsets); + } + + private static CharsetProvider provider() + { + String useIconv = SystemProperties.getProperty + ("gnu.classpath.nio.charset.provider.iconv"); + + if (useIconv != null) + return IconvProvider.provider(); + + return Provider.provider(); + } + + /** + * We need to support multiple providers, reading them from + * java.nio.charset.spi.CharsetProvider in the resource directory + * META-INF/services. This returns the "extra" charset providers. + */ + private static CharsetProvider[] providers2() + { + if (providers == null) + { + try + { + Iterator i = ServiceFactory.lookupProviders(CharsetProvider.class); + LinkedHashSet set = new LinkedHashSet(); + while (i.hasNext()) + set.add(i.next()); + + providers = new CharsetProvider[set.size()]; + set.toArray(providers); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + return providers; + } + + public final String name () + { + return canonicalName; + } + + public final Set<String> aliases () + { + if (aliases == null) + return Collections.<String>emptySet(); + + // should we cache the aliasSet instead? + int n = aliases.length; + HashSet<String> aliasSet = new HashSet<String> (n); + for (int i = 0; i < n; ++i) + aliasSet.add (aliases[i]); + return Collections.unmodifiableSet (aliasSet); + } + + public String displayName () + { + return canonicalName; + } + + public String displayName (Locale locale) + { + return canonicalName; + } + + public final boolean isRegistered () + { + return (!canonicalName.startsWith ("x-") + && !canonicalName.startsWith ("X-")); + } + + public abstract boolean contains (Charset cs); + + public abstract CharsetDecoder newDecoder (); + + public abstract CharsetEncoder newEncoder (); + + public boolean canEncode () + { + return true; + } + + // NB: This implementation serializes different threads calling + // Charset.encode(), a potential performance problem. It might + // be better to remove the cache, or use ThreadLocal to cache on + // a per-thread basis. + public final synchronized ByteBuffer encode (CharBuffer cb) + { + try + { + if (cachedEncoder == null) + { + cachedEncoder = newEncoder () + .onMalformedInput (CodingErrorAction.REPLACE) + .onUnmappableCharacter (CodingErrorAction.REPLACE); + } else + cachedEncoder.reset(); + return cachedEncoder.encode (cb); + } + catch (CharacterCodingException e) + { + throw new AssertionError (e); + } + } + + public final ByteBuffer encode (String str) + { + return encode (CharBuffer.wrap (str)); + } + + // NB: This implementation serializes different threads calling + // Charset.decode(), a potential performance problem. It might + // be better to remove the cache, or use ThreadLocal to cache on + // a per-thread basis. + public final synchronized CharBuffer decode (ByteBuffer bb) + { + try + { + if (cachedDecoder == null) + { + cachedDecoder = newDecoder () + .onMalformedInput (CodingErrorAction.REPLACE) + .onUnmappableCharacter (CodingErrorAction.REPLACE); + } else + cachedDecoder.reset(); + + return cachedDecoder.decode (bb); + } + catch (CharacterCodingException e) + { + throw new AssertionError (e); + } + } + + public final int compareTo (Charset other) + { + return canonicalName.compareToIgnoreCase (other.canonicalName); + } + + public final int hashCode () + { + return canonicalName.hashCode (); + } + + public final boolean equals (Object ob) + { + if (ob instanceof Charset) + return canonicalName.equalsIgnoreCase (((Charset) ob).canonicalName); + else + return false; + } + + public final String toString () + { + return canonicalName; + } +} diff --git a/libjava/classpath/java/nio/charset/CharsetDecoder.java b/libjava/classpath/java/nio/charset/CharsetDecoder.java new file mode 100644 index 000000000..cf43feafd --- /dev/null +++ b/libjava/classpath/java/nio/charset/CharsetDecoder.java @@ -0,0 +1,317 @@ +/* CharsetDecoder.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; + +/** + * @author Jesse Rosenstock + * @since 1.4 + */ +public abstract class CharsetDecoder +{ + private static final int STATE_RESET = 0; + private static final int STATE_CODING = 1; + private static final int STATE_END = 2; + private static final int STATE_FLUSHED = 3; + + private static final String DEFAULT_REPLACEMENT = "\uFFFD"; + + private final Charset charset; + private final float averageCharsPerByte; + private final float maxCharsPerByte; + private String replacement; + + private int state = STATE_RESET; + + private CodingErrorAction malformedInputAction + = CodingErrorAction.REPORT; + private CodingErrorAction unmappableCharacterAction + = CodingErrorAction.REPORT; + + private CharsetDecoder (Charset cs, float averageCharsPerByte, + float maxCharsPerByte, String replacement) + { + if (averageCharsPerByte <= 0.0f) + throw new IllegalArgumentException ("Non-positive averageCharsPerByte"); + if (maxCharsPerByte <= 0.0f) + throw new IllegalArgumentException ("Non-positive maxCharsPerByte"); + + this.charset = cs; + this.averageCharsPerByte + = averageCharsPerByte; + this.maxCharsPerByte + = maxCharsPerByte; + this.replacement = replacement; + implReplaceWith (replacement); + } + + protected CharsetDecoder (Charset cs, float averageCharsPerByte, + float maxCharsPerByte) + { + this (cs, averageCharsPerByte, maxCharsPerByte, DEFAULT_REPLACEMENT); + } + + public final float averageCharsPerByte () + { + return averageCharsPerByte; + } + + public final Charset charset () + { + return charset; + } + + public final CharBuffer decode (ByteBuffer in) + throws CharacterCodingException + { + // XXX: Sun's Javadoc seems to contradict itself saying an + // IllegalStateException is thrown "if a decoding operation is already + // in progress" and also that "it resets this Decoder". + // Should we check to see that the state is reset, or should we + // call reset()? + if (state != STATE_RESET) + throw new IllegalStateException (); + + // REVIEW: Using max instead of average may allocate a very large + // buffer. Maybe we should do something more efficient? + int remaining = in.remaining (); + int n = (int) (remaining * maxCharsPerByte ()); + CharBuffer out = CharBuffer.allocate (n); + + if (remaining == 0) + { + state = STATE_FLUSHED; + return out; + } + + CoderResult cr = decode (in, out, true); + if (cr.isError ()) + cr.throwException (); + + cr = flush (out); + if (cr.isError ()) + cr.throwException (); + + reset(); + out.flip (); + + // Unfortunately, resizing the actual charbuffer array is required. + char[] resized = new char[out.remaining()]; + out.get(resized); + return CharBuffer.wrap(resized); + } + + public final CoderResult decode (ByteBuffer in, CharBuffer out, + boolean endOfInput) + { + int newState = endOfInput ? STATE_END : STATE_CODING; + // XXX: Need to check for "previous step was an invocation [not] of + // this method with a value of true for the endOfInput parameter but + // a return value indicating an incomplete decoding operation" + // XXX: We will not check the previous return value, just + // that the previous call passed true for endOfInput + if (state != STATE_RESET && state != STATE_CODING + && !(endOfInput && state == STATE_END)) + throw new IllegalStateException (); + state = newState; + + for (;;) + { + CoderResult cr; + try + { + cr = decodeLoop (in, out); + } + catch (RuntimeException e) + { + throw new CoderMalfunctionError (e); + } + + if (cr.isOverflow ()) + return cr; + + if (cr.isUnderflow ()) + { + if (endOfInput && in.hasRemaining ()) + cr = CoderResult.malformedForLength (in.remaining ()); + else + return cr; + } + + CodingErrorAction action = cr.isMalformed () + ? malformedInputAction + : unmappableCharacterAction; + + if (action == CodingErrorAction.REPORT) + return cr; + + if (action == CodingErrorAction.REPLACE) + { + if (out.remaining () < replacement.length ()) + return CoderResult.OVERFLOW; + out.put (replacement); + } + + in.position (in.position () + cr.length ()); + } + } + + protected abstract CoderResult decodeLoop (ByteBuffer in, CharBuffer out); + + public Charset detectedCharset () + { + throw new UnsupportedOperationException (); + } + + public final CoderResult flush (CharBuffer out) + { + // It seems weird that you can flush after reset, but Sun's javadoc + // says an IllegalStateException is thrown "If the previous step of the + // current decoding operation was an invocation neither of the reset + // method nor ... of the three-argument decode method with a value of + // true for the endOfInput parameter." + // Further note that flush() only requires that there not be + // an IllegalStateException if the previous step was a call to + // decode with true as the last argument. It does not require + // that the call succeeded. decode() does require that it succeeded. + // XXX: test this to see if reality matches javadoc + if (state != STATE_RESET && state != STATE_END) + throw new IllegalStateException (); + + state = STATE_FLUSHED; + return implFlush (out); + } + + protected CoderResult implFlush (CharBuffer out) + { + return CoderResult.UNDERFLOW; + } + + public final CharsetDecoder onMalformedInput (CodingErrorAction newAction) + { + if (newAction == null) + throw new IllegalArgumentException ("Null action"); + + malformedInputAction = newAction; + implOnMalformedInput (newAction); + return this; + } + + protected void implOnMalformedInput (CodingErrorAction newAction) + { + // default implementation does nothing + } + + protected void implOnUnmappableCharacter (CodingErrorAction newAction) + { + // default implementation does nothing + } + + protected void implReplaceWith (String newReplacement) + { + // default implementation does nothing + } + + protected void implReset () + { + // default implementation does nothing + } + + public boolean isAutoDetecting () + { + return false; + } + + public boolean isCharsetDetected () + { + throw new UnsupportedOperationException (); + } + + public CodingErrorAction malformedInputAction () + { + return malformedInputAction; + } + + public final float maxCharsPerByte () + { + return maxCharsPerByte; + } + + public final CharsetDecoder onUnmappableCharacter + (CodingErrorAction newAction) + { + if (newAction == null) + throw new IllegalArgumentException ("Null action"); + + unmappableCharacterAction = newAction; + implOnUnmappableCharacter (newAction); + return this; + } + + public final String replacement () + { + return replacement; + } + + public final CharsetDecoder replaceWith (String newReplacement) + { + if (newReplacement == null) + throw new IllegalArgumentException ("Null replacement"); + if (newReplacement.length () == 0) + throw new IllegalArgumentException ("Empty replacement"); + // XXX: what about maxCharsPerByte? + + this.replacement = newReplacement; + implReplaceWith (newReplacement); + return this; + } + + public final CharsetDecoder reset () + { + state = STATE_RESET; + implReset (); + return this; + } + + public CodingErrorAction unmappableCharacterAction () + { + return unmappableCharacterAction; + } +} diff --git a/libjava/classpath/java/nio/charset/CharsetEncoder.java b/libjava/classpath/java/nio/charset/CharsetEncoder.java new file mode 100644 index 000000000..1f079a3cc --- /dev/null +++ b/libjava/classpath/java/nio/charset/CharsetEncoder.java @@ -0,0 +1,369 @@ +/* CharsetEncoder.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; + +/** + * @author Jesse Rosenstock + * @since 1.4 + */ +public abstract class CharsetEncoder +{ + private static final int STATE_RESET = 0; + private static final int STATE_CODING = 1; + private static final int STATE_END = 2; + private static final int STATE_FLUSHED = 3; + + private static final byte[] DEFAULT_REPLACEMENT = {(byte)'?'}; + + private final Charset charset; + private final float averageBytesPerChar; + private final float maxBytesPerChar; + private byte[] replacement; + + private int state = STATE_RESET; + + private CodingErrorAction malformedInputAction + = CodingErrorAction.REPORT; + private CodingErrorAction unmappableCharacterAction + = CodingErrorAction.REPORT; + + protected CharsetEncoder (Charset cs, float averageBytesPerChar, + float maxBytesPerChar) + { + this (cs, averageBytesPerChar, maxBytesPerChar, DEFAULT_REPLACEMENT); + } + + protected CharsetEncoder (Charset cs, float averageBytesPerChar, + float maxBytesPerChar, byte[] replacement) + { + if (averageBytesPerChar <= 0.0f) + throw new IllegalArgumentException ("Non-positive averageBytesPerChar"); + if (maxBytesPerChar <= 0.0f) + throw new IllegalArgumentException ("Non-positive maxBytesPerChar"); + + this.charset = cs; + this.averageBytesPerChar + = averageBytesPerChar; + this.maxBytesPerChar + = maxBytesPerChar; + this.replacement = replacement; + implReplaceWith (replacement); + } + + public final float averageBytesPerChar () + { + return averageBytesPerChar; + } + + public boolean canEncode (char c) + { + CharBuffer cb = CharBuffer.allocate (1).put (c); + cb.flip (); + return canEncode (cb); + } + + public boolean canEncode (CharSequence cs) + { + CharBuffer cb; + if (cs instanceof CharBuffer) + cb = ((CharBuffer) cs).duplicate (); + else + cb = CharBuffer.wrap (cs); + return canEncode (cb); + } + + private boolean canEncode (CharBuffer cb) + { + // It is an error if a coding operation is "in progress" + // I take that to mean the state is not reset or flushed. + // XXX: check "in progress" everywhere + if (state == STATE_FLUSHED) + reset (); + else if (state != STATE_RESET) + throw new IllegalStateException (); + + CodingErrorAction oldMalformedInputAction = malformedInputAction; + CodingErrorAction oldUnmappableCharacterAction + = unmappableCharacterAction; + + try + { + if (oldMalformedInputAction != CodingErrorAction.REPORT) + onMalformedInput (CodingErrorAction.REPORT); + if (oldUnmappableCharacterAction != CodingErrorAction.REPORT) + onUnmappableCharacter (CodingErrorAction.REPORT); + } + catch (Exception e) + { + return false; + } + finally + { + if (oldMalformedInputAction != CodingErrorAction.REPORT) + onMalformedInput (oldMalformedInputAction); + if (oldUnmappableCharacterAction != CodingErrorAction.REPORT) + onUnmappableCharacter (oldUnmappableCharacterAction); + } + + return true; + } + + public final Charset charset () + { + return charset; + } + + public final ByteBuffer encode (CharBuffer in) + throws CharacterCodingException + { + // XXX: Sun's Javadoc seems to contradict itself saying an + // IllegalStateException is thrown "if a decoding operation is already + // in progress" and also that "it resets this Encoder". + // Should we check to see that the state is reset, or should we + // call reset()? + if (state != STATE_RESET) + throw new IllegalStateException (); + + // REVIEW: Using max instead of average may allocate a very large + // buffer. Maybe we should do something more efficient? + int remaining = in.remaining (); + int n = (int) (remaining * maxBytesPerChar ()); + ByteBuffer out = ByteBuffer.allocate (n); + + if (remaining == 0) + { + state = STATE_FLUSHED; + return out; + } + + CoderResult cr = encode (in, out, true); + if (cr.isError ()) + cr.throwException (); + + cr = flush (out); + if (cr.isError ()) + cr.throwException (); + + out.flip (); + + // Unfortunately, resizing the actual bytebuffer array is required. + byte[] resized = new byte[out.remaining()]; + out.get(resized); + return ByteBuffer.wrap(resized); + } + + public final CoderResult encode (CharBuffer in, ByteBuffer out, + boolean endOfInput) + { + int newState = endOfInput ? STATE_END : STATE_CODING; + // XXX: Need to check for "previous step was an invocation [not] of + // this method with a value of true for the endOfInput parameter but + // a return value indicating an incomplete decoding operation" + // XXX: We will not check the previous return value, just + // that the previous call passed true for endOfInput + if (state != STATE_RESET && state != STATE_CODING + && !(endOfInput && state == STATE_END)) + throw new IllegalStateException (); + state = newState; + + for (;;) + { + CoderResult cr; + try + { + cr = encodeLoop (in, out); + } + catch (RuntimeException e) + { + throw new CoderMalfunctionError (e); + } + + if (cr.isOverflow ()) + return cr; + + if (cr.isUnderflow ()) + { + if (endOfInput && in.hasRemaining ()) + cr = CoderResult.malformedForLength (in.remaining ()); + else + return cr; + } + + CodingErrorAction action = cr.isMalformed () + ? malformedInputAction + : unmappableCharacterAction; + + if (action == CodingErrorAction.REPORT) + return cr; + + if (action == CodingErrorAction.REPLACE) + { + if (out.remaining () < replacement.length) + return CoderResult.OVERFLOW; + out.put (replacement); + } + + in.position (in.position () + cr.length ()); + } + } + + protected abstract CoderResult encodeLoop (CharBuffer in, ByteBuffer out); + + public final CoderResult flush (ByteBuffer out) + { + // It seems weird that you can flush after reset, but Sun's javadoc + // says an IllegalStateException is thrown "If the previous step of the + // current decoding operation was an invocation neither of the reset + // method nor ... of the three-argument encode method with a value of + // true for the endOfInput parameter." + // Further note that flush() only requires that there not be + // an IllegalStateException if the previous step was a call to + // encode with true as the last argument. It does not require + // that the call succeeded. encode() does require that it succeeded. + // XXX: test this to see if reality matches javadoc + if (state != STATE_RESET && state != STATE_END) + throw new IllegalStateException (); + + state = STATE_FLUSHED; + return implFlush (out); + } + + protected CoderResult implFlush (ByteBuffer out) + { + return CoderResult.UNDERFLOW; + } + + protected void implOnMalformedInput (CodingErrorAction newAction) + { + // default implementation does nothing + } + + protected void implOnUnmappableCharacter (CodingErrorAction newAction) + { + // default implementation does nothing + } + + protected void implReplaceWith (byte[] newReplacement) + { + // default implementation does nothing + } + + protected void implReset () + { + // default implementation does nothing + } + + public boolean isLegalReplacement (byte[] replacement) + { + // TODO: cache the decoder + // error actions will be REPORT after construction + CharsetDecoder decoder = charset.newDecoder (); + ByteBuffer bb = ByteBuffer.wrap (replacement); + CharBuffer cb + = CharBuffer.allocate ((int) (replacement.length + * decoder.maxCharsPerByte ())); + return !decoder.decode (bb, cb, true).isError (); + } + + public CodingErrorAction malformedInputAction () + { + return malformedInputAction; + } + + public final float maxBytesPerChar () + { + return maxBytesPerChar; + } + + public final CharsetEncoder onMalformedInput (CodingErrorAction newAction) + { + if (newAction == null) + throw new IllegalArgumentException ("Null action"); + + malformedInputAction = newAction; + implOnMalformedInput (newAction); + return this; + } + + public CodingErrorAction unmappableCharacterAction () + { + return unmappableCharacterAction; + } + + public final CharsetEncoder onUnmappableCharacter + (CodingErrorAction newAction) + { + if (newAction == null) + throw new IllegalArgumentException ("Null action"); + + unmappableCharacterAction = newAction; + implOnUnmappableCharacter (newAction); + return this; + } + + public final byte[] replacement () + { + return replacement; + } + + public final CharsetEncoder replaceWith (byte[] newReplacement) + { + if (newReplacement == null) + throw new IllegalArgumentException ("Null replacement"); + if (newReplacement.length == 0) + throw new IllegalArgumentException ("Empty replacement"); + // XXX: what about maxBytesPerChar? + + if (!isLegalReplacement (newReplacement)) + throw new IllegalArgumentException ("Illegal replacement"); + + this.replacement = newReplacement; + implReplaceWith (newReplacement); + return this; + } + + public final CharsetEncoder reset () + { + state = STATE_RESET; + implReset (); + return this; + } +} diff --git a/libjava/classpath/java/nio/charset/CoderMalfunctionError.java b/libjava/classpath/java/nio/charset/CoderMalfunctionError.java new file mode 100644 index 000000000..f7a32d207 --- /dev/null +++ b/libjava/classpath/java/nio/charset/CoderMalfunctionError.java @@ -0,0 +1,54 @@ +/* CoderMalfunctionError.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +/** + * @since 1.4 + */ +public class CoderMalfunctionError extends Error +{ + private static final long serialVersionUID = - 1151412348057794301L; + + /** + * Creates the error + */ + public CoderMalfunctionError(Exception cause) + { + super (cause); + } +} diff --git a/libjava/classpath/java/nio/charset/CoderResult.java b/libjava/classpath/java/nio/charset/CoderResult.java new file mode 100644 index 000000000..b083d840e --- /dev/null +++ b/libjava/classpath/java/nio/charset/CoderResult.java @@ -0,0 +1,189 @@ +/* CoderResult.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +import java.lang.ref.WeakReference; +import java.nio.BufferOverflowException; +import java.nio.BufferUnderflowException; +import java.util.HashMap; + +/** + * @author Jesse Rosenstock + * @since 1.4 + */ +public class CoderResult +{ + private static final int TYPE_MALFORMED = 0; + private static final int TYPE_OVERFLOW = 1; + private static final int TYPE_UNDERFLOW = 2; + private static final int TYPE_UNMAPPABLE = 3; + + public static final CoderResult OVERFLOW + = new CoderResult (TYPE_OVERFLOW, 0); + public static final CoderResult UNDERFLOW + = new CoderResult (TYPE_UNDERFLOW, 0); + + private static final String[] names + = { "MALFORMED", "OVERFLOW", "UNDERFLOW", "UNMAPPABLE" }; + + private static final Cache malformedCache + = new Cache () + { + protected CoderResult make (int length) + { + return new CoderResult (TYPE_MALFORMED, length); + } + }; + + private static final Cache unmappableCache + = new Cache () + { + protected CoderResult make (int length) + { + return new CoderResult (TYPE_UNMAPPABLE, length); + } + }; + + private final int type; + private final int length; + + // Package-private to avoid a trampoline constructor. + CoderResult (int type, int length) + { + this.type = type; + this.length = length; + } + + public boolean isError () + { + return length > 0; + } + + public boolean isMalformed () + { + return type == TYPE_MALFORMED; + } + + public boolean isOverflow () + { + return type == TYPE_OVERFLOW; + } + + public boolean isUnderflow () + { + return type == TYPE_UNDERFLOW; + } + + public boolean isUnmappable () + { + return type == TYPE_UNMAPPABLE; + } + + public int length () + { + if (length <= 0) + throw new UnsupportedOperationException (); + else + return length; + } + + public static CoderResult malformedForLength (int length) + { + return malformedCache.get (length); + } + + public void throwException () + throws CharacterCodingException + { + switch (type) + { + case TYPE_MALFORMED: + throw new MalformedInputException (length); + case TYPE_OVERFLOW: + throw new BufferOverflowException (); + case TYPE_UNDERFLOW: + throw new BufferUnderflowException (); + case TYPE_UNMAPPABLE: + throw new UnmappableCharacterException (length); + } + } + + public String toString () + { + String name = names[type]; + return (length > 0) ? name + '[' + length + ']' : name; + } + + public static CoderResult unmappableForLength (int length) + { + return unmappableCache.get (length); + } + + private abstract static class Cache + { + private final HashMap cache; + + // Package-private to avoid a trampoline constructor. + Cache () + { + cache = new HashMap (); + } + + // Package-private to avoid a trampoline. + synchronized CoderResult get (int length) + { + if (length <= 0) + throw new IllegalArgumentException ("Non-positive length"); + + Integer len = Integer.valueOf (length); + CoderResult cr = null; + Object o; + if ((o = cache.get (len)) != null) + cr = (CoderResult) ((WeakReference) o).get (); + if (cr == null) + { + cr = make (length); + cache.put (len, new WeakReference (cr)); + } + + return cr; + } + + protected abstract CoderResult make (int length); + } +} diff --git a/libjava/classpath/java/nio/charset/CodingErrorAction.java b/libjava/classpath/java/nio/charset/CodingErrorAction.java new file mode 100644 index 000000000..3b6403097 --- /dev/null +++ b/libjava/classpath/java/nio/charset/CodingErrorAction.java @@ -0,0 +1,66 @@ +/* CodingErrorAction.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +public class CodingErrorAction +{ + public static final CodingErrorAction IGNORE + = new CodingErrorAction("ignore"); + public static final CodingErrorAction REPLACE + = new CodingErrorAction("replace"); + public static final CodingErrorAction REPORT + = new CodingErrorAction("report"); + + private final String name; + + /** + * Private constructor only used to create the constant CodingErrorActions. + */ + private CodingErrorAction(String name) + { + this.name = name; + } + + /** + * Returns the name of the CodingErrorAction. + */ + public String toString () + { + return name; + } +} diff --git a/libjava/classpath/java/nio/charset/IllegalCharsetNameException.java b/libjava/classpath/java/nio/charset/IllegalCharsetNameException.java new file mode 100644 index 000000000..0e05d4dbf --- /dev/null +++ b/libjava/classpath/java/nio/charset/IllegalCharsetNameException.java @@ -0,0 +1,73 @@ +/* IllegalCharsetNameException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +/** + * @author Michael Koch + * @since 1.4 + */ +public class IllegalCharsetNameException extends IllegalArgumentException +{ + /** + * Compatible with JDK 1.4+ + */ + private static final long serialVersionUID = 1457525358470002989L; + + private String charsetName; + + /** + * Creates the exception + * + * @param charsetName name of the illegal charset + */ + public IllegalCharsetNameException (String charsetName) + { + super (); + this.charsetName = charsetName; + } + + /** + * Retrieves the illegal charset name + * + * @return the illegal charset name + */ + public String getCharsetName () + { + return charsetName; + } +} diff --git a/libjava/classpath/java/nio/charset/MalformedInputException.java b/libjava/classpath/java/nio/charset/MalformedInputException.java new file mode 100644 index 000000000..3870b55be --- /dev/null +++ b/libjava/classpath/java/nio/charset/MalformedInputException.java @@ -0,0 +1,79 @@ +/* MalformedInputException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +/** + * @since 1.4 + */ +public class MalformedInputException extends CharacterCodingException +{ + private static final long serialVersionUID = - 3438823399834806194L; + + private int inputLength; + + /** + * Creates the exception + * + * @param inputLength the position of malformed input in the input stream + */ + public MalformedInputException (int inputLength) + { + super (); + this.inputLength = inputLength; + } + + /** + * Retrieves the position of the malformed input in the input stream. + * + * @return the position + */ + public int getInputLength () + { + return inputLength; + } + + /** + * Returns the detail message string of this throwable + * + * @return the message + */ + public String getMessage () + { + return "Input length = " + inputLength; + } +} diff --git a/libjava/classpath/java/nio/charset/UnmappableCharacterException.java b/libjava/classpath/java/nio/charset/UnmappableCharacterException.java new file mode 100644 index 000000000..d9cdb49cf --- /dev/null +++ b/libjava/classpath/java/nio/charset/UnmappableCharacterException.java @@ -0,0 +1,73 @@ +/* UnmappableCharacterException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +/** + * @since 1.4 + */ +public class UnmappableCharacterException extends CharacterCodingException +{ + private static final long serialVersionUID = - 7026962371537706123L; + + private int inputLength; + + /** + * Creates the exception + */ + public UnmappableCharacterException (int inputLength) + { + super (); + this.inputLength = inputLength; + } + + /** + * Retrieves the illegal charset name + */ + public int getInputLength () + { + return inputLength; + } + + /** + * Returns the detail message string of this throwable + */ + public String getMessage () + { + return "Input length = " + inputLength; + } +} diff --git a/libjava/classpath/java/nio/charset/UnsupportedCharsetException.java b/libjava/classpath/java/nio/charset/UnsupportedCharsetException.java new file mode 100644 index 000000000..c4f9d17fd --- /dev/null +++ b/libjava/classpath/java/nio/charset/UnsupportedCharsetException.java @@ -0,0 +1,69 @@ +/* UnsupportedCharsetException.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset; + +/** + * @author Michael Koch + * @since 1.4 + */ +public class UnsupportedCharsetException extends IllegalArgumentException +{ + /** + * Compatible with JDK 1.4+ + */ + private static final long serialVersionUID = 1490765524727386367L; + + String charsetName; + + /** + * Creates the exception + */ + public UnsupportedCharsetException (String charsetName) + { + super (); + this.charsetName = charsetName; + } + + /** + * Retrieves the illegal charset name + */ + public String getCharsetName () + { + return charsetName; + } +} diff --git a/libjava/classpath/java/nio/charset/package.html b/libjava/classpath/java/nio/charset/package.html new file mode 100644 index 000000000..acc7c0203 --- /dev/null +++ b/libjava/classpath/java/nio/charset/package.html @@ -0,0 +1,47 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in java.nio.charset package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - java.nio.charset</title></head> + +<body> +<p>Classes to manipulate, encode and decode different character sets from +and to bytes.</p> + +</body> +</html> diff --git a/libjava/classpath/java/nio/charset/spi/CharsetProvider.java b/libjava/classpath/java/nio/charset/spi/CharsetProvider.java new file mode 100644 index 000000000..03653f807 --- /dev/null +++ b/libjava/classpath/java/nio/charset/spi/CharsetProvider.java @@ -0,0 +1,95 @@ +/* CharsetProvider.java -- charset service provider interface + Copyright (C) 2002, 2005, 2006 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.nio.charset.spi; + +import java.nio.charset.Charset; +import java.util.Iterator; + + +/** + * This class allows an implementor to provide additional character sets. The + * subclass must have a nullary constructor, and be attached to charset + * implementation classes. These extensions are loaded via the context class + * loader. To provide the charset extension, all files named + * <code>META-INF/services/java.nio.charset.spi.CharsetProvider</code> are + * read from the classpath. Each one should be a UTF-8 encoded list of + * fully-qualified names of concrete subclasses of this class; whitespace is + * ignored, and '#' starts comments. Duplicates are ignored. The + * implementations must be accessible to the classloader that requests them. + * + * @author Eric Blake (ebb9@email.byu.edu) + * @see Charset + * @since 1.4 + * @status updated to 1.4 + */ +public abstract class CharsetProvider +{ + /** + * Initialize a new charset provider. This performs a security check on + * RuntimePermission("charsetProvider"). + * + * @throws SecurityException if building a new set is not allowed + */ + protected CharsetProvider() + { + // We only do the security check for custom providers, not for the + // built in ones. + SecurityManager s = System.getSecurityManager(); + if (s != null && + ! (this instanceof gnu.java.nio.charset.Provider + || this instanceof gnu.java.nio.charset.iconv.IconvProvider)) + s.checkPermission(new RuntimePermission("charsetProvider")); + } + + /** + * Returns an iterator over the charsets defined by this provider. + * + * @return the iterator + * @see Charset#availableCharsets() + */ + public abstract Iterator<Charset> charsets(); + + /** + * Returns the named charset, by canonical name or alias. + * + * @param name the name of the character + * + * @return the charset, or null if not supported + */ + public abstract Charset charsetForName(String name); +} // class CharsetProvider diff --git a/libjava/classpath/java/nio/charset/spi/package.html b/libjava/classpath/java/nio/charset/spi/package.html new file mode 100644 index 000000000..dd47a250b --- /dev/null +++ b/libjava/classpath/java/nio/charset/spi/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in java.nio.charset.spi package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - java.nio.charset.spi</title></head> + +<body> +<p>Classes to provide new character sets.</p> + +</body> +</html> diff --git a/libjava/classpath/java/nio/package.html b/libjava/classpath/java/nio/package.html new file mode 100644 index 000000000..274498be3 --- /dev/null +++ b/libjava/classpath/java/nio/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in java.nio package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - java.nio</title></head> + +<body> +<p>Abstract classes for manipulating buffers of different primitive types.</p> + +</body> +</html> |