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

Discussion in 'Java' started by Louis, Oct 15, 2003.

  1. Louis

    Louis Guest

    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);
    }
    ..
    ..
    ..
     
    Louis, Oct 15, 2003
    #1
    1. Advertising

  2. Louis

    Paul Lutus Guest

    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
     
    Paul Lutus, Oct 15, 2003
    #2
    1. Advertising

  3. Louis

    Sudsy Guest

    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.
     
    Sudsy, Oct 15, 2003
    #3
  4. Louis

    Roedy Green Guest

    On Tue, 14 Oct 2003 19:47:51 -0700, Paul Lutus <>
    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.
     
    Roedy Green, Oct 15, 2003
    #4
  5. Louis

    Louis Guest

    > 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??
     
    Louis, Oct 15, 2003
    #5
  6. Louis

    Paul Lutus Guest

    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
     
    Paul Lutus, Oct 15, 2003
    #6
  7. Louis

    Sudsy Guest

    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?
     
    Sudsy, Oct 15, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    922
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,916
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    991
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    565
    Laszlo Nagy
    Feb 1, 2009
  5. Steve Holden
    Replies:
    0
    Views:
    683
    Steve Holden
    Feb 1, 2009
Loading...

Share This Page