You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/07/09 02:01:50 UTC
(commons-io) branch master updated: Add AbstractInputStream and refactor duplicate code
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 f03363e24 Add AbstractInputStream and refactor duplicate code
f03363e24 is described below
commit f03363e242e1544f530f42b41ff06207c3770cdf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 22:01:42 2024 -0400
Add AbstractInputStream and refactor duplicate code
---
src/changes/changes.xml | 1 +
.../commons/io/input/AbstractInputStream.java | 59 ++++++++++++++++++++++
.../commons/io/input/CircularInputStream.java | 9 ++--
.../io/input/MemoryMappedFileInputStream.java | 11 ++--
.../apache/commons/io/input/NullInputStream.java | 23 ++++-----
.../io/input/RandomAccessFileInputStream.java | 9 ++--
6 files changed, 83 insertions(+), 29 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5d1436d51..ac6667bea 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOSupplier.getUnchecked().</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add CloseShieldInputStream.systemIn(InputStream).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add NullInputStream.init().</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractInputStream and refactor duplicate code.</action>
<!-- FIX -->
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add missing unit tests.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">FileUtils.lastModifiedFileTime(File) calls Objects.requireNonNull() on the wrong object.</action>
diff --git a/src/main/java/org/apache/commons/io/input/AbstractInputStream.java b/src/main/java/org/apache/commons/io/input/AbstractInputStream.java
new file mode 100644
index 000000000..5c71facd1
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/input/AbstractInputStream.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.input;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Abstracts some InputStream operations for implementations in this package.
+ *
+ * @since 2.17.0
+ */
+public abstract class AbstractInputStream extends InputStream {
+
+ /**
+ * Whether {@link #close()} completed successfully.
+ */
+ private boolean closed;
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ closed = true;
+ }
+
+ /**
+ * Tests whether this instance is closed.
+ *
+ * @return whether this instance is closed.
+ */
+ public boolean isClosed() {
+ return closed;
+ }
+
+ /**
+ * Sets whether this instance is closed.
+ *
+ * @param closed whether this instance is closed.
+ */
+ public void setClosed(final boolean closed) {
+ this.closed = closed;
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/io/input/CircularInputStream.java b/src/main/java/org/apache/commons/io/input/CircularInputStream.java
index d508675b1..960f7e45a 100644
--- a/src/main/java/org/apache/commons/io/input/CircularInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/CircularInputStream.java
@@ -32,7 +32,7 @@ import org.apache.commons.io.IOUtils;
* @see InfiniteCircularInputStream
* @since 2.8.0
*/
-public class CircularInputStream extends InputStream {
+public class CircularInputStream extends AbstractInputStream {
/**
* Throws an {@link IllegalArgumentException} if the input contains -1.
@@ -54,7 +54,6 @@ public class CircularInputStream extends InputStream {
private int position = IOUtils.EOF;
private final byte[] repeatedContent;
private final long targetByteCount;
- private boolean closed;
/**
* Constructs an instance from the specified array of bytes.
@@ -73,18 +72,18 @@ public class CircularInputStream extends InputStream {
@Override
public int available() throws IOException {
// A negative targetByteCount means an infinite target count.
- return closed ? 0 : targetByteCount <= Integer.MAX_VALUE ? Math.max(Integer.MAX_VALUE, (int) targetByteCount) : Integer.MAX_VALUE;
+ return isClosed() ? 0 : targetByteCount <= Integer.MAX_VALUE ? Math.max(Integer.MAX_VALUE, (int) targetByteCount) : Integer.MAX_VALUE;
}
@Override
public void close() throws IOException {
- closed = true;
+ super.close();
byteCount = targetByteCount;
}
@Override
public int read() {
- if (targetByteCount >= 0 || closed) {
+ if (targetByteCount >= 0 || isClosed()) {
if (byteCount == targetByteCount) {
return IOUtils.EOF;
}
diff --git a/src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java b/src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
index 248f94510..02c02a9dc 100644
--- a/src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
@@ -69,7 +69,7 @@ import org.apache.commons.io.build.AbstractStreamBuilder;
* @see Builder
* @since 2.12.0
*/
-public final class MemoryMappedFileInputStream extends InputStream {
+public final class MemoryMappedFileInputStream extends AbstractInputStream {
// @formatter:off
/**
@@ -146,7 +146,6 @@ public final class MemoryMappedFileInputStream extends InputStream {
private final int bufferSize;
private final FileChannel channel;
private ByteBuffer buffer = EMPTY_BUFFER;
- private boolean closed;
/**
* The starting position (within the file) of the next sliding buffer.
@@ -172,7 +171,7 @@ public final class MemoryMappedFileInputStream extends InputStream {
}
private void checkOpen() throws IOException {
- if (closed) {
+ if (isClosed()) {
throw new IOException("Stream closed");
}
}
@@ -185,11 +184,11 @@ public final class MemoryMappedFileInputStream extends InputStream {
@Override
public void close() throws IOException {
- if (!closed) {
+ if (!isClosed()) {
cleanBuffer();
buffer = EMPTY_BUFFER;
channel.close();
- closed = true;
+ super.close();
}
}
@@ -211,7 +210,7 @@ public final class MemoryMappedFileInputStream extends InputStream {
@Override
public int read() throws IOException {
- if (closed) {
+ if (isClosed()) {
return EOF;
}
if (!buffer.hasRemaining()) {
diff --git a/src/main/java/org/apache/commons/io/input/NullInputStream.java b/src/main/java/org/apache/commons/io/input/NullInputStream.java
index 91abdc5bb..3bb495e77 100644
--- a/src/main/java/org/apache/commons/io/input/NullInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/NullInputStream.java
@@ -54,7 +54,7 @@ import java.io.InputStream;
*
* @since 1.3
*/
-public class NullInputStream extends InputStream {
+public class NullInputStream extends AbstractInputStream {
/**
* The singleton instance.
@@ -73,7 +73,6 @@ public class NullInputStream extends InputStream {
private long position;
private long mark = -1;
private long readLimit;
- private boolean closed;
private final boolean throwEofException;
private final boolean markSupported;
@@ -110,7 +109,7 @@ public class NullInputStream extends InputStream {
@Override
public int available() {
- if (closed) {
+ if (isClosed()) {
return 0;
}
final long avail = size - position;
@@ -142,7 +141,7 @@ public class NullInputStream extends InputStream {
*/
@Override
public void close() throws IOException {
- closed = true;
+ super.close();
mark = -1;
}
@@ -168,10 +167,10 @@ public class NullInputStream extends InputStream {
* Handles End of File.
*
* @return {@code -1} if {@code throwEofException} is set to {@code false}
- * @throws EOFException if {@code throwEofException} is set to {@code true}.
+ * @throws IOException if {@code throwEofException} is set to {@code true}.
*/
- private int handleEof() throws EOFException {
- closed = true;
+ private int handleEof() throws IOException {
+ super.close();
checkThrowEof("handleEof()");
return EOF;
}
@@ -183,7 +182,7 @@ public class NullInputStream extends InputStream {
* @since 2.17.0
*/
public NullInputStream init() {
- closed = false;
+ setClosed(false);
position = 0;
mark = -1;
readLimit = 0;
@@ -251,7 +250,7 @@ public class NullInputStream extends InputStream {
*/
@Override
public int read() throws IOException {
- if (closed) {
+ if (isClosed()) {
checkThrowEof("read()");
return EOF;
}
@@ -287,7 +286,7 @@ public class NullInputStream extends InputStream {
*/
@Override
public int read(final byte[] bytes, final int offset, final int length) throws IOException {
- if (closed) {
+ if (isClosed()) {
checkThrowEof("read(byte[], int, int)");
return EOF;
}
@@ -322,7 +321,7 @@ public class NullInputStream extends InputStream {
throw new IOException("Marked position [" + mark + "] is no longer valid - passed the read limit [" + readLimit + "]");
}
position = mark;
- closed = false;
+ setClosed(false);
}
/**
@@ -335,7 +334,7 @@ public class NullInputStream extends InputStream {
*/
@Override
public long skip(final long numberOfBytes) throws IOException {
- if (closed) {
+ if (isClosed()) {
checkThrowEof("skip(long)");
return EOF;
}
diff --git a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
index 2f6b4390a..3bb10c93d 100644
--- a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
@@ -19,7 +19,6 @@ package org.apache.commons.io.input;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Objects;
@@ -37,7 +36,7 @@ import org.apache.commons.io.build.AbstractStreamBuilder;
* @see Builder
* @since 2.8.0
*/
-public class RandomAccessFileInputStream extends InputStream {
+public class RandomAccessFileInputStream extends AbstractInputStream {
// @formatter:off
/**
@@ -129,7 +128,6 @@ public class RandomAccessFileInputStream extends InputStream {
return new Builder();
}
- private boolean closed;
private final boolean propagateClose;
private final RandomAccessFile randomAccessFile;
@@ -181,7 +179,7 @@ public class RandomAccessFileInputStream extends InputStream {
* @throws IOException If an I/O error occurs.
*/
public long availableLong() throws IOException {
- return closed ? 0 : randomAccessFile.length() - randomAccessFile.getFilePointer();
+ return isClosed() ? 0 : randomAccessFile.length() - randomAccessFile.getFilePointer();
}
@Override
@@ -190,7 +188,6 @@ public class RandomAccessFileInputStream extends InputStream {
if (propagateClose) {
randomAccessFile.close();
}
- closed = true;
}
/**
@@ -213,7 +210,7 @@ public class RandomAccessFileInputStream extends InputStream {
@Override
public int read() throws IOException {
- return closed ? IOUtils.EOF : randomAccessFile.read();
+ return isClosed() ? IOUtils.EOF : randomAccessFile.read();
}
@Override