Reuse EOF constant.
Javadoc tweaks. Format to max line length. Simplify.
MODIFY
src/main/java/org/apache/commons/io/CopyUtils.java
src/main/java/org/apache/commons/io/CopyUtils.java
*/
package org.apache.commons.io;
+import static org.apache.commons.io.IOUtils.EOF;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public CopyUtils() { }
/**
- * Copy bytes from a {@code byte[]} to an {@code OutputStream}.
+ * Copies bytes from a {@code byte[]} to an {@code OutputStream}.
* @param input the byte array to read from
* @param output the {@code OutputStream} to write to
* @throws IOException In case of an I/O problem
@@ -132,7 +134,7 @@ public static void copy(final byte[] input, final OutputStream output) throws IO
}
/**
- * Copy and convert bytes from a {@code byte[]} to chars on a
+ * Copies and convert bytes from a {@code byte[]} to chars on a
* {@code Writer}.
* The platform's default encoding is used for the byte-to-char conversion.
* @param input the byte array to read from
@@ -147,7 +149,7 @@ public static void copy(final byte[] input, final Writer output) throws IOExcept
}
/**
- * Copy and convert bytes from a {@code byte[]} to chars on a
+ * Copies and convert bytes from a {@code byte[]} to chars on a
* {@code Writer}, using the specified encoding.
* @param input the byte array to read from
* @param output the {@code Writer} to write to
@@ -162,7 +164,7 @@ public static void copy(final byte[] input, final Writer output, final String en
}
/**
- * Copy bytes from an {@code InputStream} to an
+ * Copies bytes from an {@code InputStream} to an
* {@code OutputStream}.
* @param input the {@code InputStream} to read from
* @param output the {@code OutputStream} to write to
@@ -173,7 +175,7 @@ public static int copy(final InputStream input, final OutputStream output) throw
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
- while (-1 != (n = input.read(buffer))) {
+ while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
@@ -185,7 +187,7 @@ public static int copy(final InputStream input, final OutputStream output) throw
// ----------------------------------------------------------------
/**
- * Copy chars from a {@code Reader} to a {@code Writer}.
+ * Copies chars from a {@code Reader} to a {@code Writer}.
* @param input the {@code Reader} to read from
* @param output the {@code Writer} to write to
* @return the number of characters copied
@@ -198,7 +200,7 @@ public static int copy(
final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
- while (-1 != (n = input.read(buffer))) {
+ while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
@@ -210,7 +212,7 @@ public static int copy(
// ----------------------------------------------------------------
/**
- * Copy and convert bytes from an {@code InputStream} to chars on a
+ * Copies and convert bytes from an {@code InputStream} to chars on a
* {@code Writer}.
* The platform's default encoding is used for the byte-to-char conversion.
* @param input the {@code InputStream} to read from
@@ -229,7 +231,7 @@ public static void copy(
}
/**
- * Copy and convert bytes from an {@code InputStream} to chars on a
+ * Copies and convert bytes from an {@code InputStream} to chars on a
* {@code Writer}, using the specified encoding.
* @param input the {@code InputStream} to read from
* @param output the {@code Writer} to write to
@@ -355,7 +357,7 @@ public static void copy(
// ----------------------------------------------------------------
/**
- * Copy chars from a {@code String} to a {@code Writer}.
+ * Copies chars from a {@code String} to a {@code Writer}.
* @param input the {@code String} to read from
* @param output the {@code Writer} to write to
* @throws IOException In case of an I/O problem

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -853,7 +853,7 @@ public static void copyFile(final File srcFile, final File destFile, final CopyO
}
/**
- * Copy bytes from a {@code File} to an {@code OutputStream}.
+ * Copies bytes from a {@code File} to an {@code OutputStream}.
* <p>
* This method buffers the input internally, so there is no need to use a {@code BufferedInputStream}.
* </p>

MODIFY
src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java
src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
@@ -56,8 +58,8 @@ public int read() throws IOException {
@Override
public int read(final char[] cbuf, final int off, final int len) throws IOException {
final int read = super.read(cbuf, off, len);
- if (read == -1) {
- return -1;
+ if (read == EOF) {
+ return EOF;
}
int pos = off - 1;
for (int readPos = off; readPos < off + read; readPos++) {

MODIFY
src/main/java/org/apache/commons/io/input/BoundedReader.java
src/main/java/org/apache/commons/io/input/BoundedReader.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
import java.io.IOException;
import java.io.Reader;
@@ -102,7 +104,7 @@ public void mark(final int readAheadLimit) throws IOException {
/**
* Reads a single character
*
- * @return -1 on eof or the character read
+ * @return -1 on EOF or the character read
* @throws IOException If an I/O error occurs while calling the underlying reader's read method
* @see java.io.Reader#read()
*/
@@ -110,11 +112,11 @@ public void mark(final int readAheadLimit) throws IOException {
public int read() throws IOException {
if (charsRead >= maxCharsFromTargetReader) {
- return -1;
+ return EOF;
}
if (markedAt >= 0 && (charsRead - markedAt) >= readAheadLimit) {
- return -1;
+ return EOF;
}
charsRead++;
return target.read();
@@ -135,8 +137,8 @@ public int read(final char[] cbuf, final int off, final int len) throws IOExcept
int c;
for (int i = 0; i < len; i++) {
c = read();
- if (c == -1) {
- return i == 0 ? -1 : i;
+ if (c == EOF) {
+ return i == 0 ? EOF : i;
}
cbuf[off + i] = (char) c;
}

MODIFY
src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -183,7 +185,7 @@ public synchronized void close() throws IOException {
@Override
public synchronized int read() throws IOException {
if (!refill()) {
- return -1;
+ return EOF;
}
return byteBuffer.get() & 0xFF;
}
@@ -194,7 +196,7 @@ public synchronized int read(final byte[] b, final int offset, int len) throws I
throw new IndexOutOfBoundsException();
}
if (!refill()) {
- return -1;
+ return EOF;
}
len = Math.min(len, byteBuffer.remaining());
byteBuffer.get(b, offset, len);

MODIFY
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
-
/**
- * The {@link ObservableInputStream} allows, that an InputStream may be consumed
- * by other receivers, apart from the thread, which is reading it.
- * The other consumers are implemented as instances of {@link Observer}. A
- * typical application may be the generation of a {@link java.security.MessageDigest} on the
- * fly.
- * {@code Note}: The {@link ObservableInputStream} is <em>not</em> thread safe,
- * as instances of InputStream usually aren't.
- * If you must access the stream from multiple threads, then synchronization, locking,
- * or a similar means must be used.
+ * The {@link ObservableInputStream} allows, that an InputStream may be consumed by other receivers, apart from the
+ * thread, which is reading it. The other consumers are implemented as instances of {@link Observer}. A typical
+ * application may be the generation of a {@link java.security.MessageDigest} on the fly. {@code Note}: The
+ * {@link ObservableInputStream} is <em>not</em> thread safe, as instances of InputStream usually aren't. If you must
+ * access the stream from multiple threads, then synchronization, locking, or a similar means must be used.
+ *
* @see MessageDigestCalculatingInputStream
*/
public class ObservableInputStream extends ProxyInputStream {
public static abstract class Observer {
/**
- * Called to indicate, that {@link InputStream#read()} has been invoked
- * on the {@link ObservableInputStream}, and will return a value.
- * @param pByte The value, which is being returned. This will never be -1 (EOF),
- * because, in that case, {@link #finished()} will be invoked instead.
+ * Called to indicate, that {@link InputStream#read()} has been invoked on the {@link ObservableInputStream},
+ * and will return a value.
+ *
+ * @param value The value, which is being returned. This will never be -1 (EOF), because, in that case,
+ * {@link #finished()} will be invoked instead.
* @throws IOException if an I/O error occurs.
*/
@SuppressWarnings("unused") // Possibly thrown from subclasses.
- public void data(final int pByte) throws IOException {
+ public void data(final int value) throws IOException {
// noop
}
/**
- * Called to indicate that {@link InputStream#read(byte[])}, or
- * {@link InputStream#read(byte[], int, int)} have been called, and are about to
- * invoke data.
- * @param pBuffer The byte array, which has been passed to the read call, and where
- * data has been stored.
- * @param pOffset The offset within the byte array, where data has been stored.
- * @param pLength The number of bytes, which have been stored in the byte array.
+ * Called to indicate that {@link InputStream#read(byte[])}, or {@link InputStream#read(byte[], int, int)} have
+ * been called, and are about to invoke data.
+ *
+ * @param buffer The byte array, which has been passed to the read call, and where data has been stored.
+ * @param offset The offset within the byte array, where data has been stored.
+ * @param length The number of bytes, which have been stored in the byte array.
* @throws IOException if an I/O error occurs.
*/
@SuppressWarnings("unused") // Possibly thrown from subclasses.
- public void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
+ public void data(final byte[] buffer, final int offset, final int length) throws IOException {
// noop
}
/**
- * Called to indicate that EOF has been seen on the underlying stream.
- * This method may be called multiple times, if the reader keeps invoking
- * either of the read methods, and they will consequently keep returning
- * EOF.
+ * Called to indicate that EOF has been seen on the underlying stream. This method may be called multiple times,
+ * if the reader keeps invoking either of the read methods, and they will consequently keep returning EOF.
+ *
* @throws IOException if an I/O error occurs.
*/
@SuppressWarnings("unused") // Possibly thrown from subclasses.
@@ -84,6 +81,7 @@ public void finished() throws IOException {
/**
* Called to indicate that the {@link ObservableInputStream} has been closed.
+ *
* @throws IOException if an I/O error occurs.
*/
@SuppressWarnings("unused") // Possibly thrown from subclasses.
@@ -93,11 +91,12 @@ public void closed() throws IOException {
/**
* Called to indicate that an error occurred on the underlying stream.
- * @param pException the exception to throw
+ *
+ * @param exception the exception to throw
* @throws IOException if an I/O error occurs.
*/
- public void error(final IOException pException) throws IOException {
- throw pException;
+ public void error(final IOException exception) throws IOException {
+ throw exception;
}
}
@@ -105,26 +104,29 @@ public void error(final IOException pException) throws IOException {
/**
* Creates a new ObservableInputStream for the given InputStream.
- * @param pProxy the input stream to proxy
+ *
+ * @param inputStream the input stream to proxy.
*/
- public ObservableInputStream(final InputStream pProxy) {
- super(pProxy);
+ public ObservableInputStream(final InputStream inputStream) {
+ super(inputStream);
}
/**
* Adds an Observer.
- * @param pObserver the observer to add
+ *
+ * @param observer the observer to add
*/
- public void add(final Observer pObserver) {
- observers.add(pObserver);
+ public void add(final Observer observer) {
+ observers.add(observer);
}
/**
* Removes an Observer.
- * @param pObserver the observer to remove
+ *
+ * @param observer the observer to remove
*/
- public void remove(final Observer pObserver) {
- observers.remove(pObserver);
+ public void remove(final Observer observer) {
+ observers.remove(observer);
}
/**
@@ -145,7 +147,7 @@ public int read() throws IOException {
}
if (ioe != null) {
noteError(ioe);
- } else if (result == -1) {
+ } else if (result == EOF) {
noteFinished();
} else {
noteDataByte(result);
@@ -154,60 +156,61 @@ public int read() throws IOException {
}
@Override
- public int read(final byte[] pBuffer) throws IOException {
+ public int read(final byte[] buffer) throws IOException {
int result = 0;
IOException ioe = null;
try {
- result = super.read(pBuffer);
+ result = super.read(buffer);
} catch (final IOException pException) {
ioe = pException;
}
if (ioe != null) {
noteError(ioe);
- } else if (result == -1) {
+ } else if (result == EOF) {
noteFinished();
} else if (result > 0) {
- noteDataBytes(pBuffer, 0, result);
+ noteDataBytes(buffer, 0, result);
}
return result;
}
@Override
- public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
+ public int read(final byte[] buffer, final int offset, final int length) throws IOException {
int result = 0;
IOException ioe = null;
try {
- result = super.read(pBuffer, pOffset, pLength);
+ result = super.read(buffer, offset, length);
} catch (final IOException pException) {
ioe = pException;
}
if (ioe != null) {
noteError(ioe);
- } else if (result == -1) {
+ } else if (result == EOF) {
noteFinished();
} else if (result > 0) {
- noteDataBytes(pBuffer, pOffset, result);
+ noteDataBytes(buffer, offset, result);
}
return result;
}
- /** Notifies the observers by invoking {@link Observer#data(byte[],int,int)}
- * with the given arguments.
- * @param pBuffer Passed to the observers.
- * @param pOffset Passed to the observers.
- * @param pLength Passed to the observers.
- * @throws IOException Some observer has thrown an exception, which is being
- * passed down.
+ /**
+ * Notifies the observers by invoking {@link Observer#data(byte[],int,int)} with the given arguments.
+ *
+ * @param buffer Passed to the observers.
+ * @param offset Passed to the observers.
+ * @param length Passed to the observers.
+ * @throws IOException Some observer has thrown an exception, which is being passed down.
*/
- protected void noteDataBytes(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
+ protected void noteDataBytes(final byte[] buffer, final int offset, final int length) throws IOException {
for (final Observer observer : getObservers()) {
- observer.data(pBuffer, pOffset, pLength);
+ observer.data(buffer, offset, length);
}
}
- /** Notifies the observers by invoking {@link Observer#finished()}.
- * @throws IOException Some observer has thrown an exception, which is being
- * passed down.
+ /**
+ * Notifies the observers by invoking {@link Observer#finished()}.
+ *
+ * @throws IOException Some observer has thrown an exception, which is being passed down.
*/
protected void noteFinished() throws IOException {
for (final Observer observer : getObservers()) {
@@ -215,34 +218,35 @@ protected void noteFinished() throws IOException {
}
}
- /** Notifies the observers by invoking {@link Observer#data(int)}
- * with the given arguments.
- * @param pDataByte Passed to the observers.
- * @throws IOException Some observer has thrown an exception, which is being
- * passed down.
+ /**
+ * Notifies the observers by invoking {@link Observer#data(int)} with the given arguments.
+ *
+ * @param value Passed to the observers.
+ * @throws IOException Some observer has thrown an exception, which is being passed down.
*/
- protected void noteDataByte(final int pDataByte) throws IOException {
+ protected void noteDataByte(final int value) throws IOException {
for (final Observer observer : getObservers()) {
- observer.data(pDataByte);
+ observer.data(value);
}
}
- /** Notifies the observers by invoking {@link Observer#error(IOException)}
- * with the given argument.
- * @param pException Passed to the observers.
- * @throws IOException Some observer has thrown an exception, which is being
- * passed down. This may be the same exception, which has been passed as an
- * argument.
+ /**
+ * Notifies the observers by invoking {@link Observer#error(IOException)} with the given argument.
+ *
+ * @param exception Passed to the observers.
+ * @throws IOException Some observer has thrown an exception, which is being passed down. This may be the same
+ * exception, which has been passed as an argument.
*/
- protected void noteError(final IOException pException) throws IOException {
+ protected void noteError(final IOException exception) throws IOException {
for (final Observer observer : getObservers()) {
- observer.error(pException);
+ observer.error(exception);
}
}
- /** Notifies the observers by invoking {@link Observer#finished()}.
- * @throws IOException Some observer has thrown an exception, which is being
- * passed down.
+ /**
+ * Notifies the observers by invoking {@link Observer#finished()}.
+ *
+ * @throws IOException Some observer has thrown an exception, which is being passed down.
*/
protected void noteClosed() throws IOException {
for (final Observer observer : getObservers()) {
@@ -250,7 +254,9 @@ protected void noteClosed() throws IOException {
}
}
- /** Gets all currently registered observers.
+ /**
+ * Gets all currently registered observers.
+ *
* @return a list of the currently registered observers
*/
protected List<Observer> getObservers() {
@@ -272,18 +278,15 @@ public void close() throws IOException {
}
}
- /** Reads all data from the underlying {@link InputStream}, while notifying the
- * observers.
- * @throws IOException The underlying {@link InputStream}, or either of the
- * observers has thrown an exception.
+ /**
+ * Reads all data from the underlying {@link InputStream}, while notifying the observers.
+ *
+ * @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception.
*/
public void consume() throws IOException {
final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
- for (;;) {
- final int res = read(buffer);
- if (res == -1) {
- return;
- }
+ while (read(buffer) != EOF) {
+ // empty
}
}

MODIFY
src/main/java/org/apache/commons/io/input/QueueInputStream.java
src/main/java/org/apache/commons/io/input/QueueInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
import org.apache.commons.io.output.QueueOutputStream;
import java.io.InputStream;
@@ -91,7 +93,7 @@ public QueueOutputStream newQueueOutputStream() {
@Override
public int read() {
final Integer value = blockingQueue.poll();
- return value == null ? -1 : ((0xFF) & value);
+ return value == null ? EOF : ((0xFF) & value);
}
}

MODIFY
src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.EOF;
+
// import javax.annotation.concurrent.GuardedBy;
import java.io.EOFException;
import java.io.IOException;
@@ -242,7 +244,7 @@ public int read() throws IOException {
return activeBuffer.get() & 0xFF;
}
final byte[] oneByteArray = oneByte.get();
- return read(oneByteArray, 0, 1) == -1 ? -1 : oneByteArray[0] & 0xFF;
+ return read(oneByteArray, 0, 1) == EOF ? -1 : oneByteArray[0] & 0xFF;
}
@Override
@@ -264,7 +266,7 @@ public int read(final byte[] b, final int offset, int len) throws IOException {
readAsync();
waitForAsyncReadComplete();
if (isEndOfStream()) {
- return -1;
+ return EOF;
}
}
// Swap the newly read read ahead buffer in place of empty active buffer.

Reuse EOF constant.

Javadoc tweaks. Format to max line length. Simplify.