Author: mturk
Date: Thu Jul 14 07:06:01 2011
New Revision: 1146580

URL: http://svn.apache.org/viewvc?rev=1146580&view=rev
Log:
Implement stream JNI methods

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
    commons/sandbox/runtime/trunk/src/main/native/configure
    commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw
    commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
 Thu Jul 14 07:06:01 2011
@@ -38,7 +38,8 @@ public abstract class Descriptor impleme
 {
 
     private static native void    retain0(long fd);
-    private static native void    release0(long fd);
+    private static native boolean release0(long fd)
+        throws IllegalStateException;
     private static native boolean unique0(long fd);
     
     /** Operating system descriptor.
@@ -121,7 +122,7 @@ public abstract class Descriptor impleme
      * @return {@code true} if descriptor is valid and not closed
      *         {@code false} otherwise.
      */
-    public final boolean valid()
+    public synchronized final boolean valid()
     {
         // true if both int is negative or zero
         // Descriptor is always assured to be above the stderr (#3)
@@ -136,7 +137,7 @@ public abstract class Descriptor impleme
      *
      * @return {@code true} if descriptor is closed; {@code false} otherwise.
      */
-    public final boolean closed()
+    public synchronized final boolean closed()
     {
         if (closed || fd == 0L)
             return true;
@@ -188,13 +189,16 @@ public abstract class Descriptor impleme
      *
      * @param fd operating system descriptor.
      * @return {@code true} if the object can be released.
+     *
+     * @throws IllegalStateException
+     *          if the reference counter goes below zero
      */
-    public static void release(final long fd)
-        throws InvalidDescriptorException
+    public static boolean release(final long fd)
+        throws IllegalStateException, InvalidDescriptorException
     {
         if (fd == 0L)
             throw new InvalidDescriptorException();        
-        release0(fd);
+        return release0(fd);
     }
 
     /**

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
 Thu Jul 14 07:06:01 2011
@@ -143,7 +143,7 @@ public interface Reader
      * @throws IOException
      *          If some other I/O error occurs.
      */
-    public long read(Pointer pointer)
+    public int read(Pointer pointer)
         throws NullPointerException, IOException;
 
     /**
@@ -180,7 +180,7 @@ public interface Reader
      * @throws IOException
      *          If some other I/O error occurs.
      */
-    public long read(Pointer pointer, long offset, long count)
+    public int read(Pointer pointer, long offset, int count)
         throws NullPointerException, IndexOutOfBoundsException, IOException;
 
     /**

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
 Thu Jul 14 07:06:01 2011
@@ -20,6 +20,8 @@ package org.apache.commons.runtime.io;
 
 import java.io.IOException;
 import java.io.SyncFailedException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.Pointer;
 
 /**
  * Bidirectional Stream.
@@ -164,5 +166,47 @@ public abstract class Stream
             // Ignore exceptions on finalize
         }
     }
-        
+
+    @Override
+    public final int read(byte[] buffer)
+        throws IOException
+    {
+        return read(buffer, 0, buffer.length);
+    }
+
+    @Override
+    public final int read(Pointer pointer)
+        throws IOException
+    {
+        return read(pointer, 0L, (int)pointer.sizeof());
+    }
+
+    @Override
+    public final int read(ByteBuffer buffer)
+        throws IOException
+    {
+        return read(buffer, buffer.position(), buffer.remaining());
+    }
+
+    @Override
+    public final int write(byte[] buffer)
+        throws IOException
+    {
+        return write(buffer, 0, buffer.length);
+    }
+
+    @Override
+    public final int write(Pointer pointer)
+        throws IOException
+    {
+        return write(pointer, 0L, (int)pointer.sizeof());
+    }
+
+    @Override
+    public final int write(ByteBuffer buffer)
+        throws IOException
+    {
+        return write(buffer, buffer.position(), buffer.remaining());
+    }
+
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
 Thu Jul 14 07:06:01 2011
@@ -51,6 +51,31 @@ public interface Writer extends Syncable
         throws IOException;
 
     /**
+     * Writes the entire content of the byte array {@code buffer} to this
+     * stream.
+     *
+     * @param buffer
+     *          The buffer to write to this stream.
+     * @return The number of bytes actually written.
+     *
+     * @throws IndexOutOfBoundsException
+     *          If {@code offset < 0} or {@code count < 0}, or if
+     *          {@code offset + count} is greater than the size of
+     *          {@code buffer}.
+     * @throws ClosedDescriptorException
+     *          If this stream is closed.
+     * @throws OperationWouldBlockException
+     *          If the stream is in nonblocking mode and the operation
+     *          would block.
+     * @throws TimeoutException
+     *          If write operation times out.
+     * @throws IOException
+     *          If some other I/O error occurs.
+     */
+    public int write(byte[] buffer)
+        throws IndexOutOfBoundsException, IOException;
+        
+    /**
      * Writes {@code count} bytes from the byte array {@code buffer} to this
      * stream, starting at the current file pointer and using {@code offset}
      * as the first position within {@code buffer} to get bytes.
@@ -100,7 +125,7 @@ public interface Writer extends Syncable
      * @throws IOException
      *          If some other I/O error occurs.
      */
-    public long write(Pointer pointer)
+    public int write(Pointer pointer)
         throws NullPointerException, IOException;
 
     /**
@@ -133,7 +158,7 @@ public interface Writer extends Syncable
      * @throws IOException
      *          If some other I/O error occurs.
      */
-    public long write(Pointer pointer, int offset, int count)
+    public int write(Pointer pointer, long offset, int count)
         throws IndexOutOfBoundsException, IOException;
 
     /**

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
 Thu Jul 14 07:06:01 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
 import java.net.SocketException;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.OverflowException;
 
 /**
@@ -244,4 +245,8 @@ public abstract class Endpoint implement
     public abstract void shutdown(ShutdownHow how)
         throws IOException;
 
+    public abstract Stream getStream()
+        throws IOException;
+
+    public abstract boolean connected();
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
 Thu Jul 14 07:06:01 2011
@@ -27,6 +27,7 @@ import org.apache.commons.runtime.io.Clo
 import org.apache.commons.runtime.io.Descriptor;
 import org.apache.commons.runtime.io.OperationInProgressException;
 import org.apache.commons.runtime.io.OperationWouldBlockException;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.Errno;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.OverflowException;
@@ -45,7 +46,7 @@ public class LocalEndpoint extends Endpo
     private EndpointAddress             ea;
     private SelectionKeyImpl            key;
     private boolean                     connected = false;
-
+    private SocketStream                stream = null;
     private static native int           connect0(long fd, byte[] sa, int 
timeout);
 
     /**
@@ -126,9 +127,11 @@ public class LocalEndpoint extends Endpo
 
 
     @Override
-    public void close()
+    public synchronized void close()
         throws IOException
     {
+
+        connected = false;
         if (sd.valid()) {
             if (key != null && key.selected) {
                 try {
@@ -179,17 +182,33 @@ public class LocalEndpoint extends Endpo
     }
 
     @Override
-    public void setTimeout(int timeout)
+    public synchronized void setTimeout(int timeout)
         throws IOException
     {
         sd.setTimeout(timeout);
     }
 
     @Override
-    public void shutdown(ShutdownHow how)
+    public synchronized void shutdown(ShutdownHow how)
         throws IOException
     {
         sd.shutdown(how);
     }
 
+    @Override
+    public final Stream getStream()
+        throws IOException
+    {
+        if (stream != null)
+            return stream;
+        stream = new SocketStream(sd);
+        return stream;
+    }
+
+    @Override
+    public synchronized boolean connected()
+    {
+        return connected;
+    }
+
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
 Thu Jul 14 07:06:01 2011
@@ -45,7 +45,7 @@ public class LocalServerEndpoint extends
     private EndpointAddress             sa;
     private boolean                     bound    = false;
     private boolean                     blocking = true;
-
+    private final Object                mutex    = new Object();
     private static native void          unlink0(byte[] addr);
 
     /**
@@ -102,21 +102,23 @@ public class LocalServerEndpoint extends
     public void close()
         throws IOException
     {
-        if (sd.valid()) {
-            if (key != null && key.selected) {
-                try {
-                    key.cancel();
-                } catch (Exception e) {
-                    // Ignore selector exceptions
+        synchronized(mutex) {
+            if (sd.valid()) {
+                if (key != null && key.selected) {
+                    try {
+                        key.cancel();
+                    } catch (Exception e) {
+                        // Ignore selector exceptions
+                    }
                 }
+                if (sa != null)
+                    unlink0(sa.sockaddr());
+                sd.close();
+            }
+            if (key != null) {
+                key.reset();
+                key = null;
             }
-            if (sa != null)
-                unlink0(sa.sockaddr());
-            sd.close();
-        }
-        if (key != null) {
-            key.reset();
-            key = null;
         }
     }
 
@@ -159,7 +161,7 @@ public class LocalServerEndpoint extends
         throws SocketException;
 
     @Override
-    public void bind(EndpointAddress endpoint, int backlog)
+    public synchronized void bind(EndpointAddress endpoint, int backlog)
         throws IOException
     {
         if (bound)
@@ -196,7 +198,7 @@ public class LocalServerEndpoint extends
     }
 
     @Override
-    public void setTimeout(int timeout)
+    public synchronized void setTimeout(int timeout)
         throws IOException
     {
         sd.setTimeout(timeout);

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
 Thu Jul 14 07:06:01 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
 import java.net.SocketException;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.Status;
 
@@ -66,4 +67,20 @@ public abstract class ServerEndpoint<E> 
         // Throw an exception.
         throw new IOException(Local.sm.get("endpoint.ENOTCONN"));
     }
+
+    @Override
+    public final Stream getStream()
+        throws IOException
+    {
+        // Stream can be used only for connection oriented endpoints.
+        // Throw an exception.
+        throw new IOException(Local.sm.get("endpoint.ENOTCONN"));
+    }
+
+    @Override
+    public boolean connected()
+    {
+        return false;
+    }
+
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
 Thu Jul 14 07:06:01 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
 import java.net.SocketException;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.Status;
 
@@ -37,6 +38,7 @@ public class SocketEndpoint extends Endp
     private SocketAddress               ea; // Remote address
     private SocketAddress               sa; // Local  address
     private SelectionKeyImpl            key;
+    private SocketStream                stream = null;
     private boolean                     connected = false;
 
     /**
@@ -74,7 +76,7 @@ public class SocketEndpoint extends Endp
         connected = true;
     }
 
-    public final void connect(SocketAddress endpoint)
+    public synchronized final void connect(SocketAddress endpoint)
         throws IOException
     {
         connect(endpoint, -1);
@@ -105,9 +107,10 @@ public class SocketEndpoint extends Endp
     }
 
     @Override
-    public void close()
+    public synchronized void close()
         throws IOException
     {
+        connected = false;
         if (sd.valid()) {
             if (key != null && key.selected) {
                 try {
@@ -158,17 +161,33 @@ public class SocketEndpoint extends Endp
     }
 
     @Override
-    public void setTimeout(int timeout)
+    public synchronized void setTimeout(int timeout)
         throws IOException
     {
         sd.setTimeout(timeout);
     }
 
     @Override
-    public void shutdown(ShutdownHow how)
+    public synchronized void shutdown(ShutdownHow how)
         throws IOException
     {
         sd.shutdown(how);
     }
 
+    @Override
+    public final Stream getStream()
+        throws IOException
+    {
+        if (stream != null)
+            return stream;
+        stream = new SocketStream(sd);
+        return stream;
+    }
+
+    @Override
+    public synchronized boolean connected()
+    {
+        return connected;
+    }
+
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
 Thu Jul 14 07:06:01 2011
@@ -48,6 +48,31 @@ class SocketStream extends Stream
     private static native long          alloc0(long fd);
     private static native void          close0(long nd);
 
+    private static native boolean       eof0(long nd);
+    private static native int           avail0(long nd);
+    // Native read methods
+    private static native int           read0(long nd)
+        throws IOException;
+    private static native int           read1(long nd, byte[] buf, int off, 
int len)
+        throws IOException;
+    private static native int           read2(long nd, long ptr, long off, int 
len)
+        throws IOException;
+    private static native int           read3(long nd, ByteBuffer buf, int 
off, int len)
+        throws IOException;
+    // Native write methods
+    private static native int           write0(long nd, int ch)
+        throws IOException;
+    private static native int           write1(long nd, byte[] buf, int off, 
int len)
+        throws IOException;
+    private static native int           write2(long nd, long ptr, long off, 
int len)
+        throws IOException;
+    private static native int           write3(long nd, ByteBuffer buf, int 
off, int len)
+        throws IOException;
+    private static native long          write4(long nd, byte[][] buf, int off, 
int len)
+        throws IOException;
+    private static native long          write5(long nd, ByteBuffer[] buf, int 
off, int len)
+        throws IOException;
+
     private SocketStream()
     {
         // No instance
@@ -71,6 +96,8 @@ class SocketStream extends Stream
     public final void close()
         throws IOException
     {
+        // XXX: Use separate closeLock object?
+        //
         if (nd != 0L) {
             try {
                 close0(nd);
@@ -136,8 +163,7 @@ class SocketStream extends Stream
     {
         if (closed())
             throw new ClosedDescriptorException();
-
-        return false;
+        return eof0(nd);
     }
     
     // === Reader methods
@@ -148,7 +174,7 @@ class SocketStream extends Stream
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return 0;
+        return avail0(nd);
     }
 
     @Override
@@ -158,17 +184,7 @@ class SocketStream extends Stream
 
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
-    }
-
-    @Override
-    public int read(byte[] buffer)
-        throws IOException
-    {
-
-        if (closed())
-            throw new ClosedDescriptorException();
-        return -1;
+        return read0(nd);
     }
 
     @Override
@@ -178,108 +194,81 @@ class SocketStream extends Stream
 
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
-    }
-
-    @Override
-    public long read(Pointer pointer)
-        throws NullPointerException, IOException
-    {
-        if (closed())
-            throw new ClosedDescriptorException();
-        return 0L;
+        return read1(nd, buffer, offset, count);
     }
 
     @Override
-    public long read(Pointer pointer, long offset, long count)
+    public int read(Pointer pointer, long offset, int count)
         throws NullPointerException, IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return 0L;
+        return read2(nd, pointer.address(), offset, count);
     }
 
     @Override
-    public int read(ByteBuffer buffer)
-        throws IOException
-    {
-        if (closed())
-            throw new ClosedDescriptorException();
-        return -1;
-    }
-
     public int read(ByteBuffer buffer, int offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
+        return read3(nd, buffer, offset, count);
     }
 
     // === Writer methods
 
+    @Override
     public int write(int b)
         throws IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
+        return write0(nd, b);
     }
 
+    @Override
     public int write(byte[] buffer, int offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
+        return write1(nd, buffer, offset, count);
     }
 
-    public long write(Pointer pointer)
-        throws NullPointerException, IOException
-    {
-        if (closed())
-            throw new ClosedDescriptorException();
-        return -1;
-    }
-
-    public long write(Pointer pointer, int offset, int count)
+    @Override
+    public int write(Pointer pointer, long offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
+        return write2(nd, pointer.address(), offset, count);
     }
     
-    public int write(ByteBuffer buffer)
-        throws IOException
-    {
-        if (closed())
-            throw new ClosedDescriptorException();
-        return -1;
-    }
-
+    @Override
     public int write(ByteBuffer buffer, int offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return -1;
+        return write3(nd, buffer, offset, count);
     }
 
+    @Override
     public long write(byte[][] array, int offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return 0L;
+        return write4(nd, array, offset, count);
     }
 
+    @Override
     public long write(ByteBuffer[] array, int offset, int count)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return 0L;
+        return write5(nd, array, offset, count);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/configure
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/configure (original)
+++ commons/sandbox/runtime/trunk/src/main/native/configure Thu Jul 14 07:06:01 
2011
@@ -1401,6 +1401,7 @@ extern "C" {
 #define HAVE_SYS_INOTIFY_H      `have_include sys/inotify`
 #define HAVE_SYS_TIME_H         `have_include sys/time`
 #define HAVE_SYS_UN_H           `have_include sys/un`
+#define HAVE_SYS_IOCTL_H        `have_include sys/ioctl`
 #define HAVE_LIMITS_H           `have_include limits`
 #define HAVE_NETDB_H            `have_include netdb`
 #define HAVE_NETINET_IN_H       `have_include netinet/in`

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c Thu Jul 14 
07:06:01 2011
@@ -141,6 +141,8 @@ ACR_NET_EXPORT(jint, SocketDescriptor, s
         how = SHUT_RDWR;
     if (shutdown(fd->u.s, how) == -1)
         rc = ACR_GET_NETOS_ERROR();
+    if (how != 1)
+        fd->flags |= ACR_DT_HITEOF;
     return rc;
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c Thu Jul 
14 07:06:01 2011
@@ -67,6 +67,8 @@ ACR_NET_EXPORT(jint, LocalDescriptor, sh
         how = SHUT_RDWR;
     if (shutdown(fd->u.s, how) == -1)
         rc = ACR_GET_NETOS_ERROR();
+    if (how != 1)
+        fd->flags |= ACR_DT_HITEOF;
     return rc;
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c Thu Jul 
14 07:06:01 2011
@@ -26,6 +26,9 @@
 #include "acr/time.h"
 #include "arch_opts.h"
 #include "arch_sync.h"
+#if HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
 #include <poll.h>
 
 #define SOCKADDR_CAST(BA) \
@@ -85,6 +88,30 @@ ACR_NET_EXPORT(jint, SocketStream, close
     return rc;
 }
 
+ACR_NET_EXPORT(jboolean, SocketStream, eof0)(JNI_STDARGS, jlong sp)
+{
+    acr_ss_t *ss = J2P(sp, acr_ss_t *);
+    if ((ss->fd->flags & ACR_DT_HITEOF) != 0)
+        return JNI_TRUE;
+    else
+        return JNI_FALSE;
+}
+
+ACR_NET_EXPORT(jint, SocketStream, avail0)(JNI_STDARGS, jlong sp)
+{
+    int avail;
+    acr_ss_t *ss = J2P(sp, acr_ss_t *);
+
+#if defined(FIONREAD)
+    if (ioctl(ss->fd->u.s, FIONREAD, &avail) != 0)
+        avail = -1;
+#else
+    char d[8];
+    avail = (int)recv(ss->fd->u.s, d, sizeof(d), MSG_PEEK);
+#endif
+    return avail;
+}
+
 ACR_NET_EXPORT(jint, SocketStream, read0)(JNI_STDARGS, jlong sp)
 {
     int sd;
@@ -199,16 +226,15 @@ finally:
     return (jint)rd;
 }
 
-ACR_NET_EXPORT(jlong, SocketStream, read2)(JNI_STDARGS, jlong sp,
-                                           jlong pa,
-                                           jlong off,
-                                           jlong len)
+ACR_NET_EXPORT(jint, SocketStream, read2)(JNI_STDARGS, jlong sp,
+                                          jlong pa,
+                                          jlong off,
+                                          jint len)
 {
     int sd;
     int rc = 0;
     ssize_t   rd = 0;
     ptrdiff_t po = (ptrdiff_t)off;
-    size_t    cs = (size_t)len;
     char     *bb = J2P(pa, char *);
     acr_ss_t *ss = J2P(sp, acr_ss_t *);
 
@@ -224,13 +250,13 @@ ACR_NET_EXPORT(jlong, SocketStream, read
         rc = ACR_EINVAL;
         goto finally;
     }
-    rd = r_read(sd, bb + po, cs);
+    rd = r_read(sd, bb + po, len);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         ss->fd->timeout > 0) {
         rc = AcrWaitIO(sd, ss->fd->timeout, POLLIN);
         if (rc != 0)
             goto finally;
-        rd = r_read(sd, bb + po, cs);
+        rd = r_read(sd, bb + po, len);
     }
     if (rd == -1)
         rc = ACR_GET_OS_ERROR();
@@ -240,16 +266,15 @@ ACR_NET_EXPORT(jlong, SocketStream, read
 finally:
     _release_sd(ss);
     if (rc != 0) {
+        rd = 0;
         /* Throw exception */
         ACR_THROW_NET_ERROR(rc);
-        return ACR_I64_C(-1);
     }
-    else
-        return (jlong)rd;
+    return (jint)rd;
 }
 
 ACR_NET_EXPORT(jint, SocketStream, read3)(JNI_STDARGS, jlong sp,
-                                          jobject dbb,
+                                          jobject buf,
                                           jint off,
                                           jint len)
 {
@@ -267,7 +292,7 @@ ACR_NET_EXPORT(jint, SocketStream, read3
         rc = ACR_EBADF;
         goto finally;
     }
-    bb = (char *)(*env)->GetDirectBufferAddress(env, dbb);
+    bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
     if (bb == 0) {
         rc = ACR_EINVAL;
         goto finally;
@@ -369,15 +394,14 @@ finally:
     return (jint)wr;
 }
 
-ACR_NET_EXPORT(jlong, SocketStream, write2)(JNI_STDARGS, jlong sp,
-                                            jlong pa,
-                                            jlong off,
-                                            jlong len)
+ACR_NET_EXPORT(jint, SocketStream, write2)(JNI_STDARGS, jlong sp,
+                                           jlong pa,
+                                           jlong off,
+                                           jint len)
 {
     int sd;
     int rc = 0;
     ptrdiff_t po = (ptrdiff_t)off;
-    size_t  cs   = (size_t)len;
     ssize_t wr   = 0;
     char     *bb = J2P(pa, char *);
     acr_ss_t *ss = J2P(sp, acr_ss_t *);
@@ -390,13 +414,13 @@ ACR_NET_EXPORT(jlong, SocketStream, writ
         rc = ACR_EINVAL;
         goto finally;
     }
-    wr = r_write(sd, bb + po, cs);
+    wr = r_write(sd, bb + po, len);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         ss->fd->timeout > 0) {
         rc = AcrWaitIO(sd, ss->fd->timeout, POLLOUT);
         if (rc != 0)
             goto finally;
-        wr = r_write(sd, bb + po, cs);
+        wr = r_write(sd, bb + po, len);
     }
     if (wr == -1)
         rc = ACR_GET_OS_ERROR();
@@ -450,7 +474,6 @@ finally:
 
 #define ACR_IOVEC_ON_STACK 32
 ACR_NET_EXPORT(jlong, SocketStream, write4)(JNI_STDARGS, jlong sp,
-                                            jint file,
                                             jobjectArray vec,
                                             jint off,
                                             jint len)
@@ -538,7 +561,6 @@ finally:
 }
 
 ACR_NET_EXPORT(jlong, SocketStream, write5)(JNI_STDARGS, jlong sp,
-                                            jint file,
                                             jobjectArray vec,
                                             jint off,
                                             jint len)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw Thu Jul 14 
07:06:01 2011
@@ -29,6 +29,7 @@
 #define HAVE_SYS_INOTIFY_H      0
 #define HAVE_SYS_TIME_H         0
 #define HAVE_SYS_UN_H           0
+#define HAVE_SYS_IOCTL_H        0
 #define HAVE_LIMITS_H           1
 #define HAVE_NETDB_H            0
 #define HAVE_NETINET_IN_H       0

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=1146580&r1=1146579&r2=1146580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Thu Jul 
14 07:06:01 2011
@@ -57,10 +57,32 @@ ACR_IO_EXPORT(void, Descriptor, retain0)
     AcrAtomic32Inc(&fd->refs);
 }
 
-ACR_IO_EXPORT(void, Descriptor, release0)(JNI_STDARGS, jlong fp)
+ACR_IO_EXPORT(jboolean, Descriptor, release0)(JNI_STDARGS, jlong fp)
 {
+    int refs;
     acr_fd_t *fd = J2P(fp, acr_fd_t *);
-    AcrAtomic32Dec(&fd->refs);
+
+    refs = AcrAtomic32Dec(&fd->refs);
+    if (refs == 0) {
+        /* Free the descriptor if refcount is zero.
+         * We could get here if Descriptor.close() was called
+         * before the release. In that case check if the
+         * descriptor was closed and if not throw an exception.
+         */
+        if (fd->u.f == -1 || fd->u.nh == 0) {
+            /* Descriptor was closed.
+             */
+            AcrFree(fd);
+        }
+        else {
+            AcrAtomic32Set(&fd->refs, 1);
+            ACR_THROW_MSG(ACR_EX_EILLEGAL, "Illegal reference count");
+        }
+    }
+    if (refs == 1)
+        return JNI_TRUE;
+    else
+        return JNI_FALSE;
 }
 
 ACR_IO_EXPORT(jboolean, Descriptor, unique0)(JNI_STDARGS, jlong fp)


Reply via email to