Sunday, June 5, 2011

Operations on stream (Stream)

Operations on stream (Stream)

Basic class I / O Reader, Writer, InputStream, and OutputStream provide only operations I / O is very basic. For example, InputStream class has instance methods
public int read () throws IOException

to read one byte of data from the input stream. If until the end of the input stream, the method read () will return the value -1. If there are errors that occur when taking input, then an exception IOException will be thrown. Because IOException is a class exemption that must be dealt with, meaning we must use the method read () inside the try statement or set subroutine throws IOException. (See again the discussion of exceptions in the previous chapter)

InputStream class also has methods to read a few bytes of data in a single step into an array of bytes. However, InputStream does not have a method to read the other data types, like int or double from the stream. This is not a problem because in practice we will not use object of type InputStream directly. We will use is a subclass of InputStream that have multiple input methods are more diverse than the InputStream itself.

So is the OutputStream class has a primitive output method for writing one byte of data to the output stream, the method
public void write (int b) throws IOException

But, we almost certainly will use derivatives class capable of handling more complex operations.

Reader and Writer classes have almost the same basic operations, namely read and write, but this class-oriented character (because it is used to read and write data to human readable). This means that read and write operations will take and write char values ​​rather than bytes. In practice we will use a class derived from these base classes.

One interesting thing from the package I / O in Java is a possibility to increase the complexity of a flow stream by wrapping it in another stream object. This wrapper object is also a stream, so that we can also read and write from the same object with additional capabilities in the object wrapper.

For example, PrintWriter is a subclass of Writer that has additional methods for writing Java data type in characters that can be read of Human. If we have an object of type Writer or their derivatives, and we want to use the method on the PrintWriter to write data, then we can wrap the object in the object Writer PrintWriter.

Example if baskomKarakter of type Writer, then we can make
PrintWriter printableBaskomKarakter = new PrintWriter (baskomKarakter);

When we write data to printableBaskomKarakter PrintWriter method on more sophisticated, then the data will be placed in the same place as when we write directly on baskomKarakter. That means we only need to create a better interface for the same output stream. Or in other words for example we could use a PrintWriter to write the file or send data on the network.

For details, methods of PrintWriter class has the following methods:
/ / Method to write data in
/ / Human readable form
public void print (String s)
public void print (char c)
public void print (int i)
public void print (long l)
public void print (float f)
public void print (double d)
public void print (boolean b)

/ / Write a new line to flow
public void println ()

/ / This method is the same as above
/ / But the output is always
/ / Plus the new row
public void println (String s)
public void println (char c)
public void println (int i)
public void println (long l)
public void println (float f)
public void println (double d
public void println (boolean b)

Note that the above methods do not ever throw an exception IOException. However, the PrintWriter class has a method
public boolean checkError ()

which will return true if there are errors that occur when writing into the stream. PrintWriter Class IOException catch exceptions internally, and set a specific value in this class if an error has occurred. So that we can use the PrintWriter methods without worrying should catch exceptions that may occur. However, if we want to make the program a tough course, we must always call checkError () to see if an error has occurred when we use one method of PrintWriter.

When we use PrintWriter methods to write data to a stream, the data is converted into a series of characters that can be read by humans. How do if we want to make the data in the form of machine language?

Java.io package has a byte stream classes, namely DataOutputStream can be used to write a data into a stream in binary format. DataOutputStream closely related to the OutputStream as the relationship between PrintWriter and Writer.

That is, OutputStream only contains basic methods for writing bytes, DataOutputStream has methods while writeDouble (double x) to write a double value, writeInt (int x) to write the value of int, and so on. And also we can wrap an object of type OutputStream or its derivatives into the stream DataOutputStream so that we can use more complex methods.

For example, if baskomByte is a variable of type OutputStream, then
BaskomData = new DataOutputStream DataOutputStream (baskomByte);

to wrap baskomByte in baskomData.

To retrieve data from the stream, has java.io DataInputStream class. We can wrap an object of type InputStream or their derivatives into an object of type DataInputStream. Methods in the DataInputStream for reading binary data can be used readDouble (), readInt () and so on. Data written by DataOutputStream is guaranteed to be read back by the DataInputStream, although we write data on one computer and read data on other types of computers with different operating systems. Compatibility binary data in Java is one of the benefits to be dijalakan Java on a variety of platforms.

One sad fact about Java is that the Java does not have the class to read data in a form that can be read by humans. In this case, Java does not have the class PrintWriter as opposed to DataOutputStream and DataInputStream. However, we still can make the class itself and use it in a way that exactly the same as classes above.

PrintWriter Class, DataInputStream and DataOutputStream allows us to input and output of all primitive data types in Java. The question is how we read and write an object?

Maybe we would traditionally make their own function to format our object into a certain shape, such as the sequence of primitive types in binary or character is then stored in a file or sent over the network. This process is called serialization (serializing) the object.

On input, we should be able to read serialized data are consistent with the format used at the time of this object serialized. For small objects, this kind of work may not be a big problem. However, for large object sizes, this is not easy.

However, Java has a way to input and output of content objects automatically, ie by using ObjectInputStream and ObjectOutputStream. These classes are subclasses of InputStream and OutputStream that can be used to read and write objects that are serialized.

ObjectInputStream and ObjectOutputStream is a class that can be covered by other classes InputStream and OutputStream. This means we can do input and output byte stream object on anything.

Metde to the object I / O is the readObject () which is available on the ObjectInputStream and writeObject (Object obj) available in ObjectOutputStream. Both can throw IOException. Remember that the readObject () returns a value of type Object, which means must be type cast to type real.

ObjectInputStream and ObjectOutputStream only work for objects that implement the interface named Serializable. Lbih far all the instance variables of the object must be serialized, because the methods of Serializable interface does not have anything. This interface exists only as a marker for the compiler so the compiler knows that this object is used to read and write to the media.

What we need to do is add "implements Serializable" to the class definition. Many of the Java standard class that has been declared to be serializable, including all components of Swing and AWT classes. This means that GUI components can be stored and read from the device I / O using ObjectInputStream and ObjectOutputStream.

No comments:

Post a Comment

 
THANK YOU FOR VISITING