8066678: java.nio.channels.Channels cleanup
authorprappo
Fri, 05 Dec 2014 15:35:15 +0000
changeset 27933 2e48c9e946a7
parent 27932 654c0f23c1c0
child 27934 2b4d774b50dc
8066678: java.nio.channels.Channels cleanup Reviewed-by: alanb, chegar
jdk/src/java.base/share/classes/java/nio/channels/Channels.java
--- a/jdk/src/java.base/share/classes/java/nio/channels/Channels.java	Fri Dec 05 12:20:51 2014 +0100
+++ b/jdk/src/java.base/share/classes/java/nio/channels/Channels.java	Fri Dec 05 15:35:15 2014 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.UnsupportedCharsetException;
 import java.nio.channels.spi.AbstractInterruptibleChannel;
+import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import sun.nio.ch.ChannelInputStream;
 import sun.nio.cs.StreamDecoder;
@@ -48,8 +49,8 @@
  * Utility methods for channels and streams.
  *
  * <p> This class defines static methods that support the interoperation of the
- * stream classes of the <tt>{@link java.io}</tt> package with the channel
- * classes of this package.  </p>
+ * stream classes of the {@link java.io} package with the channel classes
+ * of this package.  </p>
  *
  *
  * @author Mark Reinhold
@@ -60,12 +61,7 @@
 
 public final class Channels {
 
-    private Channels() { }              // No instantiation
-
-    private static void checkNotNull(Object o, String name) {
-        if (o == null)
-            throw new NullPointerException("\"" + name + "\" is null!");
-    }
+    private Channels() { throw new Error("no instances"); }
 
     /**
      * Write all remaining bytes in buffer to the given channel.
@@ -91,7 +87,7 @@
         throws IOException
     {
         if (ch instanceof SelectableChannel) {
-            SelectableChannel sc = (SelectableChannel)ch;
+            SelectableChannel sc = (SelectableChannel) ch;
             synchronized (sc.blockingLock()) {
                 if (!sc.isBlocking())
                     throw new IllegalBlockingModeException();
@@ -107,7 +103,7 @@
     /**
      * Constructs a stream that reads bytes from the given channel.
      *
-     * <p> The <tt>read</tt> methods of the resulting stream will throw an
+     * <p> The {@code read} methods of the resulting stream will throw an
      * {@link IllegalBlockingModeException} if invoked while the underlying
      * channel is in non-blocking mode.  The stream will not be buffered, and
      * it will not support the {@link InputStream#mark mark} or {@link
@@ -121,14 +117,14 @@
      * @return  A new input stream
      */
     public static InputStream newInputStream(ReadableByteChannel ch) {
-        checkNotNull(ch, "ch");
-        return new sun.nio.ch.ChannelInputStream(ch);
+        Objects.requireNonNull(ch, "ch");
+        return new ChannelInputStream(ch);
     }
 
     /**
      * Constructs a stream that writes bytes to the given channel.
      *
-     * <p> The <tt>write</tt> methods of the resulting stream will throw an
+     * <p> The {@code write} methods of the resulting stream will throw an
      * {@link IllegalBlockingModeException} if invoked while the underlying
      * channel is in non-blocking mode.  The stream will not be buffered.  The
      * stream will be safe for access by multiple concurrent threads.  Closing
@@ -139,46 +135,49 @@
      *
      * @return  A new output stream
      */
-    public static OutputStream newOutputStream(final WritableByteChannel ch) {
-        checkNotNull(ch, "ch");
+    public static OutputStream newOutputStream(WritableByteChannel ch) {
+        Objects.requireNonNull(ch, "ch");
 
         return new OutputStream() {
 
-                private ByteBuffer bb = null;
-                private byte[] bs = null;       // Invoker's previous array
-                private byte[] b1 = null;
+            private ByteBuffer bb;
+            private byte[] bs;       // Invoker's previous array
+            private byte[] b1;
 
-                public synchronized void write(int b) throws IOException {
-                   if (b1 == null)
-                        b1 = new byte[1];
-                    b1[0] = (byte)b;
-                    this.write(b1);
-                }
+            @Override
+            public synchronized void write(int b) throws IOException {
+                if (b1 == null)
+                    b1 = new byte[1];
+                b1[0] = (byte) b;
+                this.write(b1);
+            }
 
-                public synchronized void write(byte[] bs, int off, int len)
+            @Override
+            public synchronized void write(byte[] bs, int off, int len)
                     throws IOException
-                {
-                    if ((off < 0) || (off > bs.length) || (len < 0) ||
-                        ((off + len) > bs.length) || ((off + len) < 0)) {
-                        throw new IndexOutOfBoundsException();
-                    } else if (len == 0) {
-                        return;
-                    }
-                    ByteBuffer bb = ((this.bs == bs)
-                                     ? this.bb
-                                     : ByteBuffer.wrap(bs));
-                    bb.limit(Math.min(off + len, bb.capacity()));
-                    bb.position(off);
-                    this.bb = bb;
-                    this.bs = bs;
-                    Channels.writeFully(ch, bb);
+            {
+                if ((off < 0) || (off > bs.length) || (len < 0) ||
+                    ((off + len) > bs.length) || ((off + len) < 0)) {
+                    throw new IndexOutOfBoundsException();
+                } else if (len == 0) {
+                    return;
                 }
+                ByteBuffer bb = ((this.bs == bs)
+                                 ? this.bb
+                                 : ByteBuffer.wrap(bs));
+                bb.limit(Math.min(off + len, bb.capacity()));
+                bb.position(off);
+                this.bb = bb;
+                this.bs = bs;
+                Channels.writeFully(ch, bb);
+            }
 
-                public void close() throws IOException {
-                    ch.close();
-                }
+            @Override
+            public void close() throws IOException {
+                ch.close();
+            }
 
-            };
+        };
     }
 
     /**
@@ -196,13 +195,13 @@
      *
      * @since 1.7
      */
-    public static InputStream newInputStream(final AsynchronousByteChannel ch) {
-        checkNotNull(ch, "ch");
+    public static InputStream newInputStream(AsynchronousByteChannel ch) {
+        Objects.requireNonNull(ch, "ch");
         return new InputStream() {
 
-            private ByteBuffer bb = null;
-            private byte[] bs = null;           // Invoker's previous array
-            private byte[] b1 = null;
+            private ByteBuffer bb;
+            private byte[] bs;           // Invoker's previous array
+            private byte[] b1;
 
             @Override
             public synchronized int read() throws IOException {
@@ -216,13 +215,14 @@
 
             @Override
             public synchronized int read(byte[] bs, int off, int len)
-                throws IOException
+                    throws IOException
             {
                 if ((off < 0) || (off > bs.length) || (len < 0) ||
                     ((off + len) > bs.length) || ((off + len) < 0)) {
                     throw new IndexOutOfBoundsException();
-                } else if (len == 0)
+                } else if (len == 0) {
                     return 0;
+                }
 
                 ByteBuffer bb = ((this.bs == bs)
                                  ? this.bb
@@ -270,25 +270,25 @@
      *
      * @since 1.7
      */
-    public static OutputStream newOutputStream(final AsynchronousByteChannel ch) {
-        checkNotNull(ch, "ch");
+    public static OutputStream newOutputStream(AsynchronousByteChannel ch) {
+        Objects.requireNonNull(ch, "ch");
         return new OutputStream() {
 
-            private ByteBuffer bb = null;
-            private byte[] bs = null;   // Invoker's previous array
-            private byte[] b1 = null;
+            private ByteBuffer bb;
+            private byte[] bs;   // Invoker's previous array
+            private byte[] b1;
 
             @Override
             public synchronized void write(int b) throws IOException {
-               if (b1 == null)
+                if (b1 == null)
                     b1 = new byte[1];
-                b1[0] = (byte)b;
+                b1[0] = (byte) b;
                 this.write(b1);
             }
 
             @Override
             public synchronized void write(byte[] bs, int off, int len)
-                throws IOException
+                    throws IOException
             {
                 if ((off < 0) || (off > bs.length) || (len < 0) ||
                     ((off + len) > bs.length) || ((off + len) < 0)) {
@@ -343,12 +343,11 @@
      *
      * @return  A new readable byte channel
      */
-    public static ReadableByteChannel newChannel(final InputStream in) {
-        checkNotNull(in, "in");
+    public static ReadableByteChannel newChannel(InputStream in) {
+        Objects.requireNonNull(in, "in");
 
-        if (in instanceof FileInputStream &&
-            FileInputStream.class.equals(in.getClass())) {
-            return ((FileInputStream)in).getChannel();
+        if (in.getClass() == FileInputStream.class) {
+            return ((FileInputStream) in).getChannel();
         }
 
         return new ReadableByteChannelImpl(in);
@@ -358,16 +357,16 @@
         extends AbstractInterruptibleChannel    // Not really interruptible
         implements ReadableByteChannel
     {
-        InputStream in;
+        private final InputStream in;
         private static final int TRANSFER_SIZE = 8192;
-        private byte buf[] = new byte[0];
-        private boolean open = true;
-        private Object readLock = new Object();
+        private byte[] buf = new byte[0];
+        private final Object readLock = new Object();
 
         ReadableByteChannelImpl(InputStream in) {
             this.in = in;
         }
 
+        @Override
         public int read(ByteBuffer dst) throws IOException {
             int len = dst.remaining();
             int totalRead = 0;
@@ -399,9 +398,9 @@
             }
         }
 
+        @Override
         protected void implCloseChannel() throws IOException {
             in.close();
-            open = false;
         }
     }
 
@@ -418,12 +417,11 @@
      *
      * @return  A new writable byte channel
      */
-    public static WritableByteChannel newChannel(final OutputStream out) {
-        checkNotNull(out, "out");
+    public static WritableByteChannel newChannel(OutputStream out) {
+        Objects.requireNonNull(out, "out");
 
-        if (out instanceof FileOutputStream &&
-            FileOutputStream.class.equals(out.getClass())) {
-                return ((FileOutputStream)out).getChannel();
+        if (out.getClass() == FileOutputStream.class) {
+            return ((FileOutputStream) out).getChannel();
         }
 
         return new WritableByteChannelImpl(out);
@@ -433,16 +431,16 @@
         extends AbstractInterruptibleChannel    // Not really interruptible
         implements WritableByteChannel
     {
-        OutputStream out;
+        private final OutputStream out;
         private static final int TRANSFER_SIZE = 8192;
-        private byte buf[] = new byte[0];
-        private boolean open = true;
-        private Object writeLock = new Object();
+        private byte[] buf = new byte[0];
+        private final Object writeLock = new Object();
 
         WritableByteChannelImpl(OutputStream out) {
             this.out = out;
         }
 
+        @Override
         public int write(ByteBuffer src) throws IOException {
             int len = src.remaining();
             int totalWritten = 0;
@@ -465,9 +463,9 @@
             }
         }
 
+        @Override
         protected void implCloseChannel() throws IOException {
             out.close();
-            open = false;
         }
     }
 
@@ -479,7 +477,7 @@
      * given decoder.
      *
      * <p> The resulting stream will contain an internal input buffer of at
-     * least <tt>minBufferCap</tt> bytes.  The stream's <tt>read</tt> methods
+     * least {@code minBufferCap} bytes.  The stream's {@code read} methods
      * will, as needed, fill the buffer by reading bytes from the underlying
      * channel; if the channel is in non-blocking mode when bytes are to be
      * read then an {@link IllegalBlockingModeException} will be thrown.  The
@@ -495,7 +493,7 @@
      *
      * @param  minBufferCap
      *         The minimum capacity of the internal byte buffer,
-     *         or <tt>-1</tt> if an implementation-dependent
+     *         or {@code -1} if an implementation-dependent
      *         default capacity is to be used
      *
      * @return  A new reader
@@ -504,7 +502,7 @@
                                    CharsetDecoder dec,
                                    int minBufferCap)
     {
-        checkNotNull(ch, "ch");
+        Objects.requireNonNull(ch, "ch");
         return StreamDecoder.forDecoder(ch, dec.reset(), minBufferCap);
     }
 
@@ -514,16 +512,15 @@
      *
      * <p> An invocation of this method of the form
      *
-     * <blockquote><pre>
-     * Channels.newReader(ch, csname)</pre></blockquote>
+     * <pre> {@code
+     *     Channels.newReader(ch, csname)
+     * } </pre>
      *
      * behaves in exactly the same way as the expression
      *
-     * <blockquote><pre>
-     * Channels.newReader(ch,
-     *                    Charset.forName(csName)
-     *                        .newDecoder(),
-     *                    -1);</pre></blockquote>
+     * <pre> {@code
+     *     Channels.newReader(ch, Charset.forName(csName).newDecoder(), -1)
+     * } </pre>
      *
      * @param  ch
      *         The channel from which bytes will be read
@@ -540,7 +537,7 @@
     public static Reader newReader(ReadableByteChannel ch,
                                    String csName)
     {
-        checkNotNull(csName, "csName");
+        Objects.requireNonNull(csName, "csName");
         return newReader(ch, Charset.forName(csName).newDecoder(), -1);
     }
 
@@ -549,7 +546,7 @@
      * writes the resulting bytes to the given channel.
      *
      * <p> The resulting stream will contain an internal output buffer of at
-     * least <tt>minBufferCap</tt> bytes.  The stream's <tt>write</tt> methods
+     * least {@code minBufferCap} bytes.  The stream's {@code write} methods
      * will, as needed, flush the buffer by writing bytes to the underlying
      * channel; if the channel is in non-blocking mode when bytes are to be
      * written then an {@link IllegalBlockingModeException} will be thrown.
@@ -564,16 +561,16 @@
      *
      * @param  minBufferCap
      *         The minimum capacity of the internal byte buffer,
-     *         or <tt>-1</tt> if an implementation-dependent
+     *         or {@code -1} if an implementation-dependent
      *         default capacity is to be used
      *
      * @return  A new writer
      */
-    public static Writer newWriter(final WritableByteChannel ch,
-                                   final CharsetEncoder enc,
-                                   final int minBufferCap)
+    public static Writer newWriter(WritableByteChannel ch,
+                                   CharsetEncoder enc,
+                                   int minBufferCap)
     {
-        checkNotNull(ch, "ch");
+        Objects.requireNonNull(ch, "ch");
         return StreamEncoder.forEncoder(ch, enc.reset(), minBufferCap);
     }
 
@@ -582,17 +579,15 @@
      * charset and writes the resulting bytes to the given channel.
      *
      * <p> An invocation of this method of the form
-     *
-     * <blockquote><pre>
-     * Channels.newWriter(ch, csname)</pre></blockquote>
-     *
+     * <p>
+     * <pre> {@code
+     *     Channels.newWriter(ch, csname)
+     * } </pre>
      * behaves in exactly the same way as the expression
-     *
-     * <blockquote><pre>
-     * Channels.newWriter(ch,
-     *                    Charset.forName(csName)
-     *                        .newEncoder(),
-     *                    -1);</pre></blockquote>
+     * <p>
+     * <pre> {@code
+     *     Channels.newWriter(ch, Charset.forName(csName).newEncoder(), -1)
+     * } </pre>
      *
      * @param  ch
      *         The channel to which bytes will be written
@@ -609,7 +604,7 @@
     public static Writer newWriter(WritableByteChannel ch,
                                    String csName)
     {
-        checkNotNull(csName, "csName");
+        Objects.requireNonNull(csName, "csName");
         return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
     }
 }