harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r422702 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
Date Mon, 17 Jul 2006 12:43:59 GMT
Author: gharley
Date: Mon Jul 17 05:43:57 2006
New Revision: 422702

URL: http://svn.apache.org/viewvc?rev=422702&view=rev
Log:
HARMONY 889 : [nio] SocketChannel.socket().getInputStream/getOutputStream should delegate
all of its operations to the associated channel

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java?rev=422702&r1=422701&r2=422702&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
Mon Jul 17 05:43:57 2006
@@ -390,7 +390,7 @@
     public long read(ByteBuffer[] targets, int offset, int length)
             throws IOException {
         if (!isIndexValid(targets, offset, length)) {
-            throw new ArrayIndexOutOfBoundsException();
+            throw new IndexOutOfBoundsException();
         }
         checkOpenConnected();
 
@@ -468,20 +468,20 @@
      */
     public long write(ByteBuffer[] sources, int offset, int length)
             throws IOException {
-        if (isIndexValid(sources, offset, length)) {
-            checkOpenConnected();
-            if (0 == calculateByteBufferArray(sources, offset, length)){
-                return 0;
-            }
-            synchronized (writeLock) {
-                long writeCount = 0;
-                for (int val = offset; val < offset + length; val++) {
-                    writeCount = writeCount + writeImpl(sources[val]);
-                }
-                return writeCount;
+        if (!isIndexValid(sources, offset, length)) {
+            throw new IndexOutOfBoundsException();
+        }
+        checkOpenConnected();
+        if (0 == calculateByteBufferArray(sources, offset, length)) {
+            return 0;
+        }
+        synchronized (writeLock) {
+            long writeCount = 0;
+            for (int val = offset; val < offset + length; val++) {
+                writeCount = writeCount + writeImpl(sources[val]);
             }
+            return writeCount;
         }
-        throw new ArrayIndexOutOfBoundsException();
     }
 
     private int calculateByteBufferArray(ByteBuffer[] sources, int offset, int length){
@@ -614,10 +614,6 @@
         }
     }
 
-    // -------------------------------------------------------------------
-    // Adapter classes for internal socket.
-    // -------------------------------------------------------------------
-
     /*
      * get the fd
      */
@@ -625,20 +621,10 @@
         return fd;
     }
 
-    public InetAddress getAddress() {
-        return (null == this.connectAddress) ? null : this.connectAddress
-                .getAddress();
-    }
-
-    public int getPort() {
-        return (null == this.connectAddress) ? 0 : this.connectAddress
-                .getPort();
-    }
-
-    public int getLocalPort() {
-        return this.localPort;
-    }
-
+    // -------------------------------------------------------------------
+    // Adapter classes for internal socket.
+    // -------------------------------------------------------------------
+    
     private static class SocketAdapter extends Socket {
 
         // ----------------------------------------------------
@@ -784,7 +770,6 @@
         }
 
         /*
-         * 
          * @see java.net.Socket#getTcpNoDelay()
          */
         public boolean getTcpNoDelay() throws SocketException {
@@ -793,13 +778,20 @@
                     .booleanValue();
         }
 
-        /*
-         * 
+        /* 
          * @see java.net.Socket#getOutputStream()
          */
         public OutputStream getOutputStream() throws IOException {
-            return new SocketChannelOutputStream(super.getOutputStream(),
-                    channel);
+            if (!channel.isOpen()) {
+                throw new SocketException(Msg.getString("K003d"));
+            }
+            if (!channel.isConnected()) {
+                throw new SocketException(Msg.getString("K0320"));
+            }             
+            if (isOutputShutdown()) {
+                throw new SocketException(Msg.getString("KA00f"));
+            }
+            return new SocketChannelOutputStream(channel);
         }
 
         /*
@@ -807,7 +799,16 @@
          * @see java.net.Socket#getInputStream()
          */
         public InputStream getInputStream() throws IOException {
-            return new SocketChannelInputStream(super.getInputStream(), channel);
+            if (!channel.isOpen()) {
+                throw new SocketException(Msg.getString("K003d"));
+            }
+            if (!channel.isConnected()) {
+                throw new SocketException(Msg.getString("K0320"));
+            }             
+            if (isInputShutdown()) {
+                throw new SocketException(Msg.getString("K0321"));
+            }
+            return new SocketChannelInputStream(channel);
         }
 
         /*
@@ -827,95 +828,101 @@
         }
     }
 
+    
+    /*
+     * This output stream delegates all operations to the associated channel.
+     * Throws an IllegalBlockingModeException if the channel is in non-blocking
+     * mode when performing write operations.
+     */
     private static class SocketChannelOutputStream extends OutputStream {
         SocketChannel channel;
 
-        OutputStream wrapped;
-
-        public SocketChannelOutputStream(OutputStream wrapped,
-                SocketChannel channel) {
+        public SocketChannelOutputStream(SocketChannel channel) {
             this.channel = channel;
-            this.wrapped = wrapped;
         }
 
         /*
+         * Closes this stream and channel
          * 
-         * @see com.ibm.io.nio.SocketOutputStream#write(byte[], int, int)
+         * @exception IOException
+         *                thrown if an error occurs during the close
+         */
+        public void close() throws IOException {
+            channel.close();
+        }
+        
+        /* 
+         * @see java.io.OutputStream#write(byte[], int, int)
          */
         public void write(byte[] buffer, int offset, int count)
                 throws IOException {
-            if (!channel.isBlocking()) {
-                throw new IllegalBlockingModeException();
+            if (0 > offset || 0 > count || count + offset > buffer.length) {
+                throw new IndexOutOfBoundsException();
             }
-            wrapped.write(buffer, offset, count);
-        }
-
-        /*
-         * 
-         * @see com.ibm.io.nio.SocketOutputStream#write(byte[])
-         */
-        public void write(byte[] buffer) throws IOException {
+            ByteBuffer buf = ByteBuffer.wrap(buffer, offset, count);
             if (!channel.isBlocking()) {
                 throw new IllegalBlockingModeException();
-            }
-            wrapped.write(buffer);
+            }            
+            channel.write(buf);
         }
 
-        /*
-         * 
-         * @see com.ibm.io.nio.SocketOutputStream#write(int)
+        /* 
+         * @see java.io.OutputStream#write(int)
          */
         public void write(int oneByte) throws IOException {
             if (!channel.isBlocking()) {
                 throw new IllegalBlockingModeException();
             }
-            wrapped.write(oneByte);
+            ByteBuffer buffer = ByteBuffer.allocate(1);
+            buffer.put((byte)(oneByte & 0xFF));
+            channel.write(buffer);
         }
     }
 
+    /*
+     * This input stream delegates all operations to the associated channel.
+     * Throws an IllegalBlockingModeException if the channel is in non-blocking
+     * mode when performing read operations.
+     */
     private static class SocketChannelInputStream extends InputStream {
         SocketChannel channel;
 
-        InputStream wrapped;
-
-        public SocketChannelInputStream(InputStream wrapped,
-                SocketChannel channel) {
+        public SocketChannelInputStream(SocketChannel channel) {
             this.channel = channel;
-            this.wrapped = wrapped;
         }
-
+        
         /*
-         * 
-         * @see com.ibm.io.nio.SocketInputStream#read()
+         * Closes this stream and channel.
+         */
+        public void close() throws IOException {
+            channel.close();
+        }
+
+        /* 
+         * @see java.io.InputStream#read()
          */
         public int read() throws IOException {
             if (!channel.isBlocking()) {
                 throw new IllegalBlockingModeException();
             }
-            return wrapped.read();
+            ByteBuffer buf = ByteBuffer.allocate(1);
+            int result = channel.read(buf);
+            return (-1 == result) ? result : buf.get() & 0xFF;
         }
 
-        /*
-         * 
-         * @see com.ibm.io.nio.SocketInputStream#read(byte[], int, int)
+        /* 
+         * @see java.io.InputStream#read(byte[], int, int)
          */
         public int read(byte[] buffer, int offset, int count)
                 throws IOException {
-            if (!channel.isBlocking()) {
-                throw new IllegalBlockingModeException();
+            if (0 > offset || 0 > count || count + offset > buffer.length) {
+                throw new IndexOutOfBoundsException();
             }
-            return wrapped.read(buffer, offset, count);
-        }
-
-        /*
-         * 
-         * @see com.ibm.io.nio.SocketInputStream#read(byte[])
-         */
-        public int read(byte[] buffer) throws IOException {
             if (!channel.isBlocking()) {
                 throw new IllegalBlockingModeException();
             }
-            return wrapped.read(buffer);
+            ByteBuffer buf = ByteBuffer.wrap(buffer, offset, count);
+            return channel.read(buf);
         }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java?rev=422702&r1=422701&r2=422702&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
Mon Jul 17 05:43:57 2006
@@ -2610,7 +2610,233 @@
             }
         }
     }
-    
+
+    /**
+     * @tests SocketChannelImpl#socket().getOutputStream().read
+     */
+    public void test_socket_getOutputStream_nonBlocking_read_Exception()
+            throws IOException {
+        channel1.connect(this.localAddr1);
+        InputStream is = channel1.socket().getInputStream();
+        channel1.configureBlocking(false);
+        try {
+            is.read();
+            fail("should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        try {
+            is.read(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        // closed
+        is.close();
+        try {
+            is.read(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read();
+            fail("should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        try {
+            is.read(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests SocketChannelImpl#socket().getOutputStream().read
+     */
+    public void test_socket_getOutputStream_blocking_read_Exception()
+            throws IOException {
+        channel1.connect(this.localAddr1);
+        InputStream is = channel1.socket().getInputStream();
+        try {
+            is.read(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        // closed
+        is.close();
+        try {
+            is.read(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read();
+            fail("should throw ClosedChannelException");
+        } catch (ClosedChannelException e) {
+            // expected
+        }
+        try {
+            is.read(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            is.read(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests SocketChannelImpl#socket().getOutputStream().write
+     */
+    public void test_socket_getOutputStream_nonBlocking_write_Exception()
+            throws IOException {
+        channel1.connect(this.localAddr1);
+        OutputStream os = channel1.socket().getOutputStream();
+        channel1.configureBlocking(false);
+        try {
+            os.write(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(1);
+            fail("should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        try {
+            os.write(1);
+            fail("should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        try {
+            os.write(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        // closed
+        os.close();
+        try {
+            os.write(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(1);
+            fail("should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        try {
+            os.write(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests SocketChannelImpl#socket().getOutputStream().write
+     */
+    public void test_socket_getOutputStream_blocking_write_Exception()
+            throws IOException {
+        channel1.connect(this.localAddr1);
+        OutputStream os = channel1.socket().getOutputStream();
+        try {
+            os.write(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        // closed
+        os.close();
+        try {
+            os.write(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(1);
+            fail("should throw ClosedChannelException");
+        } catch (ClosedChannelException e) {
+            // expected
+        }
+        try {
+            os.write(null, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            os.write(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
     class MockSocketChannel extends SocketChannel{
         
         private boolean isWriteCalled = false;
@@ -2677,5 +2903,5 @@
             // empty            
         }
         
-    }
+    }   
 }



Mime
View raw message