Scrambled/Cooked data over HTTP connection.

I’ve been doing some small experiments with applets and I’ve run into some trouble. I’m sure there must be a really simple solution to this.

I have a binary file on a webserver and am using the following code to access it from an applet:

url = new URL( getDocumentBase(), getParameter( “filename” ) );
InputStream i = url.openStream();
// … Read bytes and close stream

The problem is the data I receive is scrambled. I’m not sure exactly how the scrambling is done but I assume it’s being “cooked” (inappropriate ascii cr/lf conversion).

The file is not scrambled when I download it using a web browser so there’s no problem with the source. The content-type is “audio/mod”. My guess is java doesn’t understand this and is treating it as ascii.

I have tried getting a URLConnection and manually setting the “content-type” request property to “application/octet-stream” but that didn’t help. Maybe this is only used for sending data?

Hope someone can help!

Martin

There is no suc thing. You are thinking of FTP, which has a (damn stupid, with hindsight) cr/lf conversion.

What are the full headers that come back from the server, and exactly how many bytes are there in the donwload compared to the original?

We had a problem with ‘jars’ being sent from a PHP script, the PHP screwed up the headers when the transfer was large enough to be made in chunks.

The only way to really track down the offending party is using a packet sniffer and compare the received data from the browser and java. If the data is identical in both cases, then it is your java failing to read the stream. if it is different (though it shouldn’t be) then it could be a server or PHP setting (or similar).

First step must be to find out which end is going screwy.

  • Dom

I figured it out eventually!

I was parsing the file using the read() methods of a basic InputStream. I didn’t realise these were non-blocking calls and not all the data I asked for was guaranteed to be read. :-[

The bug didn’t show up when reading from local files because the IO could keep up with the program. Now I’m using a DataInputStream all is OK.

Thanks for your help!

Martin