Skip to content

Commit fb78f68

Browse files
committed
Add AbstractStreamBuilder.setOpenOptions(OpenOption...)
1 parent ff37401 commit fb78f68

19 files changed

Lines changed: 152 additions & 65 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ The <action> type attribute can be add,update,fix,remove.
5656
<action dev="ggregory" type="fix" due-to="Gary Gregory">
5757
Add CharSequenceInputStream.Builder.
5858
</action>
59+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
60+
Add AbstractStreamBuilder.setOpenOptions(OpenOption...).
61+
</action>
5962
<!-- UPDATE -->
6063
</release>
6164
<release version="2.12.0" date="2023-05-13" description="Java 8 required.">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public byte[] getByteArray() throws IOException {
293293
* @throws UnsupportedOperationException if this method is not implemented in a concrete subclass.
294294
*/
295295
public File getFile() {
296-
throw new UnsupportedOperationException(origin.toString());
296+
throw new UnsupportedOperationException("getFile() from " + origin.toString());
297297
}
298298

299299
/**
@@ -327,7 +327,7 @@ public OutputStream getOutputStream(final OpenOption... options) throws IOExcept
327327
* @throws UnsupportedOperationException if this method is not implemented in a concrete subclass.
328328
*/
329329
public Path getPath() {
330-
throw new UnsupportedOperationException(origin.toString());
330+
throw new UnsupportedOperationException("getPath() from " + origin.toString());
331331
}
332332

333333
/**

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717

1818
package org.apache.commons.io.build;
1919

20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.io.Writer;
2024
import java.nio.charset.Charset;
25+
import java.nio.file.OpenOption;
2126

2227
import org.apache.commons.io.Charsets;
2328
import org.apache.commons.io.IOUtils;
29+
import org.apache.commons.io.file.PathUtils;
2430

2531
/**
2632
* Abstracts building a typed instance of {@code T}.
@@ -31,6 +37,8 @@
3137
*/
3238
public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T, B>> extends AbstractOriginSupplier<T, B> {
3339

40+
private static final OpenOption[] DEFAULT_OPEN_OPTIONS = PathUtils.EMPTY_OPEN_OPTION_ARRAY;
41+
3442
/**
3543
* The buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}).
3644
*/
@@ -51,6 +59,8 @@ public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T
5159
*/
5260
private Charset charsetDefault = Charset.defaultCharset();
5361

62+
private OpenOption[] openOptions = DEFAULT_OPEN_OPTIONS;
63+
5464
/**
5565
* Gets the buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}).
5666
*
@@ -87,6 +97,49 @@ protected Charset getCharsetDefault() {
8797
return charsetDefault;
8898
}
8999

100+
/**
101+
* Gets an input stream from the origin with open options.
102+
*
103+
* @return An input stream
104+
* @throws IOException if an I/O error occurs.
105+
* @throws UnsupportedOperationException if the origin cannot be converted to an InputStream.
106+
* @see AbstractOrigin#getInputStream(OpenOption...)
107+
* @since 2.13.0
108+
*/
109+
protected InputStream getInputStream() throws IOException {
110+
return getOrigin().getInputStream(getOpenOptions());
111+
}
112+
113+
protected OpenOption[] getOpenOptions() {
114+
return openOptions;
115+
}
116+
117+
/**
118+
* Gets an output stream from the origin with open options.
119+
*
120+
* @return An input stream
121+
* @throws IOException if an I/O error occurs.
122+
* @throws UnsupportedOperationException if the origin cannot be converted to an OututStream.
123+
* @see AbstractOrigin#getOutputStream(OpenOption...)
124+
* @since 2.13.0
125+
*/
126+
protected OutputStream getOutputStream() throws IOException {
127+
return getOrigin().getOutputStream(getOpenOptions());
128+
}
129+
130+
/**
131+
* Gets an writer from the origin with open options.
132+
*
133+
* @return An writer.
134+
* @throws IOException if an I/O error occurs.
135+
* @throws UnsupportedOperationException if the origin cannot be converted to a Writer.
136+
* @see AbstractOrigin#getOutputStream(OpenOption...)
137+
* @since 2.13.0
138+
*/
139+
protected Writer getWriter() throws IOException {
140+
return getOrigin().getWriter(getCharset(), getOpenOptions());
141+
}
142+
90143
/**
91144
* Sets the buffer size.
92145
* <p>
@@ -169,4 +222,25 @@ protected B setCharsetDefault(final Charset defaultCharset) {
169222
this.charsetDefault = defaultCharset;
170223
return asThis();
171224
}
225+
226+
/**
227+
* Sets the OpenOption[].
228+
* <p>
229+
* Normally used with InputStream, OutputStream, and Writer.
230+
* </p>
231+
* <p>
232+
* Subclasses may ignore this setting.
233+
* </p>
234+
*
235+
* @param openOptions the OpenOption[] name, null resets to the default.
236+
* @return this.
237+
* @since 2.13.0
238+
* @see #setInputStream(InputStream)
239+
* @see #setOutputStream(OutputStream)
240+
* @see #setWriter(Writer)
241+
*/
242+
public B setOpenOptions(final OpenOption... openOptions) {
243+
this.openOptions = openOptions != null ? openOptions : DEFAULT_OPEN_OPTIONS;
244+
return asThis();
245+
}
172246
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020

2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.nio.file.OpenOption;
2423
import java.util.Arrays;
2524
import java.util.Comparator;
2625
import java.util.List;
2726
import java.util.Objects;
2827

2928
import org.apache.commons.io.ByteOrderMark;
3029
import org.apache.commons.io.IOUtils;
31-
import org.apache.commons.io.build.AbstractOrigin;
3230
import org.apache.commons.io.build.AbstractStreamBuilder;
3331

3432
/**
@@ -136,7 +134,7 @@ static ByteOrderMark getDefaultByteOrderMark() {
136134
/**
137135
* Constructs a new instance.
138136
* <p>
139-
* This builder use the aspects InputStream, include, and ByteOrderMark[].
137+
* This builder use the aspects InputStream, OpenOption[], include, and ByteOrderMark[].
140138
* </p>
141139
* <p>
142140
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -145,12 +143,12 @@ static ByteOrderMark getDefaultByteOrderMark() {
145143
*
146144
* @return a new instance.
147145
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
148-
* @see AbstractOrigin#getInputStream(OpenOption...)
146+
* @see #getInputStream()
149147
*/
150148
@SuppressWarnings("resource")
151149
@Override
152150
public BOMInputStream get() throws IOException {
153-
return new BOMInputStream(getOrigin().getInputStream(), include, byteOrderMarks);
151+
return new BOMInputStream(getInputStream(), include, byteOrderMarks);
154152
}
155153

156154
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.nio.file.OpenOption;
2221
import java.security.MessageDigest;
2322
import java.security.NoSuchAlgorithmException;
2423
import java.security.Provider;
2524

26-
import org.apache.commons.io.build.AbstractOrigin;
2725
import org.apache.commons.io.build.AbstractStreamBuilder;
2826

2927
/**
@@ -75,7 +73,7 @@ public Builder() {
7573
/**
7674
* Constructs a new instance.
7775
* <p>
78-
* This builder use the aspects InputStream and MessageDigest.
76+
* This builder use the aspects InputStream, OpenOption[], and MessageDigest.
7977
* </p>
8078
* <p>
8179
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -84,12 +82,12 @@ public Builder() {
8482
*
8583
* @return a new instance.
8684
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
87-
* @see AbstractOrigin#getInputStream(OpenOption...)
85+
* @see #getInputStream()
8886
*/
8987
@SuppressWarnings("resource")
9088
@Override
9189
public MessageDigestCalculatingInputStream get() throws IOException {
92-
return new MessageDigestCalculatingInputStream(getOrigin().getInputStream(), messageDigest);
90+
return new MessageDigestCalculatingInputStream(getInputStream(), messageDigest);
9391
}
9492

9593
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.InputStream;
2323
import java.io.InterruptedIOException;
2424
import java.nio.ByteBuffer;
25-
import java.nio.file.OpenOption;
2625
import java.util.Objects;
2726
import java.util.concurrent.ExecutorService;
2827
import java.util.concurrent.Executors;
@@ -31,7 +30,6 @@
3130
import java.util.concurrent.locks.Condition;
3231
import java.util.concurrent.locks.ReentrantLock;
3332

34-
import org.apache.commons.io.build.AbstractOrigin;
3533
import org.apache.commons.io.build.AbstractStreamBuilder;
3634

3735
/**
@@ -71,7 +69,7 @@ public static class Builder extends AbstractStreamBuilder<ReadAheadInputStream,
7169
/**
7270
* Constructs a new instance.
7371
* <p>
74-
* This builder use the aspects InputStream, buffer size, ExecutorService.
72+
* This builder use the aspects InputStream, OpenOption[], buffer size, ExecutorService.
7573
* </p>
7674
* <p>
7775
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -80,12 +78,12 @@ public static class Builder extends AbstractStreamBuilder<ReadAheadInputStream,
8078
*
8179
* @return a new instance.
8280
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
83-
* @see AbstractOrigin#getInputStream(OpenOption...)
81+
* @see #getInputStream()
8482
*/
8583
@SuppressWarnings("resource")
8684
@Override
8785
public ReadAheadInputStream get() throws IOException {
88-
return new ReadAheadInputStream(getOrigin().getInputStream(), getBufferSize(), executorService != null ? executorService : newExecutorService(),
86+
return new ReadAheadInputStream(getInputStream(), getBufferSize(), executorService != null ? executorService : newExecutorService(),
8987
executorService == null);
9088
}
9189

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.UncheckedIOException;
25-
import java.nio.file.OpenOption;
2625

27-
import org.apache.commons.io.build.AbstractOrigin;
2826
import org.apache.commons.io.build.AbstractStreamBuilder;
2927
import org.apache.commons.io.function.Uncheck;
3028

@@ -65,7 +63,7 @@ public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputSt
6563
/**
6664
* Constructs a new instance.
6765
* <p>
68-
* This builder use the aspect InputStream.
66+
* This builder use the aspect InputStream and OpenOption[].
6967
* </p>
7068
* <p>
7169
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -74,12 +72,12 @@ public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputSt
7472
*
7573
* @return a new instance.
7674
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
77-
* @see AbstractOrigin#getInputStream(OpenOption...)
75+
* @see #getInputStream()
7876
*/
7977
@Override
8078
public UncheckedFilterInputStream get() {
8179
// This an unchecked class, so this method is as well.
82-
return Uncheck.get(() -> new UncheckedFilterInputStream(getOrigin().getInputStream()));
80+
return Uncheck.get(() -> new UncheckedFilterInputStream(getInputStream()));
8381
}
8482

8583
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import java.io.BufferedInputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.nio.file.OpenOption;
2423

2524
import org.apache.commons.io.IOUtils;
26-
import org.apache.commons.io.build.AbstractOrigin;
2725
import org.apache.commons.io.build.AbstractStreamBuilder;
2826

2927
/**
@@ -82,7 +80,7 @@ public static class Builder extends AbstractStreamBuilder<UnsynchronizedBuffered
8280
/**
8381
* Constructs a new instance.
8482
* <p>
85-
* This builder use the aspects InputStream and buffer size.
83+
* This builder use the aspects InputStream, OpenOption[] and buffer size.
8684
* </p>
8785
* <p>
8886
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -91,12 +89,12 @@ public static class Builder extends AbstractStreamBuilder<UnsynchronizedBuffered
9189
*
9290
* @return a new instance.
9391
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
94-
* @see AbstractOrigin#getInputStream(OpenOption...)
92+
* @see #getInputStream()
9593
*/
9694
@SuppressWarnings("resource") // Caller closes.
9795
@Override
9896
public UnsynchronizedBufferedInputStream get() throws IOException {
99-
return new UnsynchronizedBufferedInputStream(getOrigin().getInputStream(), getBufferSize());
97+
return new UnsynchronizedBufferedInputStream(getInputStream(), getBufferSize());
10098
}
10199

102100
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import java.io.FilterInputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.nio.file.OpenOption;
2423

25-
import org.apache.commons.io.build.AbstractOrigin;
2624
import org.apache.commons.io.build.AbstractStreamBuilder;
2725

2826
/**
@@ -69,7 +67,7 @@ public static class Builder extends AbstractStreamBuilder<UnsynchronizedFilterIn
6967
/**
7068
* Constructs a new instance.
7169
* <p>
72-
* This builder use the aspect InputStream.
70+
* This builder use the aspect InputStream and OpenOption[].
7371
* </p>
7472
* <p>
7573
* You must provide an origin that can be converted to an InputStream by this builder, otherwise, this call will throw an
@@ -78,12 +76,12 @@ public static class Builder extends AbstractStreamBuilder<UnsynchronizedFilterIn
7876
*
7977
* @return a new instance.
8078
* @throws UnsupportedOperationException if the origin cannot provide an InputStream.
81-
* @see AbstractOrigin#getInputStream(OpenOption...)
79+
* @see #getInputStream()
8280
*/
8381
@SuppressWarnings("resource") // Caller closes.
8482
@Override
8583
public UnsynchronizedFilterInputStream get() throws IOException {
86-
return new UnsynchronizedFilterInputStream(getOrigin().getInputStream());
84+
return new UnsynchronizedFilterInputStream(getInputStream());
8785
}
8886

8987
}

0 commit comments

Comments
 (0)