This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new c09c054 Reuse EOF constant. c09c054 is described below commit c09c054a29ae1fead7298203a07c7eed1b8ccf65 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Thu Jan 28 21:20:53 2021 -0500 Reuse EOF constant. Javadoc tweaks. Format to max line length. Simplify. --- src/main/java/org/apache/commons/io/CopyUtils.java | 22 +-- src/main/java/org/apache/commons/io/FileUtils.java | 2 +- .../io/input/AbstractCharacterFilterReader.java | 6 +- .../org/apache/commons/io/input/BoundedReader.java | 12 +- .../io/input/BufferedFileChannelInputStream.java | 6 +- .../commons/io/input/ObservableInputStream.java | 179 +++++++++++---------- .../apache/commons/io/input/QueueInputStream.java | 4 +- .../commons/io/input/ReadAheadInputStream.java | 6 +- 8 files changed, 126 insertions(+), 111 deletions(-) diff --git a/src/main/java/org/apache/commons/io/CopyUtils.java b/src/main/java/org/apache/commons/io/CopyUtils.java index d555c1e..df42bcb 100644 --- a/src/main/java/org/apache/commons/io/CopyUtils.java +++ b/src/main/java/org/apache/commons/io/CopyUtils.java @@ -16,6 +16,8 @@ */ 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; @@ -122,7 +124,7 @@ public class CopyUtils { 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 class CopyUtils { } /** - * 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 class CopyUtils { } /** - * 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 class CopyUtils { } /** - * 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 class CopyUtils { 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 class CopyUtils { // ---------------------------------------------------------------- /** - * 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 class CopyUtils { 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 class CopyUtils { // ---------------------------------------------------------------- /** - * 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 class CopyUtils { } /** - * 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 class CopyUtils { // ---------------------------------------------------------------- /** - * 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 diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 987b7ad..c0eb833 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -853,7 +853,7 @@ public class FileUtils { } /** - * 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> diff --git a/src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java b/src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java index 3f51bb5..b829dfc 100644 --- a/src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java +++ b/src/main/java/org/apache/commons/io/input/AbstractCharacterFilterReader.java @@ -16,6 +16,8 @@ */ 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 abstract class AbstractCharacterFilterReader extends FilterReader { @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++) { diff --git a/src/main/java/org/apache/commons/io/input/BoundedReader.java b/src/main/java/org/apache/commons/io/input/BoundedReader.java index 799bc34..3b3cde2 100644 --- a/src/main/java/org/apache/commons/io/input/BoundedReader.java +++ b/src/main/java/org/apache/commons/io/input/BoundedReader.java @@ -18,6 +18,8 @@ */ 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 class BoundedReader extends Reader { /** * 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 class BoundedReader extends Reader { 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 class BoundedReader extends Reader { 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; } diff --git a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java index 67ff54a..875b6d9 100644 --- a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java +++ b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java @@ -13,6 +13,8 @@ */ 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 final class BufferedFileChannelInputStream extends InputStream { @Override public synchronized int read() throws IOException { if (!refill()) { - return -1; + return EOF; } return byteBuffer.get() & 0xFF; } @@ -194,7 +196,7 @@ public final class BufferedFileChannelInputStream extends InputStream { throw new IndexOutOfBoundsException(); } if (!refill()) { - return -1; + return EOF; } len = Math.min(len, byteBuffer.remaining()); byteBuffer.get(b, offset, len); diff --git a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java index 2343a56..c287dae 100644 --- a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java @@ -16,6 +16,8 @@ */ 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; @@ -23,17 +25,13 @@ import java.util.List; 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 { @@ -44,37 +42,36 @@ 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 class ObservableInputStream extends ProxyInputStream { /** * 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 class ObservableInputStream extends ProxyInputStream { /** * 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 class ObservableInputStream extends ProxyInputStream { /** * 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 class ObservableInputStream extends ProxyInputStream { } if (ioe != null) { noteError(ioe); - } else if (result == -1) { + } else if (result == EOF) { noteFinished(); } else { noteDataByte(result); @@ -154,60 +156,61 @@ public class ObservableInputStream extends ProxyInputStream { } @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 @@ public class ObservableInputStream extends ProxyInputStream { } } - /** 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 @@ public class ObservableInputStream extends ProxyInputStream { } } - /** 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 class ObservableInputStream extends ProxyInputStream { } } - /** 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 } } diff --git a/src/main/java/org/apache/commons/io/input/QueueInputStream.java b/src/main/java/org/apache/commons/io/input/QueueInputStream.java index bf589cb..02d036c 100644 --- a/src/main/java/org/apache/commons/io/input/QueueInputStream.java +++ b/src/main/java/org/apache/commons/io/input/QueueInputStream.java @@ -16,6 +16,8 @@ */ 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 class QueueInputStream extends InputStream { @Override public int read() { final Integer value = blockingQueue.poll(); - return value == null ? -1 : ((0xFF) & value); + return value == null ? EOF : ((0xFF) & value); } } diff --git a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java index 878af30..34e69ac 100644 --- a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java @@ -13,6 +13,8 @@ */ 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 class ReadAheadInputStream extends InputStream { 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 class ReadAheadInputStream extends InputStream { readAsync(); waitForAsyncReadComplete(); if (isEndOfStream()) { - return -1; + return EOF; } } // Swap the newly read read ahead buffer in place of empty active buffer.