26

I'm dealing with some Java code in which there's an InputStream that I read one time and then I need to read it once again in the same method.

The problem is that I need to reset it's position to the start in order to read it twice.

I've found a hack-ish solution to the problem:

is.mark(Integer.MAX_VALUE);

//Read the InputStream is fully
// { ... }

try
{
    is.reset();
}
catch (IOException e)
{
    e.printStackTrace();
}

Does this solution lead to some unespected behaviours? Or it will work in it's dumbness?

2
  • It might fail in situations that the reading code will mark() it as well. Commented Sep 13, 2013 at 19:30
  • You may be able to extend InputStream, override the mark method so that it can only be called once in an instance. Commented Sep 13, 2013 at 19:32

5 Answers 5

12

As written, you have no guarantees, because mark() is not required to report whether it was successful. To get a guarantee, you must first call markSupported(), and it must return true.

Also as written, the specified read limit is very dangerous. If you happen to be using a stream that buffers in-memory, it will potentially allocate a 2GB buffer. On the other hand, if you happen to be using a FileInputStream, you're fine.

A better approach is to use a BufferedInputStream with an explicit buffer.

Sign up to request clarification or add additional context in comments.

3 Comments

That means that a BufferedInputStream can be traversed twice?
@iMineLink - Yes, provided that you give it a large enough buffer. There's nothing that's going to magically store bytes without consuming memory. If that's an issue, you'll need to store the data in a local file (I'm assuming, based on other comments, that you're reading from a socket).
My fault not to have well specified it: I'm reading from an InputStream returned by a getResourceAsStream(xyz) which I think acts as a FileInputStream. So I'll try wrapping it in a BufferedInputStream with explicit buffer size.
3

It depends on the InputStream implementation. You can also think whether it will be better if you use byte[]. The easiest way is to use Apache commons-io:

byte[] bytes = IOUtils.toByteArray(inputSream);

2 Comments

The byte[] would be too heavy for me to alloc...I already tried things like that and on some Android devices i got a OOM...should I compress it? How can I in that case?
If you are using SocketInputStream, you can not use mark() to read it more than once. If you don't have enough memory to save the stream data as a byte array, you can try to redirect the InputStream data to a temporary file, and then you can read the data from that file, using BufferedInputStream ( which supports mark() ) or RandomAccessFile.
2

You can't do this reliably; some InputStreams (such as ones connected to terminals or sockets) don't support mark and reset (see markSupported). If you really have to traverse the data twice, you need to read it into your own buffer.

Comments

2

For me, the easiest solution was to pass the object from which the InputStream could be obtained, and just obtain it again. In my case, it was from a ContentResolver.

Comments

1

Instead of trying to reset the InputStream load it into a buffer like a StringBuilder or if it's a binary data stream a ByteArrayOutputStream. You can then process the buffer within the method as many times as you want.

ByteArrayOutputStream bos = new ByteArrayOutputStream();

int read = 0;
byte[] buff = new byte[1024];
while ((read = inStream.read(buff)) != -1) {
    bos.write(buff, 0, read);
}
byte[] streamData = bos.toByteArray();

1 Comment

As commented at the other similar answer, the byte[] would be too heavy for me to alloc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.