Skip to content

Commit 8acfd4c

Browse files
committed
Add ChunkedOutputStream.Builder and refactor
- Javadoc - In builders, a negative or zero size buffer should reset the buffer size to it default buffer size value - In builders, AbstractOriginSupplier.checkOrigin() now throws IllegalStateException instead of NullPointerException
1 parent 6d29faf commit 8acfd4c

15 files changed

Lines changed: 167 additions & 45 deletions

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ The <action> type attribute can be add,update,fix,remove.
5555
<action dev="ggregory" type="fix" due-to="Gary Gregory">
5656
ByteArrayOrigin should be able convert a byte[] to a ByteArrayInputStream.
5757
</action>
58+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
59+
AbstractOriginSupplier.checkOrigin() now throws IllegalStateException instead of NullPointerException.
60+
</action>
5861
<!-- ADD -->
5962
<action dev="ggregory" type="add" due-to="Gary Gregory">
6063
Add CharSequenceInputStream.Builder.
6164
</action>
65+
<action dev="ggregory" type="add" due-to="Gary Gregory">
66+
Add ChunkedOutputStream.Builder.
67+
</action>
6268
<action dev="ggregory" type="add" due-to="Gary Gregory">
6369
Add AbstractStreamBuilder.setOpenOptions(OpenOption...).
6470
</action>

src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ protected static WriterOrigin newWriterOrigin(final Writer origin) {
166166
* Checks whether the origin is null.
167167
*
168168
* @return the origin.
169-
* @throws NullPointerException if the {@code origin} is {@code null}
169+
* @throws IllegalStateException if the {@code origin} is {@code null}.
170170
*/
171171
protected AbstractOrigin<?, ?> checkOrigin() {
172-
return Objects.requireNonNull(origin, "origin");
172+
if (origin == null) {
173+
throw new IllegalStateException("origin == null");
174+
}
175+
return origin;
173176
}
174177

175178
/**

src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.Writer;
2424
import java.nio.charset.Charset;
2525
import java.nio.file.OpenOption;
26+
import java.nio.file.Path;
2627

2728
import org.apache.commons.io.Charsets;
2829
import org.apache.commons.io.IOUtils;
@@ -85,11 +86,12 @@ protected int getBufferSizeDefault() {
8586
* @return An input stream
8687
* @throws IOException if an I/O error occurs.
8788
* @throws UnsupportedOperationException if the origin cannot be converted to a CharSequence.
89+
* @throws IllegalStateException if the {@code origin} is {@code null}.
8890
* @see AbstractOrigin#getCharSequence(Charset)
8991
* @since 2.13.0
9092
*/
9193
protected CharSequence getCharSequence() throws IOException {
92-
return getOrigin().getCharSequence(getCharset());
94+
return checkOrigin().getCharSequence(getCharset());
9395
}
9496

9597
/**
@@ -117,27 +119,42 @@ protected Charset getCharsetDefault() {
117119
* @throws IOException if an I/O error occurs.
118120
* @throws UnsupportedOperationException if the origin cannot be converted to an InputStream.
119121
* @see AbstractOrigin#getInputStream(OpenOption...)
122+
* @throws IllegalStateException if the {@code origin} is {@code null}.
120123
* @since 2.13.0
121124
*/
122125
protected InputStream getInputStream() throws IOException {
123-
return getOrigin().getInputStream(getOpenOptions());
126+
return checkOrigin().getInputStream(getOpenOptions());
124127
}
125128

126129
protected OpenOption[] getOpenOptions() {
127130
return openOptions;
128131
}
129132

130133
/**
131-
* Gets an output stream from the origin with open options.
134+
* Gets an OutputStream from the origin with open options.
132135
*
133-
* @return An input stream
136+
* @return An OutputStream
134137
* @throws IOException if an I/O error occurs.
135138
* @throws UnsupportedOperationException if the origin cannot be converted to an OututStream.
139+
* @throws IllegalStateException if the {@code origin} is {@code null}.
136140
* @see AbstractOrigin#getOutputStream(OpenOption...)
137141
* @since 2.13.0
138142
*/
139143
protected OutputStream getOutputStream() throws IOException {
140-
return getOrigin().getOutputStream(getOpenOptions());
144+
return checkOrigin().getOutputStream(getOpenOptions());
145+
}
146+
147+
/**
148+
* Gets a Path from the origin.
149+
*
150+
* @return A Path
151+
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
152+
* @throws IllegalStateException if the {@code origin} is {@code null}.
153+
* @see AbstractOrigin#getPath()
154+
* @since 2.13.0
155+
*/
156+
protected Path getPath() {
157+
return checkOrigin().getPath();
141158
}
142159

143160
/**
@@ -146,15 +163,16 @@ protected OutputStream getOutputStream() throws IOException {
146163
* @return An writer.
147164
* @throws IOException if an I/O error occurs.
148165
* @throws UnsupportedOperationException if the origin cannot be converted to a Writer.
166+
* @throws IllegalStateException if the {@code origin} is {@code null}.
149167
* @see AbstractOrigin#getOutputStream(OpenOption...)
150168
* @since 2.13.0
151169
*/
152170
protected Writer getWriter() throws IOException {
153-
return getOrigin().getWriter(getCharset(), getOpenOptions());
171+
return checkOrigin().getWriter(getCharset(), getOpenOptions());
154172
}
155173

156174
/**
157-
* Sets the buffer size.
175+
* Sets the buffer size. Invalid input (bufferSize &lt;= 0) resets the value to its default.
158176
* <p>
159177
* Subclasses may ignore this setting.
160178
* </p>
@@ -163,7 +181,7 @@ protected Writer getWriter() throws IOException {
163181
* @return this.
164182
*/
165183
public B setBufferSize(final int bufferSize) {
166-
this.bufferSize = bufferSize >= 0 ? bufferSize : bufferSizeDefault;
184+
this.bufferSize = bufferSize > 0 ? bufferSize : bufferSizeDefault;
167185
return asThis();
168186
}
169187

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static class Builder extends AbstractStreamBuilder<BufferedFileChannelInp
8686
*/
8787
@Override
8888
public BufferedFileChannelInputStream get() throws IOException {
89-
return new BufferedFileChannelInputStream(getOrigin().getPath(), getBufferSize());
89+
return new BufferedFileChannelInputStream(getPath(), getBufferSize());
9090
}
9191

9292
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public Builder() {
108108
*/
109109
@Override
110110
public MemoryMappedFileInputStream get() throws IOException {
111-
return new MemoryMappedFileInputStream(getOrigin().getPath(), getBufferSize());
111+
return new MemoryMappedFileInputStream(getPath(), getBufferSize());
112112
}
113113
}
114114

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ public static class Builder extends AbstractStreamBuilder<ReaderInputStream, Bui
114114
*
115115
* @return a new instance.
116116
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
117+
* @throws IllegalStateException if the {@code origin} is {@code null}.
117118
* @see AbstractOrigin#getReader(Charset)
118119
*/
119120
@SuppressWarnings("resource")
120121
@Override
121122
public ReaderInputStream get() throws IOException {
122-
return new ReaderInputStream(getOrigin().getReader(getCharset()), charsetEncoder, getBufferSize());
123+
return new ReaderInputStream(checkOrigin().getReader(getCharset()), charsetEncoder, getBufferSize());
123124
}
124125

125126
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Builder() {
8787
*/
8888
@Override
8989
public ReversedLinesFileReader get() throws IOException {
90-
return new ReversedLinesFileReader(getOrigin().getPath(), getBufferSize(), getCharset());
90+
return new ReversedLinesFileReader(getPath(), getBufferSize(), getCharset());
9191
}
9292

9393
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ public static class Builder extends AbstractStreamBuilder<UncheckedBufferedReade
7878
*
7979
* @return a new instance.
8080
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
81+
* @throws IllegalStateException if the {@code origin} is {@code null}.
8182
* @see AbstractOrigin#getReader(Charset)
8283
*/
8384
@Override
8485
public UncheckedBufferedReader get() {
8586
// This an unchecked class, so this method is as well.
86-
return Uncheck.get(() -> new UncheckedBufferedReader(getOrigin().getReader(getCharset()), getBufferSize()));
87+
return Uncheck.get(() -> new UncheckedBufferedReader(checkOrigin().getReader(getCharset()), getBufferSize()));
8788
}
8889

8990
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader,
7474
*
7575
* @return a new instance.
7676
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
77+
* @throws IllegalStateException if the {@code origin} is {@code null}.
7778
* @see AbstractOrigin#getReader(Charset)
7879
*/
7980
@Override
8081
public UncheckedFilterReader get() {
8182
// This an unchecked class, so this method is as well.
82-
return Uncheck.get(() -> new UncheckedFilterReader(getOrigin().getReader(getCharset())));
83+
return Uncheck.get(() -> new UncheckedFilterReader(checkOrigin().getReader(getCharset())));
8384
}
8485

8586
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ public static class Builder extends AbstractStreamBuilder<UnsynchronizedByteArra
9292
*
9393
* @return a new instance.
9494
* @throws UnsupportedOperationException if the origin cannot provide a byte[].
95+
* @throws IllegalStateException if the {@code origin} is {@code null}.
9596
* @see AbstractOrigin#getByteArray()
9697
*/
9798
@Override
9899
public UnsynchronizedByteArrayInputStream get() throws IOException {
99-
return new UnsynchronizedByteArrayInputStream(getOrigin().getByteArray(), offset, length);
100+
return new UnsynchronizedByteArrayInputStream(checkOrigin().getByteArray(), offset, length);
100101
}
101102

102103
@Override

0 commit comments

Comments
 (0)