Skip to content

Commit abc2c1b

Browse files
committed
Refactoring
- Pull up closed state - Move checkOpen() for cleaner reuse
1 parent 6d6f65d commit abc2c1b

7 files changed

Lines changed: 95 additions & 45 deletions

File tree

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.commons.io;
1819

1920
import java.io.BufferedInputStream;

src/main/java/org/apache/commons/io/input/AbstractInputStream.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@
2727
*/
2828
public abstract class AbstractInputStream extends InputStream {
2929

30-
/**
31-
* Throws an IOException on false input.
32-
*
33-
* @param isOpen whether the stream is open or not.
34-
* @throws IOException if this instance is closed.
35-
*/
36-
static void checkOpen(final boolean isOpen) throws IOException {
37-
if (!isOpen) {
38-
throw new IOException("The stream is closed.");
39-
}
40-
}
41-
4230
/**
4331
* Whether {@link #close()} completed successfully.
4432
*/
@@ -50,7 +38,7 @@ static void checkOpen(final boolean isOpen) throws IOException {
5038
* @throws IOException if this instance is closed.
5139
*/
5240
void checkOpen() throws IOException {
53-
checkOpen(!isClosed());
41+
Input.checkOpen(!isClosed());
5442
}
5543

5644
@Override

src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public synchronized int read(final byte[] b, final int offset, int len) throws I
243243
* @throws IOException if an I/O error occurs.
244244
*/
245245
private boolean refill() throws IOException {
246-
AbstractInputStream.checkOpen(fileChannel.isOpen());
246+
Input.checkOpen(fileChannel.isOpen());
247247
if (!byteBuffer.hasRemaining()) {
248248
byteBuffer.clear();
249249
int nRead = 0;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.input;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* Package-wide internals for input.
24+
*/
25+
class Input {
26+
27+
/**
28+
* Throws an IOException on false input.
29+
*
30+
* @param isOpen whether an input is open or not.
31+
* @throws IOException if {@code isOpen} is false indicating an input is closed.
32+
*/
33+
static void checkOpen(final boolean isOpen) throws IOException {
34+
if (!isOpen) {
35+
throw new IOException("Closed");
36+
}
37+
}
38+
39+
}

src/main/java/org/apache/commons/io/input/ProxyInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected void beforeRead(final int n) throws IOException {
149149
* @throws IOException if this instance is closed.
150150
*/
151151
void checkOpen() throws IOException {
152-
AbstractInputStream.checkOpen(!isClosed());
152+
Input.checkOpen(!isClosed());
153153
}
154154

155155
/**

src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedReader.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public class UnsynchronizedBufferedReader extends UnsynchronizedReader {
9191
* @param in the Reader that is buffered.
9292
*/
9393
public UnsynchronizedBufferedReader(final Reader in) {
94-
this.in = in;
95-
buf = new char[IOUtils.DEFAULT_BUFFER_SIZE];
94+
this(in, IOUtils.DEFAULT_BUFFER_SIZE);
9695
}
9796

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

113-
private void checkClosed() throws IOException {
114-
if (isClosed()) {
115-
throw new IOException("Closed");
116-
}
117-
}
118-
119112
/**
120113
* Peeks at the next input character, refilling the buffer if necessary. If this character is a newline character ("\n"), it is discarded.
121114
*/
@@ -136,6 +129,7 @@ public void close() throws IOException {
136129
if (!isClosed()) {
137130
in.close();
138131
buf = null;
132+
super.close();
139133
}
140134
}
141135

@@ -183,15 +177,6 @@ private int fillBuf() throws IOException {
183177
return count;
184178
}
185179

186-
/**
187-
* Tests whether or not this reader is closed.
188-
*
189-
* @return {@code true} if this reader is closed, {@code false} otherwise.
190-
*/
191-
private boolean isClosed() {
192-
return buf == null;
193-
}
194-
195180
/**
196181
* Sets a mark position in this reader. The parameter {@code markLimit} indicates how many characters can be read before the mark is invalidated. Calling
197182
* {@link #reset()} will reposition the reader back to the marked position if {@code markLimit} has not been surpassed.
@@ -207,7 +192,7 @@ public void mark(final int markLimit) throws IOException {
207192
if (markLimit < 0) {
208193
throw new IllegalArgumentException();
209194
}
210-
checkClosed();
195+
checkOpen();
211196
this.markLimit = markLimit;
212197
mark = pos;
213198
}
@@ -234,7 +219,7 @@ public boolean markSupported() {
234219
*/
235220
@Override
236221
public int read() throws IOException {
237-
checkClosed();
222+
checkOpen();
238223
/* Are there buffered characters available? */
239224
if (pos < end || fillBuf() != EOF) {
240225
return buf[pos++];
@@ -257,7 +242,7 @@ public int read() throws IOException {
257242
*/
258243
@Override
259244
public int read(final char[] buffer, int offset, final int length) throws IOException {
260-
checkClosed();
245+
checkOpen();
261246
if (offset < 0 || offset > buffer.length - length || length < 0) {
262247
throw new IndexOutOfBoundsException();
263248
}
@@ -317,7 +302,7 @@ public int read(final char[] buffer, int offset, final int length) throws IOExce
317302
* @throws IOException if this reader is closed or some other I/O error occurs.
318303
*/
319304
public String readLine() throws IOException {
320-
checkClosed();
305+
checkOpen();
321306
/* has the underlying stream been exhausted? */
322307
if (pos == end && fillBuf() == EOF) {
323308
return null;
@@ -331,7 +316,8 @@ public String readLine() throws IOException {
331316
final String res = new String(buf, pos, charPos - pos);
332317
pos = charPos + 1;
333318
return res;
334-
} else if (ch == CR) {
319+
}
320+
if (ch == CR) {
335321
final String res = new String(buf, pos, charPos - pos);
336322
pos = charPos + 1;
337323
if ((pos < end || fillBuf() != EOF) && buf[pos] == LF) {
@@ -360,11 +346,7 @@ public String readLine() throws IOException {
360346
}
361347
for (int charPos = pos; charPos < end; charPos++) {
362348
final char c = buf[charPos];
363-
if (eol == NUL) {
364-
if (c == LF || c == CR) {
365-
eol = c;
366-
}
367-
} else {
349+
if (eol != NUL) {
368350
if (eol == CR && c == LF) {
369351
if (charPos > pos) {
370352
result.append(buf, pos, charPos - pos - 1);
@@ -378,6 +360,9 @@ public String readLine() throws IOException {
378360
}
379361
return result.toString();
380362
}
363+
if (c == LF || c == CR) {
364+
eol = c;
365+
}
381366
}
382367
if (eol == NUL) {
383368
result.append(buf, pos, end - pos);
@@ -398,7 +383,7 @@ public String readLine() throws IOException {
398383
*/
399384
@Override
400385
public boolean ready() throws IOException {
401-
checkClosed();
386+
checkOpen();
402387
return end - pos > 0 || in.ready();
403388
}
404389

@@ -411,7 +396,7 @@ public boolean ready() throws IOException {
411396
*/
412397
@Override
413398
public void reset() throws IOException {
414-
checkClosed();
399+
checkOpen();
415400
if (mark == -1) {
416401
throw new IOException("mark == -1");
417402
}
@@ -435,7 +420,7 @@ public long skip(final long amount) throws IOException {
435420
if (amount < 0) {
436421
throw new IllegalArgumentException();
437422
}
438-
checkClosed();
423+
checkOpen();
439424
if (amount < 1) {
440425
return 0;
441426
}

src/main/java/org/apache/commons/io/input/UnsynchronizedReader.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,48 @@ public abstract class UnsynchronizedReader extends Reader {
3636
*/
3737
private static final int MAX_SKIP_BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
3838

39+
/**
40+
* Whether {@link #close()} completed successfully.
41+
*/
42+
private boolean closed;
43+
3944
/**
4045
* The skip buffer, defaults to null until allocated in {@link UnsynchronizedReader#skip(long)}.
4146
*/
4247
private char skipBuffer[];
4348

49+
/**
50+
* Checks if this instance is closed and throws an IOException if so.
51+
*
52+
* @throws IOException if this instance is closed.
53+
*/
54+
void checkOpen() throws IOException {
55+
Input.checkOpen(!isClosed());
56+
}
57+
58+
@Override
59+
public void close() throws IOException {
60+
closed = true;
61+
}
62+
63+
/**
64+
* Tests whether this instance is closed; if {@link #close()} completed successfully.
65+
*
66+
* @return whether this instance is closed.
67+
*/
68+
public boolean isClosed() {
69+
return closed;
70+
}
71+
72+
/**
73+
* Sets whether this instance is closed.
74+
*
75+
* @param closed whether this instance is closed.
76+
*/
77+
public void setClosed(final boolean closed) {
78+
this.closed = closed;
79+
}
80+
4481
/**
4582
* Skips characters by reading from this instance.
4683
*

0 commit comments

Comments
 (0)