Skip to content

Commit 4b92e65

Browse files
author
Gary Gregory
committed
Refactor common code, Javadoc, reuse constants, use 120 max line length.
Javadoc: Sentences end in a period.
1 parent a8978e8 commit 4b92e65

File tree

3 files changed

+50
-57
lines changed

3 files changed

+50
-57
lines changed

src/main/java/org/apache/commons/io/filefilter/IOFileFilter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public interface IOFileFilter extends FileFilter, FilenameFilter, PathFilter {
4343
* Defined in {@link java.io.FileFilter}.
4444
* </p>
4545
*
46-
* @param file the File to check
47-
* @return true if this file matches the test
46+
* @param file the File to check.
47+
* @return true if this file matches the test.
4848
*/
4949
@Override
5050
boolean accept(File file);
@@ -55,18 +55,18 @@ public interface IOFileFilter extends FileFilter, FilenameFilter, PathFilter {
5555
* Defined in {@link java.io.FilenameFilter}.
5656
* </p>
5757
*
58-
* @param dir the directory File to check
59-
* @param name the file name within the directory to check
60-
* @return true if this file matches the test
58+
* @param dir the directory File to check.
59+
* @param name the file name within the directory to check.
60+
* @return true if this file matches the test.
6161
*/
6262
@Override
6363
boolean accept(File dir, String name);
6464

6565
/**
6666
* Checks to see if the Path should be accepted by this filter.
6767
*
68-
* @param path the Path to check
69-
* @return true if this path matches the test
68+
* @param path the Path to check.
69+
* @return true if this path matches the test.
7070
* @since 2.9.0
7171
*/
7272
@Override
@@ -78,7 +78,7 @@ default FileVisitResult accept(final Path path, final BasicFileAttributes attrib
7878
* Creates a new "and" filter with this filter.
7979
*
8080
* @param fileFilter the filter to "and".
81-
* @return a new filter
81+
* @return a new filter.
8282
* @since 2.9.0
8383
*/
8484
default IOFileFilter and(final IOFileFilter fileFilter) {
@@ -88,7 +88,7 @@ default IOFileFilter and(final IOFileFilter fileFilter) {
8888
/**
8989
* Creates a new "not" filter with this filter.
9090
*
91-
* @return a new filter
91+
* @return a new filter.
9292
* @since 2.9.0
9393
*/
9494
default IOFileFilter negate() {
@@ -99,7 +99,7 @@ default IOFileFilter negate() {
9999
* Creates a new "or" filter with this filter.
100100
*
101101
* @param fileFilter the filter to "or".
102-
* @return a new filter
102+
* @return a new filter.
103103
* @since 2.9.0
104104
*/
105105
default IOFileFilter or(final IOFileFilter fileFilter) {

src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,37 @@
1616
*/
1717
package org.apache.commons.io.input.buffer;
1818

19+
import static org.apache.commons.io.IOUtils.EOF;
20+
1921
import java.io.IOException;
2022
import java.io.InputStream;
2123
import java.util.Objects;
2224

2325
import org.apache.commons.io.IOUtils;
2426

25-
2627
/**
27-
* Implementation of a buffered input stream, which is internally based on the
28-
* {@link CircularByteBuffer}. Unlike the {@link java.io.BufferedInputStream}, this one
29-
* doesn't need to reallocate byte arrays internally.
28+
* Implements a buffered input stream, which is internally based on a {@link CircularByteBuffer}. Unlike the
29+
* {@link java.io.BufferedInputStream}, this one doesn't need to reallocate byte arrays internally.
3030
*/
3131
public class CircularBufferInputStream extends InputStream {
32+
33+
/** What we are streaming, used to fill the internal buffer. */
3234
protected final InputStream in;
35+
36+
/** Internal buffer. */
3337
protected final CircularByteBuffer buffer;
38+
39+
/** Internal buffer size. */
3440
protected final int bufferSize;
35-
private boolean eofSeen;
41+
42+
/** Whether we've see the input stream EOF. */
43+
private boolean eof;
3644

3745
/**
38-
* Creates a new instance, which filters the given input stream, and
39-
* uses the given buffer size.
46+
* Creates a new instance, which filters the given input stream, and uses the given buffer size.
4047
*
41-
* @param inputStream The input stream, which is being buffered.
42-
* @param bufferSize The size of the {@link CircularByteBuffer}, which is
43-
* used internally.
48+
* @param inputStream The input stream, which is being buffered.
49+
* @param bufferSize The size of the {@link CircularByteBuffer}, which is used internally.
4450
*/
4551
public CircularBufferInputStream(final InputStream inputStream, final int bufferSize) {
4652
if (bufferSize <= 0) {
@@ -49,12 +55,12 @@ public CircularBufferInputStream(final InputStream inputStream, final int buffer
4955
this.in = Objects.requireNonNull(inputStream, "inputStream");
5056
this.buffer = new CircularByteBuffer(bufferSize);
5157
this.bufferSize = bufferSize;
52-
this.eofSeen = false;
58+
this.eof = false;
5359
}
5460

5561
/**
56-
* Creates a new instance, which filters the given input stream, and
57-
* uses a reasonable default buffer size ({@link IOUtils#DEFAULT_BUFFER_SIZE}).
62+
* Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size
63+
* ({@link IOUtils#DEFAULT_BUFFER_SIZE}).
5864
*
5965
* @param inputStream The input stream, which is being buffered.
6066
*/
@@ -68,15 +74,15 @@ public CircularBufferInputStream(final InputStream inputStream) {
6874
* @throws IOException in case of an error while reading from the input stream.
6975
*/
7076
protected void fillBuffer() throws IOException {
71-
if (eofSeen) {
77+
if (eof) {
7278
return;
7379
}
7480
int space = buffer.getSpace();
7581
final byte[] buf = new byte[space];
7682
while (space > 0) {
7783
final int res = in.read(buf, 0, space);
78-
if (res == -1) {
79-
eofSeen = true;
84+
if (res == EOF) {
85+
eof = true;
8086
return;
8187
} else if (res > 0) {
8288
buffer.add(buf, 0, res);
@@ -102,7 +108,7 @@ protected boolean haveBytes(final int count) throws IOException {
102108
@Override
103109
public int read() throws IOException {
104110
if (!haveBytes(1)) {
105-
return -1;
111+
return EOF;
106112
}
107113
return buffer.read() & 0xFF; // return unsigned byte
108114
}
@@ -114,15 +120,15 @@ public int read(final byte[] buffer) throws IOException {
114120

115121
@Override
116122
public int read(final byte[] targetBuffer, final int offset, final int length) throws IOException {
117-
Objects.requireNonNull(targetBuffer, "Buffer");
123+
Objects.requireNonNull(targetBuffer, "targetBuffer");
118124
if (offset < 0) {
119125
throw new IllegalArgumentException("Offset must not be negative");
120126
}
121127
if (length < 0) {
122128
throw new IllegalArgumentException("Length must not be negative");
123129
}
124130
if (!haveBytes(length)) {
125-
return -1;
131+
return EOF;
126132
}
127133
final int result = Math.min(length, buffer.getCurrentNumberOfBytes());
128134
for (int i = 0; i < result; i++) {
@@ -134,7 +140,7 @@ public int read(final byte[] targetBuffer, final int offset, final int length) t
134140
@Override
135141
public void close() throws IOException {
136142
in.close();
137-
eofSeen = true;
143+
eof = true;
138144
buffer.clear();
139145
}
140146
}

src/main/java/org/apache/commons/io/input/buffer/PeekableInputStream.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,24 @@
2020
import java.io.InputStream;
2121
import java.util.Objects;
2222

23-
2423
/**
25-
* Implementation of a buffered input stream, which allows to peek into
26-
* the buffers first bytes. This comes in handy when manually implementing
27-
* scanners, lexers, parsers, or the like.
24+
* Implements a buffered input stream, which allows to peek into the buffers first bytes. This comes in handy when
25+
* manually implementing scanners, lexers, parsers, and the like.
2826
*/
2927
public class PeekableInputStream extends CircularBufferInputStream {
3028

3129
/**
32-
* Creates a new instance, which filters the given input stream, and
33-
* uses the given buffer size.
30+
* Creates a new instance, which filters the given input stream, and uses the given buffer size.
3431
*
35-
* @param inputStream The input stream, which is being buffered.
36-
* @param bufferSize The size of the {@link CircularByteBuffer}, which is
37-
* used internally.
32+
* @param inputStream The input stream, which is being buffered.
33+
* @param bufferSize The size of the {@link CircularByteBuffer}, which is used internally.
3834
*/
3935
public PeekableInputStream(final InputStream inputStream, final int bufferSize) {
4036
super(inputStream, bufferSize);
4137
}
4238

4339
/**
44-
* Creates a new instance, which filters the given input stream, and
45-
* uses a reasonable default buffer size (8192).
40+
* Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (8192).
4641
*
4742
* @param inputStream The input stream, which is being buffered.
4843
*/
@@ -51,29 +46,21 @@ public PeekableInputStream(final InputStream inputStream) {
5146
}
5247

5348
/**
54-
* Returns, whether the next bytes in the buffer are as given by
55-
* {@code sourceBuffer}. This is equivalent to {@link #peek(byte[], int, int)}
56-
* with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
49+
* Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}. This is equivalent to
50+
* {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
5751
*
5852
* @param sourceBuffer the buffer to compare against
5953
* @return true if the next bytes are as given
6054
* @throws IOException Refilling the buffer failed.
6155
*/
6256
public boolean peek(final byte[] sourceBuffer) throws IOException {
63-
Objects.requireNonNull(sourceBuffer, "Buffer");
64-
if (sourceBuffer.length > bufferSize) {
65-
throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
66-
+ " bytes exceeds buffer size of " + bufferSize + " bytes");
67-
}
68-
if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
69-
fillBuffer();
70-
}
71-
return buffer.peek(sourceBuffer, 0, sourceBuffer.length);
57+
Objects.requireNonNull(sourceBuffer, "sourceBuffer");
58+
return peek(sourceBuffer, 0, sourceBuffer.length);
7259
}
7360

7461
/**
75-
* Returns, whether the next bytes in the buffer are as given by
76-
* {@code sourceBuffer}, {code offset}, and {@code length}.
62+
* Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}, {code offset}, and
63+
* {@code length}.
7764
*
7865
* @param sourceBuffer the buffer to compare against
7966
* @param offset the start offset
@@ -82,10 +69,10 @@ public boolean peek(final byte[] sourceBuffer) throws IOException {
8269
* @throws IOException if there is a problem calling fillBuffer()
8370
*/
8471
public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException {
85-
Objects.requireNonNull(sourceBuffer, "Buffer");
72+
Objects.requireNonNull(sourceBuffer, "sourceBuffer");
8673
if (sourceBuffer.length > bufferSize) {
8774
throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
88-
+ " bytes exceeds buffer size of " + bufferSize + " bytes");
75+
+ " bytes exceeds buffer size of " + bufferSize + " bytes");
8976
}
9077
if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
9178
fillBuffer();

0 commit comments

Comments
 (0)