Skip to content

Commit

Permalink
Use long instead of DataSize in LimitedInputStream
Browse files Browse the repository at this point in the history
It is implicit that we are dealing with bytes in the context of an
InputStream, and we were invoking .toBytes() on every use anyway.
  • Loading branch information
runeflobakk committed Jul 30, 2024
1 parent dd82650 commit 67e2f78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/no/digipost/DiggIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static <T extends AutoCloseable, R> Function<T, R> autoClosing(ThrowingFu
* @see #limit(InputStream, DataSize, Supplier)
*/
public static InputStream limit(InputStream inputStream, DataSize maxDataToRead) {
return new LimitedInputStream(inputStream, maxDataToRead, LimitedInputStream.SILENTLY_EOF_ON_REACHING_LIMIT);
return new LimitedInputStream(inputStream, maxDataToRead.toBytes(), LimitedInputStream.SILENTLY_EOF_ON_REACHING_LIMIT);
}


Expand All @@ -94,7 +94,7 @@ public static InputStream limit(InputStream inputStream, DataSize maxDataToRead)
* a non-{@link RuntimeException} which is <em>not</em> an {@link IOException}, it will be wrapped in a {@code RuntimeException}.
*/
public static InputStream limit(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes) {
return new LimitedInputStream(inputStream, maxDataToRead, throwIfTooManyBytes);
return new LimitedInputStream(inputStream, maxDataToRead.toBytes(), throwIfTooManyBytes);
}


Expand Down
17 changes: 14 additions & 3 deletions src/main/java/no/digipost/io/LimitedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,28 @@ private SilentlyEofWhenReachingLimit() {}
public static final Supplier<Exception> SILENTLY_EOF_ON_REACHING_LIMIT = new SilentlyEofWhenReachingLimit();


private final DataSize limit;
private final long maxBytesCount;
private final Supplier<? extends Exception> throwIfTooManyBytes;
private long count;


/**
* @see no.digipost.DiggIO#limit(InputStream, DataSize, Supplier)
*
* @deprecated Pending removal to avoid depending on {@link DataSize}. The constructor methods provided in
* {@link no.digipost.DiggIO} will allow using {@code DataSize}.
*/
@Deprecated
public LimitedInputStream(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes) {
this(inputStream, maxDataToRead.toBytes(), throwIfTooManyBytes);
}

/**
* @see no.digipost.DiggIO#limit(InputStream, DataSize, Supplier)
*/
public LimitedInputStream(InputStream inputStream, long maxBytesCount, Supplier<? extends Exception> throwIfTooManyBytes) {
super(inputStream);
this.limit = maxDataToRead;
this.maxBytesCount = maxBytesCount;
this.throwIfTooManyBytes = throwIfTooManyBytes;
}

Expand Down Expand Up @@ -134,7 +145,7 @@ public int read(byte[] b, int off, int len) throws IOException {


private boolean hasReachedLimit() throws IOException {
if (count > limit.toBytes()) {
if (count > maxBytesCount) {
if (throwIfTooManyBytes == SILENTLY_EOF_ON_REACHING_LIMIT) {
return true;
}
Expand Down

0 comments on commit 67e2f78

Please sign in to comment.