Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
- Pull up closed state
- Move checkOpen() for cleaner reuse
  • Loading branch information
garydgregory committed Sep 14, 2024
1 parent 6d6f65d commit abc2c1b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.io;

import java.io.BufferedInputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@
*/
public abstract class AbstractInputStream extends InputStream {

/**
* Throws an IOException on false input.
*
* @param isOpen whether the stream is open or not.
* @throws IOException if this instance is closed.
*/
static void checkOpen(final boolean isOpen) throws IOException {
if (!isOpen) {
throw new IOException("The stream is closed.");
}
}

/**
* Whether {@link #close()} completed successfully.
*/
Expand All @@ -50,7 +38,7 @@ static void checkOpen(final boolean isOpen) throws IOException {
* @throws IOException if this instance is closed.
*/
void checkOpen() throws IOException {
checkOpen(!isClosed());
Input.checkOpen(!isClosed());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public synchronized int read(final byte[] b, final int offset, int len) throws I
* @throws IOException if an I/O error occurs.
*/
private boolean refill() throws IOException {
AbstractInputStream.checkOpen(fileChannel.isOpen());
Input.checkOpen(fileChannel.isOpen());
if (!byteBuffer.hasRemaining()) {
byteBuffer.clear();
int nRead = 0;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/apache/commons/io/input/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

/**
* Package-wide internals for input.
*/
class Input {

/**
* Throws an IOException on false input.
*
* @param isOpen whether an input is open or not.
* @throws IOException if {@code isOpen} is false indicating an input is closed.
*/
static void checkOpen(final boolean isOpen) throws IOException {
if (!isOpen) {
throw new IOException("Closed");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void beforeRead(final int n) throws IOException {
* @throws IOException if this instance is closed.
*/
void checkOpen() throws IOException {
AbstractInputStream.checkOpen(!isClosed());
Input.checkOpen(!isClosed());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public class UnsynchronizedBufferedReader extends UnsynchronizedReader {
* @param in the Reader that is buffered.
*/
public UnsynchronizedBufferedReader(final Reader in) {
this.in = in;
buf = new char[IOUtils.DEFAULT_BUFFER_SIZE];
this(in, IOUtils.DEFAULT_BUFFER_SIZE);
}

/**
Expand All @@ -110,12 +109,6 @@ public UnsynchronizedBufferedReader(final Reader in, final int size) {
buf = new char[size];
}

private void checkClosed() throws IOException {
if (isClosed()) {
throw new IOException("Closed");
}
}

/**
* Peeks at the next input character, refilling the buffer if necessary. If this character is a newline character ("\n"), it is discarded.
*/
Expand All @@ -136,6 +129,7 @@ public void close() throws IOException {
if (!isClosed()) {
in.close();
buf = null;
super.close();
}
}

Expand Down Expand Up @@ -183,15 +177,6 @@ private int fillBuf() throws IOException {
return count;
}

/**
* Tests whether or not this reader is closed.
*
* @return {@code true} if this reader is closed, {@code false} otherwise.
*/
private boolean isClosed() {
return buf == null;
}

/**
* Sets a mark position in this reader. The parameter {@code markLimit} indicates how many characters can be read before the mark is invalidated. Calling
* {@link #reset()} will reposition the reader back to the marked position if {@code markLimit} has not been surpassed.
Expand All @@ -207,7 +192,7 @@ public void mark(final int markLimit) throws IOException {
if (markLimit < 0) {
throw new IllegalArgumentException();
}
checkClosed();
checkOpen();
this.markLimit = markLimit;
mark = pos;
}
Expand All @@ -234,7 +219,7 @@ public boolean markSupported() {
*/
@Override
public int read() throws IOException {
checkClosed();
checkOpen();
/* Are there buffered characters available? */
if (pos < end || fillBuf() != EOF) {
return buf[pos++];
Expand All @@ -257,7 +242,7 @@ public int read() throws IOException {
*/
@Override
public int read(final char[] buffer, int offset, final int length) throws IOException {
checkClosed();
checkOpen();
if (offset < 0 || offset > buffer.length - length || length < 0) {
throw new IndexOutOfBoundsException();
}
Expand Down Expand Up @@ -317,7 +302,7 @@ public int read(final char[] buffer, int offset, final int length) throws IOExce
* @throws IOException if this reader is closed or some other I/O error occurs.
*/
public String readLine() throws IOException {
checkClosed();
checkOpen();
/* has the underlying stream been exhausted? */
if (pos == end && fillBuf() == EOF) {
return null;
Expand All @@ -331,7 +316,8 @@ public String readLine() throws IOException {
final String res = new String(buf, pos, charPos - pos);
pos = charPos + 1;
return res;
} else if (ch == CR) {
}
if (ch == CR) {
final String res = new String(buf, pos, charPos - pos);
pos = charPos + 1;
if ((pos < end || fillBuf() != EOF) && buf[pos] == LF) {
Expand Down Expand Up @@ -360,11 +346,7 @@ public String readLine() throws IOException {
}
for (int charPos = pos; charPos < end; charPos++) {
final char c = buf[charPos];
if (eol == NUL) {
if (c == LF || c == CR) {
eol = c;
}
} else {
if (eol != NUL) {
if (eol == CR && c == LF) {
if (charPos > pos) {
result.append(buf, pos, charPos - pos - 1);
Expand All @@ -378,6 +360,9 @@ public String readLine() throws IOException {
}
return result.toString();
}
if (c == LF || c == CR) {
eol = c;
}
}
if (eol == NUL) {
result.append(buf, pos, end - pos);
Expand All @@ -398,7 +383,7 @@ public String readLine() throws IOException {
*/
@Override
public boolean ready() throws IOException {
checkClosed();
checkOpen();
return end - pos > 0 || in.ready();
}

Expand All @@ -411,7 +396,7 @@ public boolean ready() throws IOException {
*/
@Override
public void reset() throws IOException {
checkClosed();
checkOpen();
if (mark == -1) {
throw new IOException("mark == -1");
}
Expand All @@ -435,7 +420,7 @@ public long skip(final long amount) throws IOException {
if (amount < 0) {
throw new IllegalArgumentException();
}
checkClosed();
checkOpen();
if (amount < 1) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,48 @@ public abstract class UnsynchronizedReader extends Reader {
*/
private static final int MAX_SKIP_BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;

/**
* Whether {@link #close()} completed successfully.
*/
private boolean closed;

/**
* The skip buffer, defaults to null until allocated in {@link UnsynchronizedReader#skip(long)}.
*/
private char skipBuffer[];

/**
* Checks if this instance is closed and throws an IOException if so.
*
* @throws IOException if this instance is closed.
*/
void checkOpen() throws IOException {
Input.checkOpen(!isClosed());
}

@Override
public void close() throws IOException {
closed = true;
}

/**
* Tests whether this instance is closed; if {@link #close()} completed successfully.
*
* @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;
}

/**
* Skips characters by reading from this instance.
*
Expand Down

0 comments on commit abc2c1b

Please sign in to comment.