Skip to content

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



One from Apache James on Github

1 People found this is helpful