Java performance optimization Series 5-JavaIO

Source: Internet
Author: User
Tags array to string

1. Java serialization and deserialization:

(1) role:

1. The object state is saved to the local machine, so that the stored serialized bytes can be directly read when the VM is started next time, instead of initializing the object; 2. Implement object network transmission (RMI distributed object); 3. Implement object deep copy.

1. Object serialization can implement distributed objects. Main Applications: RMI uses objects to serialize services on remote hosts, just like running objects on local hosts.

Ii. java object serialization not only retains the data of an object, but also recursively stores the data of each object referenced by the object. The entire Object layer can be written to the byte stream, stored in a file or transmitted over a network connection. Object serialization allows you to perform "Deep replication" on an object, that is, copying the object itself and the referenced object itself. Serializing an object may obtain the entire object sequence.

(2) Basic Methods:

ObjectOutputStream can only serialize the class objects of the Serializable interface. By default, ObjectOutputStream is serialized by default. This serialization method only serializes non-transient instance variables of the object, instead of the transient instance variables of the object, static variables are not serialized.

When ObjectOutputStream is deserialized by default, it has the following features:

1) if the class to which the object belongs in the memory is not loaded, the class will be loaded and initialized first. If no corresponding class file exists in classpath, ClassNotFoundException is thrown;

2) during deserialization, no constructor of the class is called.

If you want to control the serialization method of the class, you can provide the following writeObject () and readObject () methods in the serializable class.

Private void writeObject (java. io. ObjectOutputStream out) throws IOException

Private void readObject (java. io. ObjectInputStream in) throws IOException, ClassNotFoundException;

When ObjectOutputStream serializes a Customer object, if the object has a writeObject () method, this method is executed. Otherwise, the method is serialized by default. In the writeObjectt () method of the object, you can first call the defaultWriteObject () method of ObjectOutputStream so that the object output stream executes the default serialization operation first. Likewise, deserialization can be obtained, but this time it is the defaultReadObject () method.

Some objects contain sensitive information, which should not be made public. If they are serialized by default, their serialized data may be stolen by criminals during network upload or transmission. This type of information can be encrypted and then serialized. During deserialization, it needs to be decrypted before being restored to the original information.

The default serialization method serializes the entire object graph, which requires recursive traversal of the object graph. If an object graph is complex, recursive traversal consumes a lot of space and time. Its internal data structure is a bidirectional list.

In an application, if some member variables are changed to the transient type, it will save space and time and improve the serialization performance.

|-1. The object implements the seriable interface and custom seriousid.

|-2. ObjectOutputStream out = new ObjectOutputStream (baos );

Out. writeObject (new PersonDemo ("rollen", 20 ));

Out. close ();

|-3. ByteArrayInputStream bais = new ByteArrayInputStream (baos. toByteArray ());

ObjectInputStream input = new ObjectInputStream (bais );

Object obj = input. readObject ();

Input. close ();

(3) Java custom serialization deserialization: the reason why the methods of readObject () and writeObject () of seriliable object classes are re-written:

Some objects contain sensitive information, which should not be made public. If they are serialized by default, their serialized data may be stolen by criminals during network upload or transmission. This type of information can be encrypted and then serialized. During deserialization, it needs to be decrypted before being restored to the original information. In this case, the default readObject and writeObject () methods cannot be used.

Private void writeObject (java. io. ObjectOutputStream out) throws IOException {

Out. defaultWriteObject ();

Out. writeUTF (name );

}

Private void readObject (java. io. ObjectInputStream in) throws IOException, ClassNotFoundException {

In. defaultReadObject ();

Name = in. readUTF ();

}

In general, the Serializable interface can be directly implemented to achieve serialization requirements, but in some cases, some special requirements need to be made for serialization.

(4) role of the Transits Keyword: block member variables that do not want to be serialized. The unblocking method can be used. (3)

(5) Role of Externalize:

The Externalizable interface inherits from the Serializable interface. If a class implements the Externalizable interface, this class controls its own serialization behavior completely. The Externalizable interface declares two methods:

Public void writeExternal (ObjectOutput out) throws IOException

Public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException

The former is responsible for serialization, and the latter is responsible for deserialization.

When deserializing objects of classes that implement the Externalizable interface, the class construction method without parameters is called first, which is different from the default deserialization method. If you delete a class constructor without parameters, or set the access permission of the constructor to private, default, or protected, java is thrown. io. invalidException: no valid constructor exception.

(6) Relationship with Java constructor:

When the object of the class implementing the Externalizable interface is deserialized, the constructor without parameters of the class is called first. When the object of the class implementing the Serializable interface is deserialized, no constructor is called. Only re-build the object in the memory based on the status information of the saved object!

(7) considerations:

1) When serialization is run, a version number called serialVersionUID is associated with each serializable class, in the deserialization process, this serial number is used to verify whether the sender and receiver of the serialized object have loaded classes compatible with serialization for this object. Assign a clear value to it. SerialVersionUID can be explicitly defined for two purposes:

In some cases, different versions of the class are required to be compatible with serialization. Therefore, make sure that different versions of the class have the same serialVersionUID;

In some cases, different versions of the class are not required to be compatible with serialization. Therefore, make sure that different versions of the class have different serialVersionUID.

2) java has many basic classes that have implemented the serializable interface, such as string and vector. For example, hashtable does not implement the serializable interface.

3). Not all objects can be serialized. For security reasons, an object has private, public, and other fields. For an object to be transmitted, such as writing to a file, or performing rmi transmission, during serialization and transmission, the private and other fields of this object are unprotected. The reasons for resource allocation, such as socket and thread classes, can be serialized, transmitted, or saved, and there is no need to re-allocate resources for them.

4) When deserializing an object, it does not call any construction method of the object. It just re-constructs the object in the memory based on the state information of the saved object!

5) when an object is serialized, only non-static member variables of the object are saved, and no member methods and static member variables can be saved.

6) If the member variable of an object is an object, the data member of this object will also be saved! This is an important reason why deep copy can be solved through serialization.

(8) solution to the conflict between serialization and Singleton mode:

There are also two custom serialization Methods writeReplace and readResolve, which are used to replace the serialized object before serialization and process the returned object after deserialization. It can be used to prevent singleTon objects from generating multiple object instances during cross-jvm serialization and deserialization. In fact, once a singleTon object can be serialized, it cannot guarantee singleTon. In the Enum Implementation of JVM, The readResolve method is rewritten. The JVM ensures that the Enum value is singleTon. Therefore, we recommend that you use Enum instead of writeReplace and readResolve.

Java code

Private Object readResolve ()

{

Return INSTANCE;

}

Private Object writeReplace (){

Return INSTANCE;

}

Note: writeReplace is called before writeObject; readResolve is called after readObject.

(9) serialization solves deep copy code:

Public Object deepClone () throws IOException, OptionalDataException,

ClassNotFoundException {

// Write the object to the stream

ByteArrayOutputStream bo = new ByteArrayOutputStream ();

ObjectOutputStream oo = new ObjectOutputStream (bo );

Oo. writeObject (this); // read from the stream

ByteArrayInputStream bi = new ByteArrayInputStream (bo. toByteArray ());

ObjectInputStream oi = new ObjectInputStream (bi );

Return (oi. readObject ());

}

The class to which the object belongs must implement the Serializable interface. At the same time, this method is written to the class to which the object belongs.

You can call this method for deep copy.

2. decoration mode in JavaIO:

The most widely used decorator pattern in Java is the JavaIO class design. For example, OutPutStream is the base class of the output stream. Its sub-classes include FileOutputStream and FilterOutputStream, while FilterOutputStream sub-classes include BufferedOutputStream and DataOutputStream. Among them, FileOutputStream is the core class of the system, which implements the function of writing data to a file, dataOutputStream can be used to add write operation support for multiple data types based on FileOutputStream (functions such as writeUTF and writeInt in the DataOutputStream class), while BufferdOutputStream can be used to buffer FileOutputStream, optimized I/O performance.

3. Use Cases of JavaIO stream:

(1) IO stream: used to process data on the device. The device here refers to files, memory, keyboard input, and screen display on the hard disk.

(2) byte streams and byte streams: byte streams are easy to understand, because all files in the format of bytes are stored on the hard disk, including images, MP3, avi, etc, therefore, byte streams can process all types of data. When reading one or more bytes (the number of bytes corresponding to the Chinese character is two, three bytes in the UTF-8 code table), first query the specified encoding table, returns the characters found. The reason why the ghost stream appears is because of the different file encoding, and the ghost Stream object that efficiently operates on characters. Therefore, as long as the plain text data is processed, the use of the byte stream should be given priority, in addition to the byte stream.

(3) Basic Rules of stream operations:

1) Clarify the data source and data sink, with the goal of specifying whether to use an input stream or an output stream.

2) Determine whether the operated data is plain text data.

(3) whether to convert byte streams and byte streams.

4) Whether cache is required.

(4) The instance describes the basic flow of stream operations: the data read on the keyboard is saved to the file with the specified encoding.

1) understand the data source: keyboard input, System. in, available InputStream and Reader

2) It is found that the stream corresponding to System. in is a byte read into the stream, so we need to convert it to a character.

3). Use InputStreamReader to convert the stream.

4) if you want to improve efficiency and add a cache mechanism, you need to add a buffer for the upstream stream. BufferedReader. Therefore, the input stream constructed in the first four steps is:

BufferedReader bur = new BufferedReader (new InputStreamReader (System. in ));

5) understand data aggregation: since it is a data aggregation, it must be an output stream. You can use OutputStream or Writer.

6). All text files are stored in the file. Therefore, use Writer.

7) because you need to specify the encoding table, use the conversion stream in Writer, OutputStreamWriter.

Note: although the object is eventually a file, you cannot select FileWriter because the object uses the default encoding table.

8) Whether to improve efficiency, select BufferedWriter.

9) The conversion output stream needs to receive a byte output stream. Therefore, if OutputStream is used, it is finally output to a file. You need to use FileOutputStream, the volume Stream object of files that can be operated in the OutputStream system.

10) the output stream object obtained through the preceding analysis is as follows:

// String charSet = System. getProperty ("file. encoding ");

String charSet = "UTF-8 ";

BufferedWriter bufw = new BufferedWriter (new OutputStreamWriter (new

FileOutputStream ("a.txt"), charSet );
4. Set object Properties that can be associated with the stream.

Map

| -- HashTable

| -- Properties

Properties: This set does not require generics, because the key values in this set are of the String type.

5. other stream objects:

(1) print the stream:

PrintStream: The System. out of a byte print stream corresponds to PrintStream. Its constructor can receive three data types of values: String path, File object, and OutputStream (when it is System. out, the input is displayed on the screen)

PrintWriter: A character print stream. Constructors can receive four types of values. String path and File object (for these two types of data, you can also specify the encoding table. That is, Character Set), OutPutSream, and Writer (for data of Type 3 and 4, you can specify automatic refresh. Note: when the value of automatic refresh is true, only three methods can be used: printlf, printf, format)

(2) pipeline streams: PipedOutputStream and PipedInputStream. It is generally used for multi-thread communication.

(3) RandomAccessFile: This object is not a member of the stream system. However, this team selects and encapsulates the byte stream and a buffer (byte array ), use internal pointers to manipulate data in the array. This object features that only files can be operated and files can be read and written. It is mostly used for multi-threaded download. ,

(4) Merge streams: Multiple read streams can be merged into one stream. In fact, each read Stream object is stored in a collection, and the end of the last Stream object is the end of the stream.

(5) Object serialization. ObjectInputStream and ObjectInputStream.

(6) stream objects of basic data types: DataInputStream and DataOutputStream.

(7) stream objects that manipulate the memory array. The data source of these objects is memory, and the data sink is also memory: ByteArrayInputStream and ByteArrayOutputStream, CharArrayReader and CharArrayWriter. These streams do not call system resources and use an array in the memory. Therefore, you do not need to close them when using them.

(8) encoding conversion:

In IO, the stream involved in encoding conversion is the conversion stream and the print stream, but the print stream only outputs. You can specify the encoding table for the conversion stream. By default, the conversion stream is the local default encoding table, GBK. You can get it through: Syetem. getProperty ("file. encoding. The process from string to byte array to encoding is completed by getBytes (charset). The process from byte array to string is a process of decoding, string (byte [], charset) is completed through the String class constructor ).

(9) encoding example and analysis:

(10) The Charset class of JavaNIO is specially used for encoding and decoding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.