Skip to content

Commit

Permalink
Use a custom LimitedInputStream instead of filesupload library
Browse files Browse the repository at this point in the history
  • Loading branch information
radito3 committed Jul 24, 2023
1 parent 6aaa2bf commit 1335266
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
4 changes: 0 additions & 4 deletions multiapps-mta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
Expand Down
1 change: 0 additions & 1 deletion multiapps-mta/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@

requires static java.compiler;
requires static org.immutables.value;
requires commons.fileupload;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.fileupload.util.LimitedInputStream;
import org.apache.commons.io.IOUtils;
import org.cloudfoundry.multiapps.common.ContentException;
import org.cloudfoundry.multiapps.common.SLException;
import org.cloudfoundry.multiapps.mta.Messages;
import org.cloudfoundry.multiapps.mta.util.LimitedInputStream;

public class ArchiveHandler {

Expand Down
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);
}
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-io.version>2.11.0</commons-io.version>
<commons-fileupload.version>1.5</commons-fileupload.version>
<slf4j.version>2.0.6</slf4j.version>
<snakeyaml.version>2.0</snakeyaml.version>
<semver4j.version>3.1.0</semver4j.version>
Expand Down Expand Up @@ -304,12 +303,6 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>org.yaml</groupId>
Expand Down

0 comments on commit 1335266

Please sign in to comment.