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);
//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.)