URGENT: Reading from socket and writing into binary... HOW?

L

Louis

Hi all,

In fact, I am working on a http client assignment. Text file is ok,
but binary is failed (i try for a gif, but the image get distorted.
Original file size is the same to download one, but md5sum are
different.

Can anyone have a look on my code? Where goes wrong?
- What 'in' should I use? I need to 'readLn' for header and then
'read/readByte' for binary...
- What file output I need? I heard that FileWriter is not appropiate?

server = new Socket(url.getHost(), (url.getPort() == -1 ?
DEFAULTHTTPPORT : url.getPort()));
out = new PrintWriter(server.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader(server.getInputStream()));
..
..
..
tempfile = File.createTempFile(tempprefix, tempsuffix);
fw = new FileWriter(tempfile);
int c;
while ((c = in.read()) != -1) {
fw.write(c);
}
..
..
..
 
P

Paul Lutus

Louis said:
Hi all,

In fact, I am working on a http client assignment. Text file is ok,
but binary is failed (i try for a gif, but the image get distorted.
Original file size is the same to download one, but md5sum are
different.

Can anyone have a look on my code? Where goes wrong?
- What 'in' should I use? I need to 'readLn' for header and then
'read/readByte' for binary...
- What file output I need? I heard that FileWriter is not appropiate?

server = new Socket(url.getHost(), (url.getPort() == -1 ?
DEFAULTHTTPPORT : url.getPort()));
out = new PrintWriter(server.getOutputStream(), true);

PrintWriter? You need to read about the different IO classes and their
purposes.
in = new BufferedReader(new
InputStreamReader(server.getInputStream()));
.
.
.
tempfile = File.createTempFile(tempprefix, tempsuffix);
fw = new FileWriter(tempfile);

Filewriter, for a GIF image? FileWriter is for writing out characters, not
bytes.
int c;
while ((c = in.read()) != -1) {
fw.write(c);
}

Also, this is very slow. Try creating a buffer to allow reading and writing
larger blocks.
 
S

Sudsy

Louis said:
Hi all,

In fact, I am working on a http client assignment. Text file is ok,
but binary is failed (i try for a gif, but the image get distorted.
Original file size is the same to download one, but md5sum are
different.

Can anyone have a look on my code? Where goes wrong?
- What 'in' should I use? I need to 'readLn' for header and then
'read/readByte' for binary...
- What file output I need? I heard that FileWriter is not appropiate?

The underlying problem is that you're using the wrong classes.
One of the beauties of Java is that there are all these libraries
available to you. Digging through the documentation can be most
rewarding.
Take a look at java.net.HttpURLConnection, for instance. It's
specifically designed to handle all the information which comes
back from an HTTP GET or POST operation. Check out the methods:

getResponseCode,
getResponseMessage,
getContentEncodingType, and, most important to you,
getContent

(the last two are inherited from java.net.URLConnection).

FileWriter is also inappropriate as it is designed (as the
documentation specifies) for "character files".
Remember the basic rule: Readers/Writers for characters,
InputStreams/OutputStreams for bytes. Use a FileOutputStream
and your content shouldn't get munged.
I'll leave the actual coding up to you. This is supposed to
be an assignment, after all.
 
L

Louis

getResponseCode,
getResponseMessage,
getContentEncodingType, and, most important to you,
getContent
(the last two are inherited from java.net.URLConnection).

ok... the assignment is to write socket. so i shouldn't use above to
do so, right?
FileWriter is also inappropriate as it is designed (as the
documentation specifies) for "character files".
Remember the basic rule: Readers/Writers for characters,
InputStreams/OutputStreams for bytes. Use a FileOutputStream
and your content shouldn't get munged.
I'll leave the actual coding up to you. This is supposed to
be an assignment, after all.

ok... i understand my problem...
- i have 3 streams for my client
a) 1x FileWriter for socket InputStream
b) 1x DataOutputStream (or BufferedReader) for socket OutputStream*
c) 1x FileOutputStream for output the bytes stream to file

Fact: Writer/Reader for character and Stream for bytes.

a) should be ok. I need character Writer to send HTTP request
c) should be ok. I output the data as is (binary)
b) is my problem. BufferedReader.readln() could read those HTTP header
per line, but it can't read the content follows; DataOutputStream is
ok for content, but no method for reading line.

I am lazy and try to use BufferedReader to get header and then use
DataOutputStream, but the switching never work... I think I need to
use DataOutputStream only and construct a method for reading line
myself.

Is that correct??
 
P

Paul Lutus

Louis wrote:

I am lazy and try to use BufferedReader to get header and then use
DataOutputStream, but the switching never work... I think I need to
use DataOutputStream only and construct a method for reading line
myself.

Is that correct??

Well, that is a simpler approach overall. Just read until you encounter the
expected line ending character(s), then create a String out of the
intermediate contents of the buffer. Then, if this scheme works as
expected, build a state machine around this idea so Strings are created
when that is appropriate and blocks of binary data when that is
appropriate.
 
S

Sudsy

Louis wrote:
ok... the assignment is to write socket. so i shouldn't use above to
do so, right?
<snip>

Now I really don't understand what it is you're trying to accomplish.
I thought you were just trying to read an image and save it to a file.
That's why I'd do something like this:

URL url = new URL( "http://someplace/someimage" );
HttpURLConnection conn = (HttpURLConnection) url.getConnection();
conn.setDoOututFalse();
conn.connect();
// use the various getHeaderField methods on conn to get headers
InputStream is = conn.getInputStream();
// read from is and write to a file

Am I missing something major? Are you trying to duplicate the
HTTP functionality in your program?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top