Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 244985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:03:05+00:00 2026-05-11T21:03:05+00:00

I have an InputStream of a file and i use apache poi components to

  • 0

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

POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);

The problem is that i need to use the same stream multiple times and the POIFSFileSystem closes the stream after use.

What is the best way to cache the data from the input stream and then serve more input streams to different POIFSFileSystem ?

EDIT 1:

By cache i meant store for later use, not as a way to speedup the application. Also is it better to just read up the input stream into an array or string and then create input streams for each use ?

EDIT 2:

Sorry to reopen the question, but the conditions are somewhat different when working inside desktop and web application.
First of all, the InputStream i get from the org.apache.commons.fileupload.FileItem in my tomcat web app doesn’t support markings thus cannot reset.

Second, I’d like to be able to keep the file in memory for faster acces and less io problems when dealing with files.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:03:05+00:00Added an answer on May 11, 2026 at 9:03 pm

    you can decorate InputStream being passed to POIFSFileSystem with a version that when close() is called it respond with reset():

    class ResetOnCloseInputStream extends InputStream {
    
        private final InputStream decorated;
    
        public ResetOnCloseInputStream(InputStream anInputStream) {
            if (!anInputStream.markSupported()) {
                throw new IllegalArgumentException("marking not supported");
            }
    
            anInputStream.mark( 1 << 24); // magic constant: BEWARE
            decorated = anInputStream;
        }
    
        @Override
        public void close() throws IOException {
            decorated.reset();
        }
    
        @Override
        public int read() throws IOException {
            return decorated.read();
        }
    }
    

    testcase

    static void closeAfterInputStreamIsConsumed(InputStream is)
            throws IOException {
        int r;
    
        while ((r = is.read()) != -1) {
            System.out.println(r);
        }
    
        is.close();
        System.out.println("=========");
    
    }
    
    public static void main(String[] args) throws IOException {
        InputStream is = new ByteArrayInputStream("sample".getBytes());
        ResetOnCloseInputStream decoratedIs = new ResetOnCloseInputStream(is);
        closeAfterInputStreamIsConsumed(decoratedIs);
        closeAfterInputStreamIsConsumed(decoratedIs);
        closeAfterInputStreamIsConsumed(is);
    }
    

    EDIT 2

    you can read the entire file in a byte[] (slurp mode) then passing it to a ByteArrayInputStream

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Apache Commons Net for FTP file transfers. Problem is files
I have created CSVReader and I am trying to read csv file from assets
First, I have researched this question a lot. I learned how to read from
I'm trying to read a text file line by line using InputStream from the
I have a simple read a txt-file function. AssetManager mngr = getAssets(); InputStream is
I have this line in my program : InputStream Resource_InputStream=this.getClass().getClassLoader().getResourceAsStream(Resource_Name); But how can I
I have a problem reading OWL/XML files from Java using Jena. I have no
I am trying to use DotNetZip 1.9 to read an uploaded zip file in
I have been struggling with this for a while and could really use some
I have a setup in which I make use of a txt file (both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.