Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   URGENT: Reading from socket and writing into binary... HOW? (http://www.velocityreviews.com/forums/t127631-urgent-reading-from-socket-and-writing-into-binary-how.html)

Louis 10-15-2003 01:43 AM

URGENT: Reading from socket and writing into binary... HOW?
 
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);
}
..
..
..

Paul Lutus 10-15-2003 02:47 AM

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

> 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.

--
Paul Lutus
http://www.arachnoid.com


Sudsy 10-15-2003 03:15 AM

Re: URGENT: Reading from socket and writing into binary... HOW?
 
Louis wrote:
> 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.


Roedy Green 10-15-2003 05:11 AM

Re: URGENT: Reading from socket and writing into binary... HOW?
 
On Tue, 14 Oct 2003 19:47:51 -0700, Paul Lutus <nospam@nosite.zzz>
wrote or quoted :

>
>PrintWriter? You need to read about the different IO classes and their
>purposes.


Easiest way to learn is to play with the file i/o amanuensis and see
the code it generates for various i/o problems.

see http://mindprod.com/fileio.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Louis 10-15-2003 04:58 PM

Re: URGENT: Reading from socket and writing into binary... HOW?
 
> 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??

Paul Lutus 10-15-2003 05:27 PM

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

< snip >

> 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.

--
Paul Lutus
http://www.arachnoid.com


Sudsy 10-15-2003 07:30 PM

Re: URGENT: Reading from socket and writing into binary... HOW?
 
Louis wrote:
<snip>
> 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?



All times are GMT. The time now is 08:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.