How to Cache InputStream for Multiple Use

后端 未结 10 1049
庸人自扰
庸人自扰 2020-11-29 05:57

I have an InputStream of a file and i use apache poi components to read from it like this:

POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
         


        
10条回答
  •  星月不相逢
    2020-11-29 06:43

    Try BufferedInputStream, which adds mark and reset functionality to another input stream, and just override its close method:

    public class UnclosableBufferedInputStream extends BufferedInputStream {
    
        public UnclosableBufferedInputStream(InputStream in) {
            super(in);
            super.mark(Integer.MAX_VALUE);
        }
    
        @Override
        public void close() throws IOException {
            super.reset();
        }
    }
    

    So:

    UnclosableBufferedInputStream  bis = new UnclosableBufferedInputStream (inputStream);
    

    and use bis wherever inputStream was used before.

提交回复
热议问题