Monday, October 29, 2012

Character and Byte Streams


ASCII: 1 byte
Unicode: 2 bytes (or 4 bytes)

Character and Byte Streams

The java.io package provides classes that allow you to convert between Unicode character streams and byte streams of non-Unicode text. With the InputStreamReaderclass, you can convert byte streams to character streams. You use the OutputStreamWriter class to translate character streams into byte streams. The following figure illustrates the conversion process:
This figure represents the conversion process

BufferedWriter


In Java, BufferedWriter is a character streams class to handle the character data. Unlike bytes stream (convert data into bytes), you can just write the strings, arrays or characters data directly to file.
package com.mkyong;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class WriteToFileExample {
 public static void main(String[] args) {
  try {
 
   String content = "This is the content to write into file";
 
   File file = new File("/users/mkyong/filename.txt");
 
   // if file doesnt exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
 
   FileWriter fw = new FileWriter(file.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(content);
   bw.close();
 
   System.out.println("Done");
 
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

What does it mean to flush a socket?

There's a certain amount of overhead involved in writing to a network socket and sending data. If data were sent every time byte entered the socket, you'd end up with 40+ bytes of TCP header for every byte of actual data. (Assuming you're using a TCP socket, of course. Other sockets will have different values). In order to avoid such inefficiency, the socket maintains a local buffer, which is usually somewhat over 1000 bytes. When that buffer is filled, a header is wrapped around the data and the packet is sent off to its destination.
In many cases, you don't need each packet to be sent immediately; if you're transferring a file, early data may not be of any use without the final data of the file, so this works well. If you need to force data to be sent immediately, however, flushing the buffer will send any data which has not yet been sent.
Note that when you close a socket, it automatically flushes any remaning data, so there's no need to flush before you close.

your source code is correct and it IS working! think about a buffer like a toilet. only when the toilet is filled with water does flushing it have any impact. 

System.out.println("Enter your choice") automatically flushes the buffer. the "ln" of that method empties the toilet.. 

EX:

double radius = Double.parseDouble(jtf.getText().trim());
toServer.writeDouble(radius);
toServer.flush();//its size is not full a package size yet, but flush it immediately

DataInputStream/DataOutputStream


public class DataInputStream
extends FilterInputStream
implements DataInput
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.
Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8.
Since:
JDK1.0
See Also:
DataOutputStream