8163517: Various cleanup in java.io code
authorigerasim
Thu, 18 Aug 2016 22:07:09 +0300
changeset 40410 5fd4a1f809f8
parent 40409 564ea8deef3c
child 40411 e936d2ba0281
8163517: Various cleanup in java.io code Reviewed-by: shade, rriggs, redestad
jdk/src/java.base/share/classes/java/io/BufferedReader.java
jdk/src/java.base/share/classes/java/io/ByteArrayOutputStream.java
jdk/src/java.base/share/classes/java/io/CharArrayWriter.java
jdk/src/java.base/share/classes/java/io/FileSystem.java
jdk/src/java.base/share/classes/java/io/ObjectInputStream.java
jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java
jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java
jdk/src/java.base/share/classes/java/io/OutputStreamWriter.java
jdk/src/java.base/share/classes/java/io/PrintStream.java
jdk/src/java.base/share/classes/java/io/PrintWriter.java
jdk/src/java.base/share/classes/java/io/SequenceInputStream.java
jdk/src/java.base/share/classes/java/io/StringBufferInputStream.java
jdk/src/java.base/share/classes/java/io/StringReader.java
jdk/src/java.base/share/classes/java/io/StringWriter.java
jdk/src/java.base/share/classes/java/io/Writer.java
--- a/jdk/src/java.base/share/classes/java/io/BufferedReader.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/BufferedReader.java	Thu Aug 18 22:07:09 2016 +0300
@@ -560,7 +560,7 @@
      * @since 1.8
      */
     public Stream<String> lines() {
-        Iterator<String> iter = new Iterator<String>() {
+        Iterator<String> iter = new Iterator<>() {
             String nextLine = null;
 
             @Override
--- a/jdk/src/java.base/share/classes/java/io/ByteArrayOutputStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/ByteArrayOutputStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -187,7 +187,7 @@
      * @return  the current contents of this output stream, as a byte array.
      * @see     java.io.ByteArrayOutputStream#size()
      */
-    public synchronized byte toByteArray()[] {
+    public synchronized byte[] toByteArray() {
         return Arrays.copyOf(buf, count);
     }
 
--- a/jdk/src/java.base/share/classes/java/io/CharArrayWriter.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/CharArrayWriter.java	Thu Aug 18 22:07:09 2016 +0300
@@ -165,7 +165,7 @@
      *
      * @param  csq
      *         The character sequence to append.  If {@code csq} is
-     *         {@code null}, then the four characters "{@code null}" are
+     *         {@code null}, then the four characters {@code "null"} are
      *         appended to this writer.
      *
      * @return  This writer
@@ -173,7 +173,7 @@
      * @since  1.5
      */
     public CharArrayWriter append(CharSequence csq) {
-        String s = (csq == null ? "null" : csq.toString());
+        String s = String.valueOf(csq);
         write(s, 0, s.length());
         return this;
     }
@@ -193,7 +193,7 @@
      *         The character sequence from which a subsequence will be
      *         appended.  If {@code csq} is {@code null}, then characters
      *         will be appended as if {@code csq} contained the four
-     *         characters "{@code null}".
+     *         characters {@code "null"}.
      *
      * @param  start
      *         The index of the first character in the subsequence
@@ -212,9 +212,8 @@
      * @since  1.5
      */
     public CharArrayWriter append(CharSequence csq, int start, int end) {
-        String s = (csq == null ? "null" : csq).subSequence(start, end).toString();
-        write(s, 0, s.length());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**
@@ -251,7 +250,7 @@
      *
      * @return an array of chars copied from the input data.
      */
-    public char toCharArray()[] {
+    public char[] toCharArray() {
         synchronized (lock) {
             return Arrays.copyOf(buf, count);
         }
--- a/jdk/src/java.base/share/classes/java/io/FileSystem.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/FileSystem.java	Thu Aug 18 22:07:09 2016 +0300
@@ -228,13 +228,8 @@
     static boolean useCanonPrefixCache = true;
 
     private static boolean getBooleanProperty(String prop, boolean defaultVal) {
-        String val = System.getProperty(prop);
-        if (val == null) return defaultVal;
-        if (val.equalsIgnoreCase("true")) {
-            return true;
-        } else {
-            return false;
-        }
+        return Boolean.parseBoolean(System.getProperty(prop,
+                String.valueOf(defaultVal)));
     }
 
     static {
--- a/jdk/src/java.base/share/classes/java/io/ObjectInputStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/ObjectInputStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -1265,22 +1265,21 @@
         WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
         Boolean result = Caches.subclassAudits.get(key);
         if (result == null) {
-            result = Boolean.valueOf(auditSubclass(cl));
+            result = auditSubclass(cl);
             Caches.subclassAudits.putIfAbsent(key, result);
         }
-        if (result.booleanValue()) {
-            return;
+        if (!result) {
+            sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
         }
-        sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
     }
 
     /**
      * Performs reflective checks on given subclass to verify that it doesn't
-     * override security-sensitive non-final methods.  Returns true if subclass
-     * is "safe", false otherwise.
+     * override security-sensitive non-final methods.  Returns TRUE if subclass
+     * is "safe", FALSE otherwise.
      */
-    private static boolean auditSubclass(final Class<?> subcl) {
-        Boolean result = AccessController.doPrivileged(
+    private static Boolean auditSubclass(Class<?> subcl) {
+        return AccessController.doPrivileged(
             new PrivilegedAction<>() {
                 public Boolean run() {
                     for (Class<?> cl = subcl;
@@ -1303,7 +1302,6 @@
                 }
             }
         );
-        return result.booleanValue();
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -1050,22 +1050,21 @@
         WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
         Boolean result = Caches.subclassAudits.get(key);
         if (result == null) {
-            result = Boolean.valueOf(auditSubclass(cl));
+            result = auditSubclass(cl);
             Caches.subclassAudits.putIfAbsent(key, result);
         }
-        if (result.booleanValue()) {
-            return;
+        if (!result) {
+            sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
         }
-        sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
     }
 
     /**
      * Performs reflective checks on given subclass to verify that it doesn't
-     * override security-sensitive non-final methods.  Returns true if subclass
-     * is "safe", false otherwise.
+     * override security-sensitive non-final methods.  Returns TRUE if subclass
+     * is "safe", FALSE otherwise.
      */
-    private static boolean auditSubclass(final Class<?> subcl) {
-        Boolean result = AccessController.doPrivileged(
+    private static Boolean auditSubclass(Class<?> subcl) {
+        return AccessController.doPrivileged(
             new PrivilegedAction<>() {
                 public Boolean run() {
                     for (Class<?> cl = subcl;
@@ -1088,7 +1087,6 @@
                 }
             }
         );
-        return result.booleanValue();
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java	Thu Aug 18 22:07:09 2016 +0300
@@ -1509,11 +1509,9 @@
     private static String getPackageName(Class<?> cl) {
         String s = cl.getName();
         int i = s.lastIndexOf('[');
-        if (i >= 0) {
-            s = s.substring(i + 2);
-        }
-        i = s.lastIndexOf('.');
-        return (i >= 0) ? s.substring(0, i) : "";
+        i = (i < 0) ? 0 : i + 2;
+        int j = s.lastIndexOf('.');
+        return (i < j) ? s.substring(i, j) : "";
     }
 
     /**
@@ -1535,14 +1533,14 @@
     private static String getMethodSignature(Class<?>[] paramTypes,
                                              Class<?> retType)
     {
-        StringBuilder sbuf = new StringBuilder();
-        sbuf.append('(');
+        StringBuilder sb = new StringBuilder();
+        sb.append('(');
         for (int i = 0; i < paramTypes.length; i++) {
-            appendClassSignature(sbuf, paramTypes[i]);
+            appendClassSignature(sb, paramTypes[i]);
         }
-        sbuf.append(')');
-        appendClassSignature(sbuf, retType);
-        return sbuf.toString();
+        sb.append(')');
+        appendClassSignature(sb, retType);
+        return sb.toString();
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/OutputStreamWriter.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/OutputStreamWriter.java	Thu Aug 18 22:07:09 2016 +0300
@@ -233,22 +233,16 @@
 
     @Override
     public Writer append(CharSequence csq, int start, int end) throws IOException {
-        if (csq == null) {
-            write("null".subSequence(start, end).toString());
-            return this;
-        } else {
-            return append(csq.subSequence(start, end));
-        }
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     @Override
     public Writer append(CharSequence csq) throws IOException {
-        if (csq == null) {
-            se.write("null");
-        } else if (csq instanceof CharBuffer) {
+        if (csq instanceof CharBuffer) {
             se.write((CharBuffer) csq);
         } else {
-            se.write(csq.toString());
+            se.write(String.valueOf(csq));
         }
         return this;
     }
--- a/jdk/src/java.base/share/classes/java/io/PrintStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/PrintStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -568,7 +568,7 @@
      * @param      b   The {@code boolean} to be printed
      */
     public void print(boolean b) {
-        write(b ? "true" : "false");
+        write(String.valueOf(b));
     }
 
     /**
@@ -663,10 +663,7 @@
      * @param      s   The {@code String} to be printed
      */
     public void print(String s) {
-        if (s == null) {
-            s = "null";
-        }
-        write(s);
+        write(String.valueOf(s));
     }
 
     /**
@@ -1068,10 +1065,7 @@
      * @since  1.5
      */
     public PrintStream append(CharSequence csq) {
-        if (csq == null)
-            print("null");
-        else
-            print(csq.toString());
+        print(String.valueOf(csq));
         return this;
     }
 
@@ -1111,9 +1105,8 @@
      * @since  1.5
      */
     public PrintStream append(CharSequence csq, int start, int end) {
-        CharSequence cs = (csq == null ? "null" : csq);
-        write(cs.subSequence(start, end).toString());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/PrintWriter.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/PrintWriter.java	Thu Aug 18 22:07:09 2016 +0300
@@ -504,7 +504,7 @@
      * @param      b   The {@code boolean} to be printed
      */
     public void print(boolean b) {
-        write(b ? "true" : "false");
+        write(String.valueOf(b));
     }
 
     /**
@@ -599,10 +599,7 @@
      * @param      s   The {@code String} to be printed
      */
     public void print(String s) {
-        if (s == null) {
-            s = "null";
-        }
-        write(s);
+        write(String.valueOf(s));
     }
 
     /**
@@ -1005,10 +1002,7 @@
      * @since  1.5
      */
     public PrintWriter append(CharSequence csq) {
-        if (csq == null)
-            write("null");
-        else
-            write(csq.toString());
+        write(String.valueOf(csq));
         return this;
     }
 
@@ -1047,9 +1041,8 @@
      * @since  1.5
      */
     public PrintWriter append(CharSequence csq, int start, int end) {
-        CharSequence cs = (csq == null ? "null" : csq);
-        write(cs.subSequence(start, end).toString());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/SequenceInputStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/SequenceInputStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -65,12 +65,7 @@
      */
     public SequenceInputStream(Enumeration<? extends InputStream> e) {
         this.e = e;
-        try {
-            nextStream();
-        } catch (IOException ex) {
-            // This should never happen
-            throw new Error("panic");
-        }
+        peekNextStream();
     }
 
     /**
@@ -86,16 +81,10 @@
      */
     public SequenceInputStream(InputStream s1, InputStream s2) {
         Vector<InputStream> v = new Vector<>(2);
-
         v.addElement(s1);
         v.addElement(s2);
         e = v.elements();
-        try {
-            nextStream();
-        } catch (IOException ex) {
-            // This should never happen
-            throw new Error("panic");
-        }
+        peekNextStream();
     }
 
     /**
@@ -105,14 +94,17 @@
         if (in != null) {
             in.close();
         }
+        peekNextStream();
+    }
 
+    private void peekNextStream() {
         if (e.hasMoreElements()) {
             in = (InputStream) e.nextElement();
             if (in == null)
                 throw new NullPointerException();
+        } else {
+            in = null;
         }
-        else in = null;
-
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/StringBufferInputStream.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/StringBufferInputStream.java	Thu Aug 18 22:07:09 2016 +0300
@@ -108,6 +108,7 @@
      *             <code>-1</code> if there is no more data because the end of
      *             the stream has been reached.
      */
+    @SuppressWarnings("deprecation")
     public synchronized int read(byte b[], int off, int len) {
         if (b == null) {
             throw new NullPointerException();
@@ -126,12 +127,8 @@
         if (len <= 0) {
             return 0;
         }
-        String  s = buffer;
-        int cnt = len;
-        while (--cnt >= 0) {
-            b[off++] = (byte)s.charAt(pos++);
-        }
-
+        buffer.getBytes(pos, pos + len, b, off);
+        pos += len;
         return len;
     }
 
--- a/jdk/src/java.base/share/classes/java/io/StringReader.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/StringReader.java	Thu Aug 18 22:07:09 2016 +0300
@@ -142,8 +142,8 @@
      */
     public boolean ready() throws IOException {
         synchronized (lock) {
-        ensureOpen();
-        return true;
+            ensureOpen();
+            return true;
         }
     }
 
--- a/jdk/src/java.base/share/classes/java/io/StringWriter.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/StringWriter.java	Thu Aug 18 22:07:09 2016 +0300
@@ -139,7 +139,7 @@
      *
      * @param  csq
      *         The character sequence to append.  If {@code csq} is
-     *         {@code null}, then the four characters "{@code null}" are
+     *         {@code null}, then the four characters {@code "null"} are
      *         appended to this writer.
      *
      * @return  This writer
@@ -147,10 +147,7 @@
      * @since  1.5
      */
     public StringWriter append(CharSequence csq) {
-        if (csq == null)
-            write("null");
-        else
-            write(csq.toString());
+        write(String.valueOf(csq));
         return this;
     }
 
@@ -170,7 +167,7 @@
      *         The character sequence from which a subsequence will be
      *         appended.  If {@code csq} is {@code null}, then characters
      *         will be appended as if {@code csq} contained the four
-     *         characters "{@code null}".
+     *         characters {@code "null"}.
      *
      * @param  start
      *         The index of the first character in the subsequence
@@ -189,9 +186,8 @@
      * @since  1.5
      */
     public StringWriter append(CharSequence csq, int start, int end) {
-        CharSequence cs = (csq == null ? "null" : csq);
-        write(cs.subSequence(start, end).toString());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/io/Writer.java	Thu Aug 18 15:05:02 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/io/Writer.java	Thu Aug 18 22:07:09 2016 +0300
@@ -221,7 +221,7 @@
      *
      * @param  csq
      *         The character sequence to append.  If {@code csq} is
-     *         {@code null}, then the four characters "{@code null}" are
+     *         {@code null}, then the four characters {@code "null"} are
      *         appended to this writer.
      *
      * @return  This writer
@@ -232,10 +232,7 @@
      * @since  1.5
      */
     public Writer append(CharSequence csq) throws IOException {
-        if (csq == null)
-            write("null");
-        else
-            write(csq.toString());
+        write(String.valueOf(csq));
         return this;
     }
 
@@ -256,7 +253,7 @@
      *         The character sequence from which a subsequence will be
      *         appended.  If {@code csq} is {@code null}, then characters
      *         will be appended as if {@code csq} contained the four
-     *         characters "{@code null}".
+     *         characters {@code "null"}.
      *
      * @param  start
      *         The index of the first character in the subsequence
@@ -278,9 +275,8 @@
      * @since  1.5
      */
     public Writer append(CharSequence csq, int start, int end) throws IOException {
-        CharSequence cs = (csq == null ? "null" : csq);
-        write(cs.subSequence(start, end).toString());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**