Ionut Negru
Ionut Negru

Reputation: 6314

Android/Java InputStream becomes empty (unreadable)

I'm struggling with an issue that seems to be much more complicated than I initially though.

I use an SDK which passes me an InputStream which I should use to make an http request(the stream is used to set the body - it is optional for some requests). I'm using the HttpURLConnection to make the request and get the response.

The problem appears as soon as I send the InputStream (it is found inside an custom model object) to another class through an static method.

RequestManager.startRequest(request, callback);

the request contains the InputStream. At this point it seems to be valid.

As soon as the InputStream is tried to be accessed in the RequestManager class or in other classes it is empty.

I must point out that it is sent to another thread which is handled by the RequestManager.

What could be the problem? I made some debugs and the InputStream seems to be the same object that it was sent by the SDK. Could something happen to the InputStream while the thread runs?

LE(method for read InputStream):

private String readStream(InputStream inputStream) {
        BufferedReader br = new BufferedReader(new InputStreamReader((inputStream)));
        StringBuilder sb = new StringBuilder();
        String output;
        try {
            while ((output = br.readLine()) != null) {
                sb.append(output);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }

If I use this method right before calling startRequest() it returns the String which should be set as the body of the request.

As soon as I try to use this method inside the manager, it returns an empty String.

LE: Added Screenshots of debug InputStream received from the SDK InputStream received from the SDK Read InputStream from SDK - before sending it to manager Read InputStream from SDK - before sending it to manager Read InputStream inside the manager before setting the body Read InputStream inside the manager before setting the body

LE: update

I have made more tests and it seems this is happening when the InputSream is accessed from another thread directly. If the InputStream is accessed on the same Thread that was created, everything is working fine, but as soon as some other Thread tries to access it becomes unavailable (it seems the read is blocked).

Upvotes: 0

Views: 1159

Answers (1)

BNK
BNK

Reputation: 24114

As we discuss above, the offsets are different. So you should reset the stream (inputStream.reset();)before the 2nd read. This is my suggestion and answer. Hope this help!

UPDATE:

According to InputStream description

public synchronized void reset ()

Resets this stream to the last marked location. Throws an IOException if the number of bytes read since the mark has been set is greater than the limit provided to mark, or if no mark has been set.

I think you should check all your codes again

Moreover, refer to the following questions, hope you fix your issue soon:

  1. InputStream will not reset to beginning
  2. Read stream twice

Upvotes: 0

Related Questions