InputStream != InputStream


Tough problem y’all! (Disclaimer I don’t have the solution yet but I gonna babble like I know what I’m talking about!) I got used to treating all InputStreams as equal. What I mean by that is I assumed that read() would return the same values when the streams are loaded with the same data. For example, take a File loaded with a Mobb Deep mp3. Stream that into an object. Why would continuing to read that be any different than if the same data were stored in a byte array wrapped by a ByteArrayInputStream? Why would either of these be any different than a CoyoteInputStream coming from a web post containing the exact same file? As it turns out I’ve hit a situation where there must be a difference. In my situation I take an input stream from an HTTP response and pass it into a custom object that interprets the response. When I do that my object reports corrupt data. If I take that same stream and copy it into a ByteArrayOutputStream then pass the toBytes() to a ByteArrayInputStream and drop it into my stream reader then the corruption goes away. Here’s kinda what it looks like.

//This doesn't work...
inputStream = getResponseFromPost("http://someserver/some.php", postData);
new MyStreamInterpreter().usingStream(inputStream).interpret(myCallback);

Vs.

//This does work...
inputStream = getResponseFromPost("http://someserver/some.php", postData);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
transferStream(inputStream, memory);
new MyStreamInterpreter().usingStream(new ByteArrayInputStream(memory.toByteArray())).interpret(myCallback);

In case you’re wondering transferStream does a bytewise block copy from source input to destination output. I’m completely puzzled! I thought I’d write more but it’s the end of the day and I gotta bounce like a check written against an account where the owner just got two traffic tickets, a broken down car, a leaky toilet, and a hole in his ceiling. In short… I gotta go.

(In ghetto-slick speak “to bounce” is to make like a tree and gedouddahere! Or more professionally stated, to remove ones self from the current location and begin travel to an alternate location, most likely ones homestead.)

4 thoughts on “InputStream != InputStream

  1. For the most part you can assume (and even replace with) getResponseFromPost() merely opens a URL connection retuning an image or some other multimedia file. The part that breaks is slurping the stream using block reads and passing those blocks into a callback. I’d like to post more of the code but the part where I believe the problem lies is considered company IP. I’m gonna try isolating it to a few lines of code which would likely reveal the problem making the exercise of discovery irrelevant. In short, I’m just venting about a problem that exists between two systems that I couldn’t solve or understand in the course of an 8 hour day of continuous interruptions.

  2. And hey Mr. WordPress! Where is this new blog now? Are you relocating your blogspot account? I can’t click the bunny or the name to find it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s