Buffer.java: Implemented.

2002-11-13  Michael Koch <konqueror@gmx.de>

	* java/nio/Buffer.java: Implemented.
	* java/nio/CharBuffer.java: New file.
	* java/nio/InvalidMarkException.java: New file.
	* java/nio/channels/DatagramChannel.java: Implemented.
	* java/nio/channels/ServerSocketChannel.java: Implemented.
	* java/nio/channels/SocketChannel.java: Implemented.
	* java/nio/channels/spi/AbstractChannel.java: Removed.
	* java/nio/channels/spi/AbstractSelectableChannel.java:
	Implemented.
	* java/nio/charset/Charset.java:
	Merge from Classpath.
	* java/nio/charset/CharsetDecoder.java: New file.
	* java/nio/charset/CharsetEncoder.java: New file.
	* java/nio/charset/CoderResult.java: New file.
	* Makefile.am (ordinary_java_source_files): Added new files.
	* Makefile.in: Regenerated.

From-SVN: r59075
This commit is contained in:
Michael Koch 2002-11-13 12:21:26 +00:00 committed by Michael Koch
parent 7b53becc10
commit 93f93f9f28
14 changed files with 2006 additions and 82 deletions

View file

@ -37,14 +37,117 @@ 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
{
public DatagramChannel (SelectorProvider provider)
/**
* Initializes the channel.
*/
protected DatagramChannel (SelectorProvider provider)
{
super (provider);
}
/**
* Opens a datagram channel.
*/
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.
*/
public final long write (ByteBuffer[] srcs)
{
long b = 0;
for (int i = 0;i < srcs.length; i++)
b += write (srcs[i]);
return b;
}
/**
* Connects this channel's socket.
*/
public abstract DatagramChannel connect (SocketAddress remote);
/**
* Disonnects this channel's socket.
*/
public abstract DatagramChannel disconnect ();
/**
* Tells whether or not this channel's socket is connected.
*/
public abstract boolean isConnected ();
/**
* Reads data from this channel.
*/
public abstract int read (ByteBuffer dst);
/**
* Reads data from this channel.
*/
public abstract long read (ByteBuffer[] dsts, int offset, int length);
/**
* Receives a datagram via this channel.
*/
public abstract SocketAddress receive (ByteBuffer dst);
/**
* Sends a datagram via this channel.
*/
public abstract int send (ByteBuffer src, SocketAddress target);
/**
* Retrieves the channel's socket.
*/
public abstract DatagramSocket socket ();
/**
* Writes data to this channel.
*/
public abstract int write (ByteBuffer src);
/**
* Writes data to this channel.
*/
public abstract long write (ByteBuffer[] srcs, int offset, int length);
/**
* Retrieves the valid operations for this channel.
*/
public final int validOps ()
{
return SelectionKey.OP_READ | SelectionKey.OP_WRITE;
}
}

View file

@ -39,12 +39,49 @@ package java.nio.channels;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.net.ServerSocket;
/**
* @author Michael Koch
* @since 1.4
*/
public abstract class ServerSocketChannel
extends AbstractSelectableChannel
{
/**
* Initializes this channel.
*/
public ServerSocketChannel (SelectorProvider provider)
{
super (provider);
}
/**
* Accepts a connection made to this channel's socket.
*/
public abstract SocketChannel accept ();
/**
* Retrieves the channels socket.
*/
public abstract ServerSocket socket ();
/**
* Opens a server socker channel.
*/
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;
}
}

View file

@ -39,12 +39,127 @@ package java.nio.channels;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
public abstract class SocketChannel
extends AbstractSelectableChannel
/**
* @author Michael Koch
* @since 1.4
*/
abstract public class SocketChannel extends AbstractSelectableChannel
{
public SocketChannel (SelectorProvider provider)
/**
* Initializes this socket.
*/
protected SocketChannel (SelectorProvider provider)
{
super (provider);
}
/**
* Opens a socket channel.
*/
public static SocketChannel open () throws IOException
{
return SelectorProvider.provider ().openSocketChannel ();
}
/**
* Opens a channel and connects it to a remote address.
*/
public static SocketChannel open (SocketAddress remote) throws IOException
{
SocketChannel ch = open ();
if (ch.connect (remote))
{
}
return ch;
}
/**
* Reads data from the channel.
*/
public final long read (ByteBuffer[] dsts)
{
long b = 0;
for (int i = 0; i < dsts.length; i++)
{
b += read (dsts [i]);
}
return b;
}
/**
* Writes data to the channel.
*/
public final long write (ByteBuffer[] dsts)
{
long b = 0;
for (int i= 0; i < dsts.length; i++)
{
b += write (dsts [i]);
}
return b;
}
/**
* Retrieves the valid operations for this channel.
*/
public final int validOps ()
{
return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE;
}
/**
* Reads data from the channel.
*/
public abstract int read (ByteBuffer dst);
/**
* Connects the channel's socket to the remote address.
*/
public abstract boolean connect (SocketAddress remote) throws IOException;
/**
* Finishes the process of connecting a socket channel.
*/
public abstract boolean finishConnect ();
/**
* 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.
*/
public abstract long read (ByteBuffer[] dsts, int offset, int length);
/**
* Retrieves the channel's socket.
*/
public abstract Socket socket ();
/**
* Writes data to the channel.
*/
public abstract int write (ByteBuffer src);
/**
* Writes data to the channel.
*/
public abstract long write (ByteBuffer[] srcs, int offset, int length);
}

View file

@ -1,57 +0,0 @@
/* AbstractChannel.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., 59 Temple Place, Suite 330, Boston, MA
02111-1307 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.Channel;
public abstract class AbstractChannel implements Channel
{
boolean opened;
public boolean isOpen()
{
return opened;
}
public void close() throws IOException
{
if (! isOpen())
return;
}
}

View file

@ -1,4 +1,4 @@
/* AbstractSelectableChannel.java --
/* AbstractSelectableChannel.java
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@ -37,18 +37,171 @@ 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.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public abstract class AbstractSelectableChannel
extends SelectableChannel
public abstract class AbstractSelectableChannel extends SelectableChannel
{
int registered;
boolean blocking = true;
Object LOCK = new Object ();
SelectorProvider provider;
List keys;
/**
* Initializes the channel
*/
protected AbstractSelectableChannel (SelectorProvider provider)
{
this.provider = provider;
}
/**
* Retrieves the object upon which the configureBlocking and register
* methods synchronize.
*/
public final Object blockingLock ()
{
return LOCK;
}
/**
* Adjusts this channel's blocking mode.
*/
public final SelectableChannel configureBlocking (boolean block)
{
synchronized (LOCK)
{
blocking = true;
implConfigureBlocking (block);
}
return this;
}
/**
* Closes this channel.
*/
protected final void implCloseChannel ()
{
implCloseSelectableChannel ();
}
/**
* Closes this selectable channel.
*/
protected abstract void implCloseSelectableChannel ();
/**
* Adjusts this channel's blocking mode.
*/
protected abstract void implConfigureBlocking (boolean block);
/**
* Tells whether or not every I/O operation on this channel will block
* until it completes.
*/
public final boolean isBlocking()
{
return true;
return blocking;
}
/**
* Tells whether or not this channel is currently registered with
* any selectors.
*/
public final boolean isRegistered()
{
return registered > 0;
}
/**
* Retrieves the key representing the channel's registration with the
* given selector.
*/
public final SelectionKey keyFor(Selector selector)
{
try
{
return register (selector, 0, null);
}
catch (Exception e)
{
return null;
}
}
/**
* Returns the provider that created this channel.
*/
public final SelectorProvider provider ()
{
return provider;
}
private SelectionKey locate (Selector selector)
{
if (keys == null)
return null;
SelectionKey k = null;
ListIterator it = keys.listIterator ();
while (it.hasNext ())
{
k = (SelectionKey) it.next ();
if (k.selector () == selector)
{
return k;
}
}
return k;
}
private void add (SelectionKey key)
{
if (keys == null)
keys = new LinkedList ();
keys.add (key);
}
/**
* Registers this channel with the given selector, returning a selection key.
*/
public final SelectionKey register (Selector selin, int ops, Object att)
throws ClosedChannelException
{
if (!isOpen ())
throw new ClosedChannelException();
SelectionKey k = null;
AbstractSelector selector = (AbstractSelector) selin;
synchronized (LOCK)
{
k = locate (selector);
if (k != null)
{
k.attach (att);
}
else
{
k = selector.register (this, ops, att);
if (k != null)
add (k);
}
}
return k;
}
}