8

Is there anyway to reuse an inputStream by changing its content? (Without new statement).

For instance, I was able to something very close to my requirement, but not enough

In the following code I am using a SequenceInputStream, and everytime I am adding a new InputStream to that sequence.

But I would like to do the same thing by using the same inputStream (I don't care which implementation of InputStream).

I thought about mark()/reset() APIs, but i still need to change the content to be read.

The idea to avoid new InputStream creations is because of performance issues

     //Input Streams
    List<InputStream> inputStreams = new ArrayList<InputStream>();
    try{
        //First InputStream
        byte[] input = new byte[]{24,8,102,97};
        inputStreams.add(new ByteArrayInputStream(input));

        Enumeration<InputStream> enu = Collections.enumeration(inputStreams);
        SequenceInputStream is = new SequenceInputStream(enu);

        byte [] out = new byte[input.length];
        is.read(out);

        for (byte b : out){
            System.out.println(b);//Will print 24,8,102,97
        }

        //Second InputStream
        input = new byte[]{ 4,66};
        inputStreams.add(new ByteArrayInputStream(input));
        out = new byte[input.length];
        is.read(out);

        for (byte b : out){
            System.out.println(b);//will print 4,66
        }
        is.close();
    }catch (Exception e){//
    }
Ashish Kumar
  • 916
  • 2
  • 15
  • 32
fabriziomieli
  • 141
  • 1
  • 1
  • 9
  • `Vector`! We are in the 21th century! –  Apr 14 '16 at 13:04
  • 1
    consider `ArrayList` instead of `Vector`, you will win on the performance – Andrew Tobilko Apr 14 '16 at 13:07
  • @AndrewTobilko are you sure about that? https://en.wikipedia.org/wiki/Java_performance#Escape_analysis_and_lock_coarsening specifically cites an example of `Vector` not locking unless necessary in Java 6+. Of course, that relies upon the escape analysis detecting that locking is unnecessary; using `ArrayList` means that no locking is done ever (unless done explicitly), so you have more control over the performance. – Andy Turner Apr 14 '16 at 13:10
  • I changed to List so everybody is happier...but that's not the point of my question. This is only a sample code in order to explain my point... – fabriziomieli Apr 14 '16 at 13:11
  • 1
    @DisplayName `Vector` is so 20th Century. ;) – Peter Lawrey Apr 14 '16 at 13:58

2 Answers2

7

No, You can't restart reading the input stream after it reaches to the end of the stream as it is uni-directional i.e. moves only in single direction.

But Refer below links, they may help:

How to Cache InputStream for Multiple Use

Getting an InputStream to read more than once, regardless of markSupported()

Community
  • 1
  • 1
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
1

You could create your own implementation (subclass) of InputStream that would allow what you require. I doubt there is an existing implementation of this.

I highly doubt you'll get any measurable performance boost from this though, there's not much of logic in e.g. FileInputStream that you wouldn't need to perform anyways, and Java is well optimized for garbage-collecting short-lived objects.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43