Skip to content
Advertisement

Java InputStream wrapper which limits the nuber of read bytes

We need to have a process “peek” into the incoming InputStream and using a JSON streaming parser and ensure then no more then XXX bytes are read (i.e. we NEVER want to read past byte XX). Ideally this should come in the form of an InputStream wrapper that throws some kind of “soft” exception

InputStream limited = new SizeLimitedInputStream(realInputStrea, 500000); // max 500000 bytes
try {
    // peek the input
} catch (SizeLimitReachedException e) {
   // that's ok
}

I’ve been looking into existing libs or JDK to avoid writing this from scratch but I don’t bee which’s the best starting class for this

Advertisement

Answer

One from Apache James on Github https://github.com/k4200/openmailarchiva-server/blob/b42f9e4f2b5b0fba9cae291fdfcaa1fc23ae5fa0/src/org/apache/james/util/SizeLimitedInputStream.java

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement