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

  • Home
  • SEARCH
  • 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 8390121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:54:11+00:00 2026-06-09T18:54:11+00:00

An API that I’m implementing deals with InputStreams containing hierarchically structured data i.e. nested

  • 0

An API that I’m implementing deals with InputStreams containing hierarchically structured data i.e. nested blocks, including a number of images in the leaf blocks. (If you must know, it is CBEFF data that I’m parsing.) Each block of data is prefixed with a header containing some meta-data about that block.

1st level 1 header
    1st level 2 header
    1st level 2 data block
    2nd level 2 header
    2nd level 2 data block
2nd level 1 header
    3rd level 2 header
    3rd level 2 data block

The original InputStream is an argument to the constructor of my API classes and is passed around down the hierarchy.
Currently I’m reading the images into byte arrays in the constructor of my API classes, so each constructor blocks while reading the complete data that that class is responsible for and later on when clients call the relevant getter method of that API class they will get the image data served from memory. I’d much rather offer the contained images in the form of some kind of lazy InputStreams to clients of my API, so that the image bytes are only read from the original InputStream as a result of clients reading bytes of the resulting InputStream delivered by the getter. This makes it possible, for example, to do progressive rendering, which is useful as the original InputStream is slow.

Is there an elegant way to solve this with InputStreams?

  • 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-06-09T18:54:12+00:00Added an answer on June 9, 2026 at 6:54 pm

    InputStream isn’t suitable for random access. So reading parts of it isn’t going to work in most cases, even though you can achieve similar effects on some input streams using a combination of reset and skip. But not all streams support reset, and skipping bytes is often as expensive as reading bytes.

    So I suggest you try some alternate approach. Either you buffer the whole stream to some random-access buffer, like a temporary file, which still means reading all the bytes off the stream in the first place. Or you find a way to get random access to the original source. You didn’t specify what kind of source you’re dealing with, but e.g. for a HTTP connection you can download parts using a range request. Similar solutions might work for other sources.

    No matter how you implement the random access (and seeing your comment, you’ll likely do so using an InputStream with reset and skip), you can create your own class to represent a part of that stream. You can let that class itself be an instance of InputStream by subclassing FilterInputStream.

    cLass SubStream extends FilterInputStream {
        private long offset;
        public SubStream(long offset, InputStream parent) {
            super(parent);
            this.offset = offset;
        }
        public SubStream(InputStream parent) {
            this(0, parent);
        }
        @Override public void reset() throws IOException {
            in.reset();
            in.skip(offset);
        }
        public SubStream subStream(long offset) {
            return new FilterInputStream(this.offset + offset, in);
        }
        public Object syncObject() {
            return in;
        }
    }
    

    You’d have to ensure that any operation using one of these streams calls reset first. If you need to enforce a proper end-of-stream treatment, you’ll have to override most read implementations. If concurrent access might be possible, then you’ll want to synchronize operations on the underlying stream. So the code using this class could look something like this:

         synchronized(part.syncObject()) {
             part.reset();
             return read(part);
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this API that gives me all the images in my galleries. Now,
I am using an API that works with images and one of the methods
I'm implementing an API that must accept fairly large file uploads. The request will
I'm buiding an API that allows me to fetch strings in various encodings, including
I have a web API that returns the next set of x number of
I'm using an API that results an array like this when there is data:
Is there a google api that will calculate closest pair of points? Where I
I have an API that can be implemented by various vendors. I can send
Is there any GUI API that can be used together with DirectX other than
I'm developing a API that uses lambda expressions to specify properties. I'm using this

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.