Results 1 to 5 of 5

Thread: BufferedReader

  1. #1
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Question BufferedReader

    Can someone explain what exactly these sentences do? :)

    Java Code:
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    client.getInputStream( ) ) );
    
    
    PrintWriter out = new PrintWriter(
    new BufferedWriter(
    new OutputStreamWriter(
    client.getOutputStream( ) ) ),true );

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nerijus View Post
    Can someone explain what exactly these sentences do?
    Look up the constructors for all the used classes and it becomes clear; the IO system uses the 'decorator' or 'wrapper' pattern, e.g your client can deliver an InputStream, when you wrap that in an InputStreamReader you have a Reader, when you wrap that in a BufferedReader you have a BufferedReader. Similar reasoning applies to the wrapping of the output.

    kind regards,

    Jos

  3. #3
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    thanks :) and can you explain why client read message from server is this way:
    Java Code:
    System.out.println(in.readLine());
    and send message to server like that:
    Java Code:
    out.println("Hi");
    What is the difference between these methods:
    Java Code:
    OutputStream outputStream = client.getOutputStream();
    InputStream inputStream = client.getInputStream();
    
    //Sends message:
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       
            String line = reader.readLine();
            
            byte[] msg = line.getBytes();
       
            outputStream.write(msg);
    //Reads message:
            int bytesRead = inputStream.read(info);
    
            byte[] temp = Arrays.copyOfRange(info, 0, bytesRead);
    
            System.out.println("\nRecieved "+ bytesRead +"bytes");
            System.out.println("Recieved "+ new String(temp));

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    The difference is this:

    OutputStream (or Writer) write into the stream RIGHT AWAY. So if you do

    Java Code:
    outputStream.write(bytes1);
    outputStream.write(bytes2);
    outputStream.write(bytes3);
    this will result in 3 separate writes into the output stream. The time it takes to write into the stream is SETUP + CONSTANT * message size. In this case, the total time is 3 * SETUP + CONSTANT * total message size.


    Now suppose you were using BufferedOutputStream (or BufferedWriter). when you call


    Java Code:
    bufferedOutputStream.write(bytes1);
    bufferedOutputStream.write(bytes2);
    bufferedOutputStream.write(bytes3);
    it writes into some buffer instead of the stream. The actual writing into the stream will happen later, when the BufferedWriter decided the buffer is large enough so it makes sense to write it. So in effect, it combines multiple writes into one. The time you spend with this approach is SETUP + CONSTANT * total message size, which of course is better.


    One problem you occasionally run into is that BufferedWriter.write() doesn't actually send anything, and you aren't receiving anything at the other end. use BufferedWriter.flush() to force it to actually perform the writing.

  5. #5
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

Similar Threads

  1. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 03:56 PM
  2. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 11:56 AM
  3. how can i make bufferedreader
    By chyeeqi in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 06:24 PM
  4. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:57 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •