Results 1 to 5 of 5
Thread: BufferedReader
- 04-26-2010, 06:42 PM #1
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
- 04-26-2010, 06:51 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-26-2010, 07:06 PM #3
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
thanks :) and can you explain why client read message from server is this way:
and send message to server like that:Java Code:System.out.println(in.readLine());
What is the difference between these methods:Java Code:out.println("Hi");
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));
- 04-26-2010, 07:15 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
The difference is this:
OutputStream (or Writer) write into the stream RIGHT AWAY. So if you do
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.Java Code:outputStream.write(bytes1); outputStream.write(bytes2); outputStream.write(bytes3);
Now suppose you were using BufferedOutputStream (or BufferedWriter). when you call
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.Java Code:bufferedOutputStream.write(bytes1); bufferedOutputStream.write(bytes2); bufferedOutputStream.write(bytes3);
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.
- 04-26-2010, 07:28 PM #5
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
How to use BufferedReader?
By ProgramBeginner in forum New To JavaReplies: 4Last Post: 10-11-2011, 02:56 PM -
BufferedReader, need help!
By zacharyrod in forum New To JavaReplies: 10Last Post: 11-19-2009, 10:56 AM -
how can i make bufferedreader
By chyeeqi in forum New To JavaReplies: 4Last Post: 08-21-2009, 05:24 PM -
BufferedReader
By vidhya.sk in forum New To JavaReplies: 2Last Post: 09-18-2008, 01:57 PM -
BufferedReader empty
By Peter in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks