summaryrefslogtreecommitdiff
path: root/gnu/java
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/java')
-rw-r--r--gnu/java/nio/PipeImpl.java32
-rw-r--r--gnu/java/nio/SelectorImpl.java2
-rw-r--r--gnu/java/nio/SocketChannelImpl.java120
-rw-r--r--gnu/java/nio/channels/FileChannelImpl.java49
4 files changed, 75 insertions, 128 deletions
diff --git a/gnu/java/nio/PipeImpl.java b/gnu/java/nio/PipeImpl.java
index f7b01c8b7..cccaa3988 100644
--- a/gnu/java/nio/PipeImpl.java
+++ b/gnu/java/nio/PipeImpl.java
@@ -37,6 +37,7 @@ exception statement from your version. */
package gnu.java.nio;
+
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
@@ -47,12 +48,14 @@ class PipeImpl extends Pipe
public static final class SourceChannelImpl extends Pipe.SourceChannel
{
private int native_fd;
+ private VMChannel vmch;
public SourceChannelImpl (SelectorProvider selectorProvider,
int native_fd)
{
super (selectorProvider);
this.native_fd = native_fd;
+ vmch = VMChannel.getVMChannel(this);
}
protected final void implCloseSelectableChannel()
@@ -64,19 +67,19 @@ class PipeImpl extends Pipe
protected void implConfigureBlocking (boolean blocking)
throws IOException
{
- throw new Error ("Not implemented");
+ vmch.setBlocking(blocking);
}
public final int read (ByteBuffer src)
throws IOException
{
- throw new Error ("Not implemented");
+ return vmch.read(src);
}
public final long read (ByteBuffer[] srcs)
throws IOException
{
- return read (srcs, 0, srcs.length);
+ return vmch.readScattering(srcs, 0, srcs.length);
}
public final synchronized long read (ByteBuffer[] srcs, int offset,
@@ -89,13 +92,7 @@ class PipeImpl extends Pipe
|| len > srcs.length - offset)
throw new IndexOutOfBoundsException();
- long bytesRead = 0;
-
- for (int index = 0; index < len; index++)
- bytesRead += read (srcs [offset + index]);
-
- return bytesRead;
-
+ return vmch.readScattering(srcs, offset, len);
}
public final int getNativeFD()
@@ -107,12 +104,14 @@ class PipeImpl extends Pipe
public static final class SinkChannelImpl extends Pipe.SinkChannel
{
private int native_fd;
+ private VMChannel vmch;
public SinkChannelImpl (SelectorProvider selectorProvider,
int native_fd)
{
super (selectorProvider);
this.native_fd = native_fd;
+ vmch = VMChannel.getVMChannel(this);
}
protected final void implCloseSelectableChannel()
@@ -124,19 +123,19 @@ class PipeImpl extends Pipe
protected final void implConfigureBlocking (boolean blocking)
throws IOException
{
- throw new Error ("Not implemented");
+ vmch.setBlocking(blocking);
}
public final int write (ByteBuffer dst)
throws IOException
{
- throw new Error ("Not implemented");
+ return vmch.write(dst);
}
public final long write (ByteBuffer[] srcs)
throws IOException
{
- return write (srcs, 0, srcs.length);
+ return vmch.writeGathering(srcs, 0, srcs.length);
}
public final synchronized long write (ByteBuffer[] srcs, int offset, int len)
@@ -147,13 +146,8 @@ class PipeImpl extends Pipe
|| len < 0
|| len > srcs.length - offset)
throw new IndexOutOfBoundsException();
-
- long bytesWritten = 0;
- for (int index = 0; index < len; index++)
- bytesWritten += write (srcs [offset + index]);
-
- return bytesWritten;
+ return vmch.writeGathering(srcs, offset, len);
}
public final int getNativeFD()
diff --git a/gnu/java/nio/SelectorImpl.java b/gnu/java/nio/SelectorImpl.java
index e10f71574..7639c9ae3 100644
--- a/gnu/java/nio/SelectorImpl.java
+++ b/gnu/java/nio/SelectorImpl.java
@@ -379,6 +379,8 @@ public class SelectorImpl extends AbstractSelector
result = new DatagramChannelSelectionKey (ch, this);
else if (ch instanceof ServerSocketChannelImpl)
result = new ServerSocketChannelSelectionKey (ch, this);
+ else if (ch instanceof gnu.java.nio.SocketChannelImpl)
+ result = new gnu.java.nio.SocketChannelSelectionKeyImpl((gnu.java.nio.SocketChannelImpl)ch, this);
else
throw new InternalError ("No known channel type");
diff --git a/gnu/java/nio/SocketChannelImpl.java b/gnu/java/nio/SocketChannelImpl.java
index 680eba2f9..697987eb6 100644
--- a/gnu/java/nio/SocketChannelImpl.java
+++ b/gnu/java/nio/SocketChannelImpl.java
@@ -41,8 +41,6 @@ package gnu.java.nio;
import gnu.java.net.PlainSocketImpl;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
@@ -110,7 +108,8 @@ public final class SocketChannelImpl extends SocketChannel
protected void implConfigureBlocking (boolean blocking) throws IOException
{
- socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
+ VMChannel vmch = VMChannel.getVMChannel(impl);
+ vmch.setBlocking(blocking);
}
public boolean connect (SocketAddress remote) throws IOException
@@ -215,25 +214,6 @@ public final class SocketChannelImpl extends SocketChannel
{
if (!isConnected())
throw new NotYetConnectedException();
-
- byte[] data;
- int offset = 0;
- InputStream input = socket.getInputStream();
- int available = input.available();
- int len = dst.remaining();
-
- if ((! isBlocking()) && available == 0)
- return 0;
-
- if (dst.hasArray())
- {
- offset = dst.arrayOffset() + dst.position();
- data = dst.array();
- }
- else
- {
- data = new byte [len];
- }
int readBytes = 0;
boolean completed = false;
@@ -241,8 +221,8 @@ public final class SocketChannelImpl extends SocketChannel
try
{
begin();
- socket.getPlainSocketImpl().setInChannelOperation(true);
- readBytes = input.read (data, offset, len);
+ VMChannel vmch = VMChannel.getVMChannel(impl);
+ readBytes = vmch.read(dst);
completed = true;
}
finally
@@ -251,15 +231,6 @@ public final class SocketChannelImpl extends SocketChannel
socket.getPlainSocketImpl().setInChannelOperation(false);
}
- if (readBytes > 0)
- if (dst.hasArray())
- {
- dst.position (dst.position() + readBytes);
- }
- else
- {
- dst.put (data, offset, readBytes);
- }
return readBytes;
}
@@ -270,16 +241,22 @@ public final class SocketChannelImpl extends SocketChannel
if (!isConnected())
throw new NotYetConnectedException();
- if ((offset < 0)
- || (offset > dsts.length)
- || (length < 0)
- || (length > (dsts.length - offset)))
- throw new IndexOutOfBoundsException();
-
long readBytes = 0;
+
+ boolean completed = false;
- for (int index = offset; index < length; index++)
- readBytes += read (dsts [index]);
+ try
+ {
+ begin();
+ VMChannel vmch = VMChannel.getVMChannel(impl);
+ readBytes = vmch.readScattering(dsts, offset, length);
+ completed = true;
+ }
+ finally
+ {
+ end (completed);
+ socket.getPlainSocketImpl().setInChannelOperation(false);
+ }
return readBytes;
}
@@ -289,30 +266,15 @@ public final class SocketChannelImpl extends SocketChannel
{
if (!isConnected())
throw new NotYetConnectedException();
-
- byte[] data;
- int offset = 0;
- int len = src.remaining();
-
- if (!src.hasArray())
- {
- data = new byte [len];
- src.get (data, 0, len);
- }
- else
- {
- offset = src.arrayOffset() + src.position();
- data = src.array();
- }
- OutputStream output = socket.getOutputStream();
+ int readBytes = 0;
boolean completed = false;
try
{
begin();
- socket.getPlainSocketImpl().setInChannelOperation(true);
- output.write (data, offset, len);
+ VMChannel vmch = VMChannel.getVMChannel(impl);
+ readBytes = vmch.write(src);
completed = true;
}
finally
@@ -321,12 +283,8 @@ public final class SocketChannelImpl extends SocketChannel
socket.getPlainSocketImpl().setInChannelOperation(false);
}
- if (src.hasArray())
- {
- src.position (src.position() + len);
- }
-
- return len;
+
+ return readBytes;
}
public long write (ByteBuffer[] srcs, int offset, int length)
@@ -334,18 +292,24 @@ public final class SocketChannelImpl extends SocketChannel
{
if (!isConnected())
throw new NotYetConnectedException();
-
- if ((offset < 0)
- || (offset > srcs.length)
- || (length < 0)
- || (length > (srcs.length - offset)))
- throw new IndexOutOfBoundsException();
-
- long writtenBytes = 0;
-
- for (int index = offset; index < length; index++)
- writtenBytes += write (srcs [index]);
-
- return writtenBytes;
+
+ long readBytes = 0;
+ boolean completed = false;
+
+ try
+ {
+ begin();
+ VMChannel vmch = VMChannel.getVMChannel(impl);
+ readBytes = vmch.writeGathering(srcs, offset, length);
+ completed = true;
+ }
+ finally
+ {
+ end (completed);
+ socket.getPlainSocketImpl().setInChannelOperation(false);
+ }
+
+
+ return readBytes;
}
}
diff --git a/gnu/java/nio/channels/FileChannelImpl.java b/gnu/java/nio/channels/FileChannelImpl.java
index 671ae5bb1..ed439e141 100644
--- a/gnu/java/nio/channels/FileChannelImpl.java
+++ b/gnu/java/nio/channels/FileChannelImpl.java
@@ -40,6 +40,7 @@ package gnu.java.nio.channels;
import gnu.classpath.Configuration;
import gnu.java.nio.FileLockImpl;
+import gnu.java.nio.VMChannel;
import java.io.File;
import java.io.FileNotFoundException;
@@ -102,6 +103,7 @@ public final class FileChannelImpl extends FileChannel
// we want to make sure this has the value -1. This is the most
// efficient way to accomplish that.
private int fd = -1;
+ private VMChannel ch;
private int mode;
@@ -123,6 +125,7 @@ public final class FileChannelImpl extends FileChannel
description = path;
fd = open (path, mode);
this.mode = mode;
+ this.ch = VMChannel.getVMChannel(this);
// First open the file and then check if it is a a directory
// to avoid race condition.
@@ -155,6 +158,7 @@ public final class FileChannelImpl extends FileChannel
this.fd = fd;
this.mode = mode;
this.description = "descriptor(" + fd + ")";
+ this.ch = VMChannel.getVMChannel(this);
}
private native int open (String path, int mode) throws FileNotFoundException;
@@ -181,6 +185,7 @@ public final class FileChannelImpl extends FileChannel
public int read (ByteBuffer dst) throws IOException
{
+ /*
int result;
byte[] buffer = new byte [dst.remaining ()];
@@ -190,6 +195,8 @@ public final class FileChannelImpl extends FileChannel
dst.put (buffer, 0, result);
return result;
+ */
+ return ch.read(dst);
}
public int read (ByteBuffer dst, long position)
@@ -214,33 +221,12 @@ public final class FileChannelImpl extends FileChannel
public long read (ByteBuffer[] dsts, int offset, int length)
throws IOException
{
- long result = 0;
-
- for (int i = offset; i < offset + length; i++)
- {
- result += read (dsts [i]);
- }
-
- return result;
+ return ch.readScattering(dsts, offset, length);
}
public int write (ByteBuffer src) throws IOException
{
- int len = src.remaining ();
- if (src.hasArray())
- {
- byte[] buffer = src.array();
- write(buffer, src.arrayOffset() + src.position(), len);
- src.position(src.position() + len);
- }
- else
- {
- // Use a more efficient native method! FIXME!
- byte[] buffer = new byte [len];
- src.get (buffer, 0, len);
- write (buffer, 0, len);
- }
- return len;
+ return ch.write(src);
}
public int write (ByteBuffer src, long position)
@@ -274,14 +260,7 @@ public final class FileChannelImpl extends FileChannel
public long write(ByteBuffer[] srcs, int offset, int length)
throws IOException
{
- long result = 0;
-
- for (int i = offset;i < offset + length;i++)
- {
- result += write (srcs[i]);
- }
-
- return result;
+ return ch.writeGathering(srcs, offset, length);
}
public native MappedByteBuffer mapImpl (char mode, long position, int size)
@@ -563,4 +542,12 @@ public final class FileChannelImpl extends FileChannel
+ ",mode=" + mode + ","
+ description + "]");
}
+
+ /**
+ * @return The native file descriptor.
+ */
+ public int getNativeFD()
+ {
+ return fd;
+ }
}