8163517: Various cleanup in java.io code

Reviewed-by: shade, rriggs, redestad
This commit is contained in:
Ivan Gerasimov 2016-08-18 22:07:09 +03:00
parent 7cb94a8bf8
commit 25ff06c014
15 changed files with 69 additions and 120 deletions

View File

@ -560,7 +560,7 @@ public class BufferedReader extends Reader {
* @since 1.8
*/
public Stream<String> lines() {
Iterator<String> iter = new Iterator<String>() {
Iterator<String> iter = new Iterator<>() {
String nextLine = null;
@Override

View File

@ -187,7 +187,7 @@ public class ByteArrayOutputStream extends OutputStream {
* @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);
}

View File

@ -165,7 +165,7 @@ class CharArrayWriter extends Writer {
*
* @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 @@ class CharArrayWriter extends Writer {
* @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 @@ class CharArrayWriter extends Writer {
* 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 @@ class CharArrayWriter extends Writer {
* @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 @@ class CharArrayWriter extends Writer {
*
* @return an array of chars copied from the input data.
*/
public char toCharArray()[] {
public char[] toCharArray() {
synchronized (lock) {
return Arrays.copyOf(buf, count);
}

View File

@ -228,13 +228,8 @@ abstract class FileSystem {
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 {

View File

@ -1265,22 +1265,21 @@ public class ObjectInputStream
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 @@ public class ObjectInputStream
}
}
);
return result.booleanValue();
}
/**

View File

@ -1050,22 +1050,21 @@ public class ObjectOutputStream
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 @@ public class ObjectOutputStream
}
}
);
return result.booleanValue();
}
/**

View File

@ -1509,11 +1509,9 @@ public class ObjectStreamClass implements Serializable {
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 @@ public class ObjectStreamClass implements Serializable {
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();
}
/**

View File

@ -233,22 +233,16 @@ public class OutputStreamWriter extends Writer {
@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;
}

View File

@ -568,7 +568,7 @@ public class PrintStream extends FilterOutputStream
* @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 @@ public class PrintStream extends FilterOutputStream
* @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 @@ public class PrintStream extends FilterOutputStream
* @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 @@ public class PrintStream extends FilterOutputStream
* @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));
}
/**

View File

@ -504,7 +504,7 @@ public class PrintWriter extends Writer {
* @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 @@ public class PrintWriter extends Writer {
* @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 @@ public class PrintWriter extends Writer {
* @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 @@ public class PrintWriter extends Writer {
* @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));
}
/**

View File

@ -65,12 +65,7 @@ class SequenceInputStream extends InputStream {
*/
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 @@ class SequenceInputStream extends InputStream {
*/
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 @@ class SequenceInputStream extends InputStream {
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;
}
/**

View File

@ -108,6 +108,7 @@ class StringBufferInputStream extends InputStream {
* <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 @@ class StringBufferInputStream extends InputStream {
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;
}

View File

@ -142,8 +142,8 @@ public class StringReader extends Reader {
*/
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
return true;
ensureOpen();
return true;
}
}

View File

@ -139,7 +139,7 @@ public class StringWriter extends Writer {
*
* @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 @@ public class StringWriter extends Writer {
* @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 @@ public class StringWriter extends Writer {
* 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 @@ public class StringWriter extends Writer {
* @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));
}
/**

View File

@ -221,7 +221,7 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
*
* @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 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* @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 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* 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 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* @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));
}
/**