Package org.apache.commons.io.input
Class ThrottledInputStream.Builder
java.lang.Object
org.apache.commons.io.build.AbstractSupplier<ThrottledInputStream,ThrottledInputStream.Builder>
org.apache.commons.io.build.AbstractOriginSupplier<ThrottledInputStream,ThrottledInputStream.Builder>
org.apache.commons.io.build.AbstractStreamBuilder<ThrottledInputStream,ThrottledInputStream.Builder>
org.apache.commons.io.input.ProxyInputStream.AbstractBuilder<ThrottledInputStream,ThrottledInputStream.Builder>
org.apache.commons.io.input.ThrottledInputStream.Builder
- All Implemented Interfaces:
IOSupplier<ThrottledInputStream>
- Enclosing class:
ThrottledInputStream
public static class ThrottledInputStream.Builder
extends ProxyInputStream.AbstractBuilder<ThrottledInputStream,ThrottledInputStream.Builder>
Builds a new
ThrottledInputStream.
Using NIO
ThrottledInputStream in = ThrottledInputStream.builder()
.setPath(Paths.get("MyFile.xml"))
.setMaxBytes(100_000, ChronoUnit.SECONDS)
.get();
Using IO
ThrottledInputStream in = ThrottledInputStream.builder()
.setFile(new File("MyFile.xml"))
.setMaxBytes(100_000, ChronoUnit.SECONDS)
.get();
ThrottledInputStream in = ThrottledInputStream.builder()
.setInputStream(inputStream)
.setMaxBytes(100_000, ChronoUnit.SECONDS)
.get();
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget()Builds a newThrottledInputStream.(package private) double(package private) ThrottledInputStream.BuildersetMaxBytes(long value, Duration duration) Sets the maximum bytes per duration.setMaxBytes(long value, ChronoUnit chronoUnit) Sets the maximum bytes per time period unit.private ThrottledInputStream.BuildersetMaxBytesPerSecond(double maxBytesPerSecond) Sets the maximum bytes per second.voidsetMaxBytesPerSecond(long maxBytesPerSecond) Sets the maximum bytes per second.Methods inherited from class org.apache.commons.io.input.ProxyInputStream.AbstractBuilder
getAfterRead, setAfterReadMethods inherited from class org.apache.commons.io.build.AbstractStreamBuilder
getBufferSize, getBufferSizeDefault, getCharSequence, getCharset, getCharsetDefault, getFile, getInputStream, getOpenOptions, getOutputStream, getPath, getRandomAccessFile, getReader, getWriter, setBufferSize, setBufferSize, setBufferSizeChecker, setBufferSizeDefault, setBufferSizeMax, setCharset, setCharset, setCharsetDefault, setOpenOptionsMethods inherited from class org.apache.commons.io.build.AbstractOriginSupplier
checkOrigin, getOrigin, hasOrigin, newByteArrayOrigin, newCharSequenceOrigin, newFileOrigin, newFileOrigin, newInputStreamOrigin, newOutputStreamOrigin, newPathOrigin, newPathOrigin, newRandomAccessFileOrigin, newRandomAccessFileOrigin, newReaderOrigin, newURIOrigin, newWriterOrigin, setByteArray, setCharSequence, setFile, setFile, setInputStream, setOrigin, setOutputStream, setPath, setPath, setRandomAccessFile, setRandomAccessFile, setReader, setURI, setWriterMethods inherited from class org.apache.commons.io.build.AbstractSupplier
asThisMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.io.function.IOSupplier
asSupplier, getUnchecked
-
Field Details
-
maxBytesPerSecond
private double maxBytesPerSecondEffectively not throttled.
-
-
Constructor Details
-
Builder
public Builder()Constructs a new builder ofThrottledInputStream.
-
-
Method Details
-
get
Builds a newThrottledInputStream.You must set an aspect that supports
AbstractStreamBuilder.getInputStream(), otherwise, this method throws an exception.This builder uses the following aspects:
AbstractStreamBuilder.getInputStream()gets the target aspect.- maxBytesPerSecond
- Returns:
- a new instance.
- Throws:
IllegalStateException- if theoriginisnull.UnsupportedOperationException- if the origin cannot be converted to anInputStream.IOException- if an I/O error occurs converting to anInputStreamusingAbstractStreamBuilder.getInputStream().- See Also:
-
getMaxBytesPerSecond
double getMaxBytesPerSecond() -
setMaxBytes
Sets the maximum bytes per time period unit.For example, to throttle reading to 100K per second, use:
builder.setMaxBytes(100_000, ChronoUnit.SECONDS)
To test idle timeouts for example, use 1 byte per minute, 1 byte per 30 seconds, and so on.
- Parameters:
value- the maximum byteschronoUnit- a duration scale goal.- Returns:
- this instance.
- Throws:
IllegalArgumentException- Thrown if maxBytesPerSecond <= 0.- Since:
- 2.19.0
-
setMaxBytes
Sets the maximum bytes per duration.For example, to throttle reading to 100K per second, use:
builder.setMaxBytes(100_000, Duration.ofSeconds(1))
To test idle timeouts for example, use 1 byte per minute, 1 byte per 30 seconds, and so on.
- Parameters:
value- the maximum bytesduration- a duration goal.- Returns:
- this instance.
- Throws:
IllegalArgumentException- Thrown if maxBytesPerSecond <= 0.
-
setMaxBytesPerSecond
Sets the maximum bytes per second.- Parameters:
maxBytesPerSecond- the maximum bytes per second.- Returns:
- this instance.
- Throws:
IllegalArgumentException- Thrown if maxBytesPerSecond <= 0.
-
setMaxBytesPerSecond
public void setMaxBytesPerSecond(long maxBytesPerSecond) Sets the maximum bytes per second.- Parameters:
maxBytesPerSecond- the maximum bytes per second.- Throws:
IllegalArgumentException- Thrown if maxBytesPerSecond <= 0.
-