-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom LimitedInputStream instead of filesupload library
- Loading branch information
Showing
5 changed files
with
48 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,5 @@ | |
|
||
requires static java.compiler; | ||
requires static org.immutables.value; | ||
requires commons.fileupload; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
multiapps-mta/src/main/java/org/cloudfoundry/multiapps/mta/util/LimitedInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.cloudfoundry.multiapps.mta.util; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public abstract class LimitedInputStream extends FilterInputStream { | ||
|
||
private final AtomicLong counter; | ||
private final long maxSize; | ||
|
||
public LimitedInputStream(InputStream in, long maxSize) { | ||
super(in); | ||
this.counter = new AtomicLong(0); | ||
this.maxSize = maxSize; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int i = super.read(); | ||
if (i > 0) { | ||
counter.incrementAndGet(); | ||
checkSize(); | ||
} | ||
return i; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int i = super.read(b, off, len); | ||
if (i > 0) { | ||
counter.addAndGet(i); | ||
checkSize(); | ||
} | ||
return i; | ||
} | ||
|
||
private void checkSize() { | ||
long currentSize = counter.get(); | ||
if (currentSize >= maxSize) { | ||
raiseError(maxSize, currentSize); | ||
} | ||
} | ||
|
||
protected abstract void raiseError(long maxSize, long currentSize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters