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
commit bdb9f8b9095004703666675591c3c3de30e7c03a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed May 3 14:05:41 2023 -0400 Use builder and deprecate some ctors --- .../apache/commons/io/build/AbstractOrigin.java | 43 +++++- .../commons/io/build/AbstractOriginSupplier.java | 21 +++ .../io/input/UncheckedFilterInputStream.java | 44 ++++-- .../commons/io/input/UncheckedFilterReader.java | 44 ++++-- .../io/input/UnixLineEndingInputStream.java | 49 +++---- .../input/UnsynchronizedBufferedInputStream.java | 132 ++++++++++------- .../input/UnsynchronizedByteArrayInputStream.java | 86 +++++++++++- .../io/input/UnsynchronizedFilterInputStream.java | 66 +++++++-- .../io/output/AbstractByteArrayOutputStream.java | 4 +- .../UnsynchronizedByteArrayOutputStream.java | 10 +- .../io/input/UncheckedFilterInputStreamTest.java | 16 ++- .../io/input/UncheckedFilterReaderTest.java | 21 +-- .../UnsynchronizedBufferedInputStreamTest.java | 22 +-- .../UnsynchronizedByteArrayInputStreamTest.java | 156 ++++++++++++--------- .../input/UnsynchronizedFilterInputStreamTest.java | 8 +- 15 files changed, 498 insertions(+), 224 deletions(-) diff --git a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java index 1f4b69d3..46a008ec 100644 --- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java +++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java @@ -45,6 +45,28 @@ import java.util.Objects; */ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends AbstractSupplier<T, B> { + /** + * A {@link File} origin. + */ + public static class ByteArrayOrigin extends AbstractOrigin<byte[], ByteArrayOrigin> { + + /** + * Constructs a new instance for the given origin. + * + * @param origin The origin. + */ + public ByteArrayOrigin(final byte[] origin) { + super(origin); + } + + @Override + public byte[] getByteArray() { + // No conversion + return get(); + } + + } + /** * A {@link File} origin. */ @@ -61,6 +83,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public File getFile() { + // No conversion return get(); } @@ -90,6 +113,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public InputStream getInputStream(final OpenOption... options) { + // No conversion return get(); } @@ -114,6 +138,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public OutputStream getOutputStream(final OpenOption... options) { + // No conversion return get(); } @@ -140,6 +165,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public Path getPath() { + // No conversion return get(); } @@ -164,6 +190,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public Reader getReader(final Charset charset) throws IOException { + // No conversion return get(); } } @@ -182,11 +209,6 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends super(origin); } - @Override - public URI get() { - return origin; - } - @Override public File getFile() { return getPath().toFile(); @@ -218,6 +240,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends @Override public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException { + // No conversion return get(); } } @@ -246,6 +269,16 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends return origin; } + /** + * Gets this origin as a byte array, if possible. + * + * @return this origin as a byte array, if possible. + * @throws IOException if an I/O error occurs. + */ + public byte[] getByteArray() throws IOException { + return Files.readAllBytes(getPath()); + } + /** * Gets this origin as a Path, if possible. * diff --git a/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java b/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java index 9f2c599a..e6ed6db4 100644 --- a/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java +++ b/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java @@ -27,6 +27,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Objects; +import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin; import org.apache.commons.io.build.AbstractOrigin.FileOrigin; import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin; import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin; @@ -57,6 +58,16 @@ public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier return initialBufferSize; } + /** + * Creates a new byte array origin for a byte array. + * + * @param origin the file. + * @return a new file origin + */ + protected static ByteArrayOrigin newByteArrayOrigin(final byte[] origin) { + return new ByteArrayOrigin(origin); + } + /** * Creates a new file origin for a file. * @@ -180,6 +191,16 @@ public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier return origin != null; } + /** + * Sets a new origin. + * + * @param origin the new origin. + * @return this + */ + public B setByteArray(final byte[] origin) { + return setOrigin(newByteArrayOrigin(origin)); + } + /** * Sets a new origin. * diff --git a/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java b/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java index b69602b8..d2deaf6d 100644 --- a/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; +import org.apache.commons.io.build.AbstractStreamBuilder; import org.apache.commons.io.function.Uncheck; /** @@ -33,17 +34,44 @@ import org.apache.commons.io.function.Uncheck; * @see UncheckedIOException * @since 2.12.0 */ -public class UncheckedFilterInputStream extends FilterInputStream { +public final class UncheckedFilterInputStream extends FilterInputStream { /** - * Creates a {@link UncheckedFilterInputStream}. + * Builds a new {@link UncheckedFilterInputStream} instance. + * <p> + * Using File IO: + * </p> + * <pre>{@code + * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder() + * .setFile(file) + * .get()} + * </pre> + * <p> + * Using NIO Path: + * </p> + * <pre>{@code + * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder() + * .setPath(path) + * .get()} + * </pre> + */ + public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputStream, Builder> { + + @Override + public UncheckedFilterInputStream get() { + // This an unchecked class, so this method is as well. + return Uncheck.get(() -> new UncheckedFilterInputStream(getOrigin().getInputStream())); + } + + } + + /** + * Constructs a new {@link Builder}. * - * @param inputStream the underlying input stream, or {@code null} if this instance is to be created without an - * underlying stream. - * @return a new UncheckedFilterInputStream. + * @return a new {@link Builder}. */ - public static UncheckedFilterInputStream on(final InputStream inputStream) { - return new UncheckedFilterInputStream(inputStream); + public static Builder builder() { + return new Builder(); } /** @@ -52,7 +80,7 @@ public class UncheckedFilterInputStream extends FilterInputStream { * @param inputStream the underlying input stream, or {@code null} if this instance is to be created without an * underlying stream. */ - public UncheckedFilterInputStream(final InputStream inputStream) { + private UncheckedFilterInputStream(final InputStream inputStream) { super(inputStream); } diff --git a/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java index 794630a0..ab219c2c 100644 --- a/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java +++ b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java @@ -23,6 +23,7 @@ import java.io.Reader; import java.io.UncheckedIOException; import java.nio.CharBuffer; +import org.apache.commons.io.build.AbstractStreamBuilder; import org.apache.commons.io.function.Uncheck; /** @@ -33,17 +34,44 @@ import org.apache.commons.io.function.Uncheck; * @see UncheckedIOException * @since 2.12.0 */ -public class UncheckedFilterReader extends FilterReader { +public final class UncheckedFilterReader extends FilterReader { /** - * Creates a new filtered reader. + * Builds a new {@link UncheckedFilterReader} instance. + * <p> + * Using File IO: + * </p> + * <pre>{@code + * UncheckedFilterReader s = UncheckedFilterReader.builder() + * .setFile(file) + * .get()} + * </pre> + * <p> + * Using NIO Path: + * </p> + * <pre>{@code + * UncheckedFilterReader s = UncheckedFilterReader.builder() + * .setPath(path) + * .get()} + * </pre> + */ + public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader, Builder> { + + @Override + public UncheckedFilterReader get() { + // This an unchecked class, so this method is as well. + return Uncheck.get(() -> new UncheckedFilterReader(getOrigin().getReader(getCharset()))); + } + + } + + /** + * Constructs a new {@link Builder}. * - * @param reader a Reader object providing the underlying stream. - * @return a new UncheckedFilterReader. - * @throws NullPointerException if {@code reader} is {@code null}. + * @return a new {@link Builder}. */ - public static UncheckedFilterReader on(final Reader reader) { - return new UncheckedFilterReader(reader); + public static Builder builder() { + return new Builder(); } /** @@ -52,7 +80,7 @@ public class UncheckedFilterReader extends FilterReader { * @param reader a Reader object providing the underlying stream. * @throws NullPointerException if {@code reader} is {@code null}. */ - public UncheckedFilterReader(final Reader reader) { + private UncheckedFilterReader(final Reader reader) { super(reader); } diff --git a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java index 40dd6925..35a68493 100644 --- a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java @@ -30,11 +30,11 @@ import java.io.InputStream; */ public class UnixLineEndingInputStream extends InputStream { - private boolean slashNSeen; + private boolean atSlashLf; - private boolean slashRSeen; + private boolean atSlashCr; - private boolean eofSeen; + private boolean atEos; private final InputStream target; @@ -43,11 +43,11 @@ public class UnixLineEndingInputStream extends InputStream { /** * Creates an input stream that filters another stream * - * @param in The input stream to wrap + * @param inputStream The input stream to wrap * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with LF */ - public UnixLineEndingInputStream(final InputStream in, final boolean ensureLineFeedAtEndOfFile) { - this.target = in; + public UnixLineEndingInputStream(final InputStream inputStream, final boolean ensureLineFeedAtEndOfFile) { + this.target = inputStream; this.ensureLineFeedAtEndOfFile = ensureLineFeedAtEndOfFile; } @@ -62,16 +62,17 @@ public class UnixLineEndingInputStream extends InputStream { } /** - * Handles the EOF-handling at the end of the stream - * @param previousWasSlashR Indicates if the last seen was a \r - * @return The next char to output to the stream + * Handles the end of stream condition. + * + * @param previousWasSlashCr Indicates if the last seen was a {@code \r}. + * @return The next char to output to the stream. */ - private int eofGame(final boolean previousWasSlashR) { - if (previousWasSlashR || !ensureLineFeedAtEndOfFile) { + private int handleEos(final boolean previousWasSlashCr) { + if (previousWasSlashCr || !ensureLineFeedAtEndOfFile) { return EOF; } - if (!slashNSeen) { - slashNSeen = true; + if (!atSlashLf) { + atSlashLf = true; return LF; } return EOF; @@ -90,19 +91,19 @@ public class UnixLineEndingInputStream extends InputStream { */ @Override public int read() throws IOException { - final boolean previousWasSlashR = slashRSeen; - if (eofSeen) { - return eofGame(previousWasSlashR); + final boolean previousWasSlashR = atSlashCr; + if (atEos) { + return handleEos(previousWasSlashR); } final int target = readWithUpdate(); - if (eofSeen) { - return eofGame(previousWasSlashR); + if (atEos) { + return handleEos(previousWasSlashR); } - if (slashRSeen) { + if (atSlashCr) { return LF; } - if (previousWasSlashR && slashNSeen) { + if (previousWasSlashR && atSlashLf) { return read(); } @@ -116,12 +117,12 @@ public class UnixLineEndingInputStream extends InputStream { */ private int readWithUpdate() throws IOException { final int target = this.target.read(); - eofSeen = target == EOF; - if (eofSeen) { + atEos = target == EOF; + if (atEos) { return target; } - slashNSeen = target == LF; - slashRSeen = target == CR; + atSlashLf = target == LF; + atSlashCr = target == CR; return target; } } diff --git a/src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStream.java b/src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStream.java index 2352d9eb..fc90c3f3 100644 --- a/src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStream.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.build.AbstractStreamBuilder; /** * An unsynchronized version of {@link BufferedInputStream}, not thread-safe. @@ -45,11 +46,43 @@ import org.apache.commons.io.IOUtils; * @since 2.12.0 */ //@NotThreadSafe -public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInputStream { +public final class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInputStream { + + /** + * Builds a new {@link UnsynchronizedBufferedInputStream} instance. + * <p> + * Using File IO: + * </p> + * <pre>{@code + * UnsynchronizedBufferedInputStream s = UnsynchronizedBufferedInputStream.builder() + * .setFile(file) + * .setBufferSize(8192) + * .get()} + * </pre> + * <p> + * Using NIO Path: + * </p> + * <pre>{@code + * UnsynchronizedBufferedInputStream s = UnsynchronizedBufferedInputStream.builder() + * .setPath(path) + * .setBufferSize(8192) + * .get()} + * </pre> + */ + public static class Builder extends AbstractStreamBuilder<UnsynchronizedBufferedInputStream, Builder> { + + @SuppressWarnings("resource") // Caller closes. + @Override + public UnsynchronizedBufferedInputStream get() throws IOException { + return new UnsynchronizedBufferedInputStream(getOrigin().getInputStream(), getBufferSize()); + } + + } + /** * The buffer containing the current bytes read from the target InputStream. */ - protected volatile byte[] buf; + protected volatile byte[] buffer; /** * The total number of bytes inside the byte array {@code buf}. @@ -59,29 +92,18 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput /** * The current limit, which when passed, invalidates the current mark. */ - protected int marklimit; + protected int markLimit; /** * The currently marked position. -1 indicates no mark has been set or the mark has been invalidated. */ - protected int markpos = IOUtils.EOF; + protected int markPos = IOUtils.EOF; /** * The current position within the byte array {@code buf}. */ protected int pos; - /** - * Constructs a new {@code BufferedInputStream} on the {@link InputStream} {@code in}. The default buffer size (8 KB) is allocated and all reads can now be - * filtered through this stream. - * - * @param in the InputStream the buffer reads from. - */ - public UnsynchronizedBufferedInputStream(final InputStream in) { - super(in); - buf = new byte[IOUtils.DEFAULT_BUFFER_SIZE]; - } - /** * Constructs a new {@code BufferedInputStream} on the {@link InputStream} {@code in}. The buffer size is specified by the parameter {@code size} and all * reads are now filtered through this stream. @@ -90,12 +112,12 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput * @param size the size of buffer to allocate. * @throws IllegalArgumentException if {@code size < 0}. */ - public UnsynchronizedBufferedInputStream(final InputStream in, final int size) { + private UnsynchronizedBufferedInputStream(final InputStream in, final int size) { super(in); if (size <= 0) { throw new IllegalArgumentException("Size must be > 0"); } - buf = new byte[size]; + buffer = new byte[size]; } /** @@ -107,8 +129,8 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput */ @Override public int available() throws IOException { - final InputStream localIn = in; // 'in' could be invalidated by close() - if (buf == null || localIn == null) { + final InputStream localIn = inputStream; // 'in' could be invalidated by close() + if (buffer == null || localIn == null) { throw new IOException("Stream is closed"); } return count - pos + localIn.available(); @@ -121,47 +143,51 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput */ @Override public void close() throws IOException { - buf = null; - final InputStream localIn = in; - in = null; + buffer = null; + final InputStream localIn = inputStream; + inputStream = null; if (localIn != null) { localIn.close(); } } - private int fillbuf(final InputStream localIn, byte[] localBuf) throws IOException { - if (markpos == IOUtils.EOF || pos - markpos >= marklimit) { + private int fillBuffer(final InputStream localIn, byte[] localBuf) throws IOException { + if (markPos == IOUtils.EOF || pos - markPos >= markLimit) { /* Mark position not set or exceeded readlimit */ final int result = localIn.read(localBuf); if (result > 0) { - markpos = IOUtils.EOF; + markPos = IOUtils.EOF; pos = 0; count = result; } return result; } - if (markpos == 0 && marklimit > localBuf.length) { + if (markPos == 0 && markLimit > localBuf.length) { /* Increase buffer size to accommodate the readlimit */ int newLength = localBuf.length * 2; - if (newLength > marklimit) { - newLength = marklimit; + if (newLength > markLimit) { + newLength = markLimit; } final byte[] newbuf = new byte[newLength]; System.arraycopy(localBuf, 0, newbuf, 0, localBuf.length); // Reassign buf, which will invalidate any local references // FIXME: what if buf was null? - localBuf = buf = newbuf; - } else if (markpos > 0) { - System.arraycopy(localBuf, markpos, localBuf, 0, localBuf.length - markpos); + localBuf = buffer = newbuf; + } else if (markPos > 0) { + System.arraycopy(localBuf, markPos, localBuf, 0, localBuf.length - markPos); } /* Set the new position and mark position */ - pos -= markpos; - count = markpos = 0; + pos -= markPos; + count = markPos = 0; final int bytesread = localIn.read(localBuf, pos, localBuf.length - pos); count = bytesread <= 0 ? pos : pos + bytesread; return bytesread; } + byte[] getBuffer() { + return buffer; + } + /** * Sets a mark position in this stream. The parameter {@code readlimit} indicates how many bytes can be read before a mark is invalidated. Calling * {@code reset()} will reposition the stream back to the marked position if {@code readlimit} has not been surpassed. The underlying buffer may be @@ -172,8 +198,8 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput */ @Override public void mark(final int readlimit) { - marklimit = readlimit; - markpos = pos; + markLimit = readlimit; + markPos = pos; } /** @@ -199,19 +225,19 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput public int read() throws IOException { // Use local refs since buf and in may be invalidated by an // unsynchronized close() - byte[] localBuf = buf; - final InputStream localIn = in; + byte[] localBuf = buffer; + final InputStream localIn = inputStream; if (localBuf == null || localIn == null) { throw new IOException("Stream is closed"); } /* Are there buffered bytes available? */ - if (pos >= count && fillbuf(localIn, localBuf) == IOUtils.EOF) { + if (pos >= count && fillBuffer(localIn, localBuf) == IOUtils.EOF) { return IOUtils.EOF; /* no, fill buffer */ } // localBuf may have been invalidated by fillbuf - if (localBuf != buf) { - localBuf = buf; + if (localBuf != buffer) { + localBuf = buffer; if (localBuf == null) { throw new IOException("Stream is closed"); } @@ -241,7 +267,7 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput public int read(final byte[] buffer, int offset, final int length) throws IOException { // Use local ref since buf may be invalidated by an unsynchronized // close() - byte[] localBuf = buf; + byte[] localBuf = buffer; if (localBuf == null) { throw new IOException("Stream is closed"); } @@ -252,7 +278,7 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput if (length == 0) { return 0; } - final InputStream localIn = in; + final InputStream localIn = inputStream; if (localIn == null) { throw new IOException("Stream is closed"); } @@ -277,18 +303,18 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput /* * If we're not marked and the required size is greater than the buffer, simply read the bytes directly bypassing the buffer. */ - if (markpos == IOUtils.EOF && required >= localBuf.length) { + if (markPos == IOUtils.EOF && required >= localBuf.length) { read = localIn.read(buffer, offset, required); if (read == IOUtils.EOF) { return required == length ? IOUtils.EOF : length - required; } } else { - if (fillbuf(localIn, localBuf) == IOUtils.EOF) { + if (fillBuffer(localIn, localBuf) == IOUtils.EOF) { return required == length ? IOUtils.EOF : length - required; } // localBuf may have been invalidated by fillbuf - if (localBuf != buf) { - localBuf = buf; + if (localBuf != buffer) { + localBuf = buffer; if (localBuf == null) { throw new IOException("Stream is closed"); } @@ -318,13 +344,13 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput */ @Override public void reset() throws IOException { - if (buf == null) { + if (buffer == null) { throw new IOException("Stream is closed"); } - if (IOUtils.EOF == markpos) { + if (IOUtils.EOF == markPos) { throw new IOException("Mark has been invalidated"); } - pos = markpos; + pos = markPos; } /** @@ -338,8 +364,8 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput public long skip(final long amount) throws IOException { // Use local refs since buf and in may be invalidated by an // unsynchronized close() - final byte[] localBuf = buf; - final InputStream localIn = in; + final byte[] localBuf = buffer; + final InputStream localIn = inputStream; if (localBuf == null) { throw new IOException("Stream is closed"); } @@ -357,8 +383,8 @@ public class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInput long read = count - pos; pos = count; - if (markpos != IOUtils.EOF && amount <= marklimit) { - if (fillbuf(localIn, localBuf) == IOUtils.EOF) { + if (markPos != IOUtils.EOF && amount <= markLimit) { + if (fillBuffer(localIn, localBuf) == IOUtils.EOF) { return read; } if (count - pos >= amount - read) { diff --git a/src/main/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStream.java b/src/main/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStream.java index cd90b8c5..4d84c655 100644 --- a/src/main/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStream.java @@ -19,9 +19,12 @@ package org.apache.commons.io.input; import static java.lang.Math.min; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Objects; +import org.apache.commons.io.build.AbstractStreamBuilder; + /** * This is an alternative to {@link java.io.ByteArrayInputStream} * which removes the synchronization overhead for non-concurrent @@ -33,11 +36,87 @@ import java.util.Objects; //@NotThreadSafe public class UnsynchronizedByteArrayInputStream extends InputStream { + /** + * Builds a new {@link UnsynchronizedByteArrayInputStream} instance. + * <p> + * Using a Byte Array: + * </p> + * <pre>{@code + * UnsynchronizedByteArrayInputStream s = UnsynchronizedByteArrayInputStream.builder() + * .setByteArray(byteArray) + * .setOffset(0) + * .setLength(byteArray.length) + * .get()} + * </pre> + * <p> + * Using File IO: + * </p> + * <pre>{@code + * UnsynchronizedByteArrayInputStream s = UnsynchronizedByteArrayInputStream.builder() + * .setFile(file) + * .setOffset(0) + * .setLength(byteArray.length) + * .get()} + * </pre> + * <p> + * Using NIO Path: + * </p> + * <pre>{@code + * UnsynchronizedByteArrayInputStream s = UnsynchronizedByteArrayInputStream.builder() + * .setPath(path) + * .setOffset(0) + * .setLength(byteArray.length) + * .get()} + * </pre> + */ + public static class Builder extends AbstractStreamBuilder<UnsynchronizedByteArrayInputStream, Builder> { + + private int offset; + private int length; + + @Override + public UnsynchronizedByteArrayInputStream get() throws IOException { + return new UnsynchronizedByteArrayInputStream(getOrigin().getByteArray(), offset, length); + } + + @Override + public Builder setByteArray(byte[] origin) { + length = Objects.requireNonNull(origin, "origin").length; + return super.setByteArray(origin); + } + + public Builder setLength(int length) { + if (length < 0) { + throw new IllegalArgumentException("length cannot be negative"); + } + this.length = length; + return this; + } + + public Builder setOffset(int offset) { + if (offset < 0) { + throw new IllegalArgumentException("offset cannot be negative"); + } + this.offset = offset; + return this; + } + + } + /** * The end of stream marker. */ public static final int END_OF_STREAM = -1; + /** + * Constructs a new {@link Builder}. + * + * @return a new {@link Builder}. + */ + public static Builder builder() { + return new Builder(); + } + /** * The underlying data buffer. */ @@ -65,7 +144,9 @@ public class UnsynchronizedByteArrayInputStream extends InputStream { * Creates a new byte array input stream. * * @param data the buffer + * @deprecated Use {@link #builder()}. */ + @Deprecated public UnsynchronizedByteArrayInputStream(final byte[] data) { this.data = Objects.requireNonNull(data, "data"); this.offset = 0; @@ -80,7 +161,9 @@ public class UnsynchronizedByteArrayInputStream extends InputStream { * @param offset the offset into the buffer * * @throws IllegalArgumentException if the offset is less than zero + * @deprecated Use {@link #builder()}. */ + @Deprecated public UnsynchronizedByteArrayInputStream(final byte[] data, final int offset) { Objects.requireNonNull(data, "data"); if (offset < 0) { @@ -92,7 +175,6 @@ public class UnsynchronizedByteArrayInputStream extends InputStream { this.markedOffset = this.offset; } - /** * Creates a new byte array input stream. * @@ -101,7 +183,9 @@ public class UnsynchronizedByteArrayInputStream extends InputStream { * @param length the length of the buffer * * @throws IllegalArgumentException if the offset or length less than zero + * @deprecated Use {@link #builder()}. */ + @Deprecated public UnsynchronizedByteArrayInputStream(final byte[] data, final int offset, final int length) { if (offset < 0) { throw new IllegalArgumentException("offset cannot be negative"); diff --git a/src/main/java/org/apache/commons/io/input/UnsynchronizedFilterInputStream.java b/src/main/java/org/apache/commons/io/input/UnsynchronizedFilterInputStream.java index b76c668e..918c1b19 100644 --- a/src/main/java/org/apache/commons/io/input/UnsynchronizedFilterInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UnsynchronizedFilterInputStream.java @@ -21,6 +21,8 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import org.apache.commons.io.build.AbstractStreamBuilder; + /** * An unsynchronized version of {@link FilterInputStream}, not thread-safe. * <p> @@ -38,18 +40,58 @@ import java.io.InputStream; //@NotThreadSafe public class UnsynchronizedFilterInputStream extends InputStream { + /** + * Builds a new {@link UnsynchronizedFilterInputStream} instance. + * <p> + * Using File IO: + * </p> + * <pre>{@code + * UnsynchronizedFilterInputStream s = UnsynchronizedFilterInputStream.builder() + * .setFile(file) + * .setBufferSize(8192) + * .get()} + * </pre> + * <p> + * Using NIO Path: + * </p> + * <pre>{@code + * UnsynchronizedFilterInputStream s = UnsynchronizedFilterInputStream.builder() + * .setPath(path) + * .setBufferSize(8192) + * .get()} + * </pre> + */ + public static class Builder extends AbstractStreamBuilder<UnsynchronizedFilterInputStream, Builder> { + + @SuppressWarnings("resource") // Caller closes. + @Override + public UnsynchronizedFilterInputStream get() throws IOException { + return new UnsynchronizedFilterInputStream(getOrigin().getInputStream()); + } + + } + + /** + * Constructs a new {@link Builder}. + * + * @return a new {@link Builder}. + */ + public static Builder builder() { + return new Builder(); + } + /** * The source input stream that is filtered. */ - protected volatile InputStream in; + protected volatile InputStream inputStream; /** * Constructs a new {@code FilterInputStream} with the specified input stream as source. * - * @param in the non-null InputStream to filter reads on. + * @param inputStream the non-null InputStream to filter reads on. */ - protected UnsynchronizedFilterInputStream(final InputStream in) { - this.in = in; + protected UnsynchronizedFilterInputStream(final InputStream inputStream) { + this.inputStream = inputStream; } /** @@ -60,7 +102,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public int available() throws IOException { - return in.available(); + return inputStream.available(); } /** @@ -70,7 +112,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public void close() throws IOException { - in.close(); + inputStream.close(); } /** @@ -86,7 +128,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { @SuppressWarnings("sync-override") // by design. @Override public void mark(final int readlimit) { - in.mark(readlimit); + inputStream.mark(readlimit); } /** @@ -100,7 +142,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public boolean markSupported() { - return in.markSupported(); + return inputStream.markSupported(); } /** @@ -112,7 +154,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public int read() throws IOException { - return in.read(); + return inputStream.read(); } /** @@ -141,7 +183,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public int read(final byte[] buffer, final int offset, final int count) throws IOException { - return in.read(buffer, offset, count); + return inputStream.read(buffer, offset, count); } /** @@ -155,7 +197,7 @@ public class UnsynchronizedFilterInputStream extends InputStream { @SuppressWarnings("sync-override") // by design. @Override public void reset() throws IOException { - in.reset(); + inputStream.reset(); } /** @@ -170,6 +212,6 @@ public class UnsynchronizedFilterInputStream extends InputStream { */ @Override public long skip(final long count) throws IOException { - return in.skip(count); + return inputStream.skip(count); } } diff --git a/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java b/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java index c4304c6b..6e47aa39 100644 --- a/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java @@ -70,13 +70,13 @@ public abstract class AbstractByteArrayOutputStream extends OutputStream { /** * Constructs an InputStream subclass. * - * @param buf the buffer + * @param buffer the buffer * @param offset the offset into the buffer * @param length the length of the buffer * * @return the InputStream subclass. */ - T construct(final byte[] buf, final int offset, final int length); + T construct(final byte[] buffer, final int offset, final int length); } static final int DEFAULT_SIZE = 1024; diff --git a/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java b/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java index f48d0435..6f8ee73e 100644 --- a/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.apache.commons.io.function.Uncheck; import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream; /** @@ -119,7 +120,14 @@ public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArray @Override public InputStream toInputStream() { - return toInputStream(UnsynchronizedByteArrayInputStream::new); + // @formatter:off + return toInputStream((buffer, offset, length) -> Uncheck + .get(() -> UnsynchronizedByteArrayInputStream.builder() + .setByteArray(buffer) + .setOffset(offset) + .setLength(length) + .get())); + // @formatter:on } @Override diff --git a/src/test/java/org/apache/commons/io/input/UncheckedFilterInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UncheckedFilterInputStreamTest.java index 2da41b5f..3a77acd0 100644 --- a/src/test/java/org/apache/commons/io/input/UncheckedFilterInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/UncheckedFilterInputStreamTest.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.UncheckedIOException; import org.apache.commons.io.IOUtils; @@ -40,9 +41,9 @@ public class UncheckedFilterInputStreamTest { @SuppressWarnings("resource") @BeforeEach public void beforeEach() { - stringInputStream = UncheckedFilterInputStream.on(new BufferedInputStream(new StringInputStream("01"))); + stringInputStream = UncheckedFilterInputStream.builder().setInputStream(new BufferedInputStream(new StringInputStream("01"))).get(); exception = new IOException("test exception"); - brokenInputStream = UncheckedFilterInputStream.on(new BrokenInputStream(exception)); + brokenInputStream = UncheckedFilterInputStream.builder().setInputStream(new BrokenInputStream(exception)).get(); } @Test @@ -66,7 +67,8 @@ public class UncheckedFilterInputStreamTest { @Test public void testRead() { - try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { + final InputStream inputStream = stringInputStream; + try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.builder().setInputStream(inputStream).get()) { assertEquals('0', uncheckedReader.read()); assertEquals('1', uncheckedReader.read()); assertEquals(IOUtils.EOF, uncheckedReader.read()); @@ -76,7 +78,8 @@ public class UncheckedFilterInputStreamTest { @Test public void testReadByteArray() { - try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { + final InputStream inputStream = stringInputStream; + try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.builder().setInputStream(inputStream).get()) { final byte[] array = new byte[1]; assertEquals(1, uncheckedReader.read(array)); assertEquals('0', array[0]); @@ -93,7 +96,8 @@ public class UncheckedFilterInputStreamTest { @Test public void testReadByteArrayIndexed() { - try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { + final InputStream inputStream = stringInputStream; + try (UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.builder().setInputStream(inputStream).get()) { final byte[] array = new byte[1]; assertEquals(1, uncheckedReader.read(array, 0, 1)); assertEquals('0', array[0]); @@ -125,7 +129,7 @@ public class UncheckedFilterInputStreamTest { @Test public void testResetThrows() { - try (UncheckedFilterInputStream closedReader = UncheckedFilterInputStream.on(ClosedInputStream.INSTANCE)) { + try (UncheckedFilterInputStream closedReader = UncheckedFilterInputStream.builder().setInputStream(ClosedInputStream.INSTANCE).get()) { closedReader.close(); assertThrows(UncheckedIOException.class, () -> brokenInputStream.reset()); } diff --git a/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java b/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java index 1c75795f..45262263 100644 --- a/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java +++ b/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.io.Reader; import java.io.StringReader; import java.io.UncheckedIOException; import java.nio.CharBuffer; @@ -41,9 +42,9 @@ public class UncheckedFilterReaderTest { @SuppressWarnings("resource") @BeforeEach public void beforeEach() { - ucStringReader = UncheckedFilterReader.on(new StringReader("01")); + ucStringReader = UncheckedFilterReader.builder().setReader(new StringReader("01")).get(); exception = new IOException("test exception"); - ucBrokenReader = UncheckedFilterReader.on(new BrokenReader(exception)); + ucBrokenReader = UncheckedFilterReader.builder().setReader(new BrokenReader(exception)).get(); } @Test @@ -67,7 +68,7 @@ public class UncheckedFilterReaderTest { @Test public void testMarkThrows() { - try (UncheckedFilterReader closedReader = UncheckedFilterReader.on(ClosedReader.INSTANCE)) { + try (UncheckedFilterReader closedReader = UncheckedFilterReader.builder().setReader(ClosedReader.INSTANCE).get()) { closedReader.close(); assertThrows(UncheckedIOException.class, () -> closedReader.mark(1)); } @@ -75,7 +76,8 @@ public class UncheckedFilterReaderTest { @Test public void testRead() { - try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) { + final Reader reader = ucStringReader; + try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.builder().setReader(reader).get()) { assertEquals('0', uncheckedReader.read()); assertEquals('1', uncheckedReader.read()); assertEquals(IOUtils.EOF, uncheckedReader.read()); @@ -85,7 +87,8 @@ public class UncheckedFilterReaderTest { @Test public void testReadCharArray() { - try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) { + final Reader reader = ucStringReader; + try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.builder().setReader(reader).get()) { final char[] array = new char[1]; assertEquals(1, uncheckedReader.read(array)); assertEquals('0', array[0]); @@ -102,7 +105,8 @@ public class UncheckedFilterReaderTest { @Test public void testReadCharArrayIndexed() { - try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) { + final Reader reader = ucStringReader; + try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.builder().setReader(reader).get()) { final char[] array = new char[1]; assertEquals(1, uncheckedReader.read(array, 0, 1)); assertEquals('0', array[0]); @@ -129,7 +133,8 @@ public class UncheckedFilterReaderTest { @Test public void testReadCharBuffer() { - try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) { + final Reader reader = ucStringReader; + try (UncheckedFilterReader uncheckedReader = UncheckedFilterReader.builder().setReader(reader).get()) { final CharBuffer buffer = CharBuffer.wrap(new char[1]); assertEquals(1, uncheckedReader.read(buffer)); buffer.flip(); @@ -170,7 +175,7 @@ public class UncheckedFilterReaderTest { @Test public void testResetThrows() { - try (UncheckedFilterReader closedReader = UncheckedFilterReader.on(ClosedReader.INSTANCE)) { + try (UncheckedFilterReader closedReader = UncheckedFilterReader.builder().setReader(ClosedReader.INSTANCE).get()) { closedReader.close(); assertThrows(UncheckedIOException.class, () -> ucBrokenReader.reset()); } diff --git a/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStreamTest.java index 54d676e6..02951f89 100644 --- a/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStreamTest.java @@ -18,7 +18,6 @@ package org.apache.commons.io.input; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -44,20 +43,6 @@ import org.junit.jupiter.api.Test; */ public class UnsynchronizedBufferedInputStreamTest { - static class MyUnsynchronizedBufferedInputStream extends UnsynchronizedBufferedInputStream { - static byte[] buf; - - MyUnsynchronizedBufferedInputStream(final InputStream is) { - super(is); - buf = super.buf; - } - - MyUnsynchronizedBufferedInputStream(final InputStream is, final int size) { - super(is, size); - buf = super.buf; - } - } - private static final int BUFFER_SIZE = 4096; public static final String DATA = StringUtils.repeat("This is a test.", 500); @@ -203,12 +188,7 @@ public class UnsynchronizedBufferedInputStreamTest { // is.read should now throw an exception because it will have to be filled. assertThrows(IOException.class, () -> is.read()); - // regression test for harmony-2407 - new MyUnsynchronizedBufferedInputStream(null); - assertNotNull(MyUnsynchronizedBufferedInputStream.buf); - MyUnsynchronizedBufferedInputStream.buf = null; - new MyUnsynchronizedBufferedInputStream(null, 100); - assertNotNull(MyUnsynchronizedBufferedInputStream.buf); + assertThrows(NullPointerException.class, () -> UnsynchronizedBufferedInputStream.builder().setInputStream(null).setBufferSize(100).get()); } /** diff --git a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java index 6f13b4eb..866c833b 100644 --- a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; @@ -32,19 +33,46 @@ import org.junit.jupiter.api.Test; */ public class UnsynchronizedByteArrayInputStreamTest { + private UnsynchronizedByteArrayInputStream newStream(final byte[] buffer) { + try { + return UnsynchronizedByteArrayInputStream.builder().setByteArray(buffer).get(); + } catch (IOException e) { + fail("Should never happen because no conversion is needed.", e); + return null; + } + } + + private UnsynchronizedByteArrayInputStream newStream(final byte[] buffer, final int offset) { + try { + return UnsynchronizedByteArrayInputStream.builder().setByteArray(buffer).setOffset(offset).get(); + } catch (IOException e) { + fail("Should never happen because no conversion is needed.", e); + return null; + } + } + + private UnsynchronizedByteArrayInputStream newStream(final byte[] buffer, final int offset, final int length) { + try { + return UnsynchronizedByteArrayInputStream.builder().setByteArray(buffer).setOffset(offset).setLength(length).get(); + } catch (IOException e) { + fail("Should never happen because no conversion is needed.", e); + return null; + } + } + @Test public void testConstructor1() throws IOException { final byte[] empty = IOUtils.EMPTY_BYTE_ARRAY; final byte[] one = new byte[1]; final byte[] some = new byte[25]; - try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty)) { + try (UnsynchronizedByteArrayInputStream is = newStream(empty)) { assertEquals(empty.length, is.available()); } - try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(one)) { + try (UnsynchronizedByteArrayInputStream is = newStream(one)) { assertEquals(one.length, is.available()); } - try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(some)) { + try (UnsynchronizedByteArrayInputStream is = newStream(some)) { assertEquals(some.length, is.available()); } } @@ -56,25 +84,25 @@ public class UnsynchronizedByteArrayInputStreamTest { final byte[] one = new byte[1]; final byte[] some = new byte[25]; - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty, 0); + UnsynchronizedByteArrayInputStream is = newStream(empty, 0); assertEquals(empty.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(empty, 1); + is = newStream(empty, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 0); + is = newStream(one, 0); assertEquals(one.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 1); + is = newStream(one, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 2); + is = newStream(one, 2); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 0); + is = newStream(some, 0); assertEquals(some.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 1); + is = newStream(some, 1); assertEquals(some.length - 1, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 10); + is = newStream(some, 10); assertEquals(some.length - 10, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, some.length); + is = newStream(some, some.length); assertEquals(0, is.available()); } @@ -85,72 +113,72 @@ public class UnsynchronizedByteArrayInputStreamTest { final byte[] one = new byte[1]; final byte[] some = new byte[25]; - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty, 0); + UnsynchronizedByteArrayInputStream is = newStream(empty, 0); assertEquals(empty.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(empty, 1); + is = newStream(empty, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(empty, 0,1); + is = newStream(empty, 0, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(empty, 1,1); + is = newStream(empty, 1, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 0); + is = newStream(one, 0); assertEquals(one.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 1); + is = newStream(one, 1); assertEquals(one.length - 1, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 2); + is = newStream(one, 2); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 0, 1); + is = newStream(one, 0, 1); assertEquals(1, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 1, 1); + is = newStream(one, 1, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 0, 2); + is = newStream(one, 0, 2); assertEquals(1, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 2, 1); + is = newStream(one, 2, 1); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(one, 2, 2); + is = newStream(one, 2, 2); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 0); + is = newStream(some, 0); assertEquals(some.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 1); + is = newStream(some, 1); assertEquals(some.length - 1, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 10); + is = newStream(some, 10); assertEquals(some.length - 10, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, some.length); + is = newStream(some, some.length); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, some.length, some.length); + is = newStream(some, some.length, some.length); assertEquals(0, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, some.length - 1, some.length); + is = newStream(some, some.length - 1, some.length); assertEquals(1, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 0, 7); + is = newStream(some, 0, 7); assertEquals(7, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 7, 7); + is = newStream(some, 7, 7); assertEquals(7, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, 0, some.length * 2); + is = newStream(some, 0, some.length * 2); assertEquals(some.length, is.available()); - is = new UnsynchronizedByteArrayInputStream(some, some.length - 1, 7); + is = newStream(some, some.length - 1, 7); assertEquals(1, is.available()); } @Test public void testInvalidConstructor2OffsetUnder() { assertThrows(IllegalArgumentException.class, () -> { - new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, -1); + newStream(IOUtils.EMPTY_BYTE_ARRAY, -1); }); } @Test public void testInvalidConstructor3LengthUnder() { assertThrows(IllegalArgumentException.class, () -> { - new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, 0, -1); + newStream(IOUtils.EMPTY_BYTE_ARRAY, 0, -1); }); } @Test public void testInvalidConstructor3OffsetUnder() { assertThrows(IllegalArgumentException.class, () -> { - new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, -1, 1); + newStream(IOUtils.EMPTY_BYTE_ARRAY, -1, 1); }); } @@ -158,7 +186,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @SuppressWarnings("resource") // not necessary to close these resources public void testInvalidReadArrayExplicitLenUnder() { final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY; - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertThrows(IndexOutOfBoundsException.class, () -> { is.read(buf, 0, -1); }); @@ -168,7 +196,7 @@ public class UnsynchronizedByteArrayInputStreamTest { public void testInvalidReadArrayExplicitOffsetUnder() { final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY; @SuppressWarnings("resource") // not necessary to close these resources - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertThrows(IndexOutOfBoundsException.class, () -> { is.read(buf, -1, 1); }); @@ -178,7 +206,7 @@ public class UnsynchronizedByteArrayInputStreamTest { public void testInvalidReadArrayExplicitRangeOver() { final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY; @SuppressWarnings("resource") // not necessary to close these resources - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertThrows(IndexOutOfBoundsException.class, () -> { is.read(buf, 0, 1); }); @@ -188,7 +216,7 @@ public class UnsynchronizedByteArrayInputStreamTest { public void testInvalidReadArrayNull() { final byte[] buf = null; @SuppressWarnings("resource") // not necessary to close these resources - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertThrows(NullPointerException.class, () -> { is.read(buf); }); @@ -197,7 +225,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testInvalidSkipNUnder() { @SuppressWarnings("resource") // not necessary to close these resources - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertThrows(IllegalArgumentException.class, () -> { is.skip(-1); }); @@ -206,7 +234,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testMarkReset() { @SuppressWarnings("resource") // not necessary to close these resources - final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + final UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertTrue(is.markSupported()); assertEquals(0xa, is.read()); assertTrue(is.markSupported()); @@ -232,19 +260,18 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadArray() { byte[] buf = new byte[10]; - @SuppressWarnings("resource") // not necessary to close these resources - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY); + UnsynchronizedByteArrayInputStream is = newStream(IOUtils.EMPTY_BYTE_ARRAY); int read = is.read(buf); assertEquals(END_OF_STREAM, read); assertArrayEquals(new byte[10], buf); buf = IOUtils.EMPTY_BYTE_ARRAY; - is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); read = is.read(buf); assertEquals(0, read); buf = new byte[10]; - is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); read = is.read(buf); assertEquals(3, read); assertEquals(0xa, buf[0]); @@ -253,7 +280,7 @@ public class UnsynchronizedByteArrayInputStreamTest { assertEquals(0, buf[3]); buf = new byte[2]; - is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); read = is.read(buf); assertEquals(2, read); assertEquals(0xa, buf[0]); @@ -266,31 +293,30 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadArrayExplicit() { byte[] buf = new byte[10]; - @SuppressWarnings("resource") // not necessary to close these resources - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY); + UnsynchronizedByteArrayInputStream is = newStream(IOUtils.EMPTY_BYTE_ARRAY); int read = is.read(buf, 0, 10); assertEquals(END_OF_STREAM, read); assertArrayEquals(new byte[10], buf); buf = new byte[10]; - is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY); + is = newStream(IOUtils.EMPTY_BYTE_ARRAY); read = is.read(buf, 4, 2); assertEquals(END_OF_STREAM, read); assertArrayEquals(new byte[10], buf); buf = new byte[10]; - is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY); + is = newStream(IOUtils.EMPTY_BYTE_ARRAY); read = is.read(buf, 4, 6); assertEquals(END_OF_STREAM, read); assertArrayEquals(new byte[10], buf); buf = IOUtils.EMPTY_BYTE_ARRAY; - is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc}); - read = is.read(buf, 0,0); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); + read = is.read(buf, 0, 0); assertEquals(0, read); buf = new byte[10]; - is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); read = is.read(buf, 0, 2); assertEquals(2, read); assertEquals(0xa, buf[0]); @@ -303,11 +329,10 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadSingle() { - @SuppressWarnings("resource") // not necessary to close these resources - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY); + UnsynchronizedByteArrayInputStream is = newStream(IOUtils.EMPTY_BYTE_ARRAY); assertEquals(END_OF_STREAM, is.read()); - is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(0xa, is.read()); assertEquals(0xb, is.read()); assertEquals(0xc, is.read()); @@ -316,8 +341,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testSkip() { - @SuppressWarnings("resource") // not necessary to close these resources - UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + UnsynchronizedByteArrayInputStream is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(3, is.available()); is.skip(1); @@ -328,30 +352,26 @@ public class UnsynchronizedByteArrayInputStreamTest { assertEquals(0, is.available()); assertEquals(END_OF_STREAM, is.read()); - - is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(3, is.available()); is.skip(0); assertEquals(3, is.available()); assertEquals(0xa, is.read()); - - is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(3, is.available()); is.skip(2); assertEquals(1, is.available()); assertEquals(0xc, is.read()); assertEquals(END_OF_STREAM, is.read()); - - is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(3, is.available()); is.skip(3); assertEquals(0, is.available()); assertEquals(END_OF_STREAM, is.read()); - - is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); + is = newStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc }); assertEquals(3, is.available()); is.skip(999); assertEquals(0, is.available()); diff --git a/src/test/java/org/apache/commons/io/input/UnsynchronizedFilterInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnsynchronizedFilterInputStreamTest.java index eb9a9786..818e3eef 100644 --- a/src/test/java/org/apache/commons/io/input/UnsynchronizedFilterInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedFilterInputStreamTest.java @@ -39,12 +39,6 @@ import org.junit.jupiter.api.Test; */ public class UnsynchronizedFilterInputStreamTest { - static class MyUnsynchronizedFilterInputStream extends UnsynchronizedFilterInputStream { - public MyUnsynchronizedFilterInputStream(final InputStream is) { - super(is); - } - } - public static final String DATA = StringUtils.repeat("This is a test.", 500); private Path fileName; @@ -63,7 +57,7 @@ public class UnsynchronizedFilterInputStreamTest { protected void setUp() throws IOException { fileName = Files.createTempFile(getClass().getSimpleName(), ".tst"); Files.write(fileName, DATA.getBytes("UTF-8")); - is = new MyUnsynchronizedFilterInputStream(Files.newInputStream(fileName)); + is = new UnsynchronizedFilterInputStream(Files.newInputStream(fileName)); } /**