Io data stream and Transcoding

Source: Internet
Author: User

From: http://www.oreilly.com.tw/sleepless/java_big5_2.htm
 
I/O memory usage
All current Java Io operations use the stream method, and all the related classes are stored in Java. Io. Extract binary data using the Child class of outputstream, extract binary data using the Child class of inputstream, and extract text data using the Child class of writer, the reader sub-class is used for inputting text information.

You may be surprised: "Is it necessary to handle text and binary in different ways? Isn't text material a binary ?」 No, actually they are very similar. The biggest difference is that inputstream/outputstream will send data in the original way, But Reader/writer willCompositionReader/writer uploads (text) information when necessary. When is it necessary?

Java stream (including Reader and writer) can be connected in tandem. When the reader's data source is another reader, it does not need to be written. When the reader's data source is an inputstream, it will crash. When the writer's data is processed as another writer, the writer's data is processed as an outputstream.

Why? This can be specified. Because zookeeper occurs only at the junction of Reader/inputstream and writer/outputstream, it is precisely because these two types of events are different between inputstreamreader and outputstreamwriter, the second number of constructor is used to specify the number of constructor instances.

Public inputstreamreader (inputstream in, string ENC)
Throws unsupportedencodingexception;
Public outputstreamwriter (outputstream out, string ENC)
Throws unsupportedencodingexception;

Inputstreamreader uploads the encoding method of ENC to Unicode (because the data is sent from the "external" to the "internal ), outputstreamwriter supports converting unicode encoding to ENC (because the data is sent from the "internal" to "external 」). The content of the JRE must all use Unicode character encoding, while the external character encoding is not necessarily. It depends on why. You can use the getencoding () method to find out how inputstreamreader and outputstreamwriter are stored.

Please note: Even if you do not use inputstreamreader and outputstreamwriter, only other reader and writer are used, but these reader and writer are also very likely (but not exactly) is directly or indirectly connected to inputstreamreader and outputstreamwriter. For example, the content of filereader is actually retrieved from fileinputstream through an inputstreamreader, at this time, the inputstreamreader encoding method is to convert the OS text into Unicode (in a traditional Chinese Windows example, "ms950.

If you clearly know that the case you want to commit (or the source/destination of the resource) is using a certain sort method, you can also manually specify the Failover method. However, remember to capture the unsupportedencodingexception that may be caused by the vulnerability and the exception must be handled. You cannot ignore this exception, this is because the JRE may not have attached this sequence table (or your sequence name may have been attached to the sequence ).

Case 1/o
If you are on a Thai version of Windows and want to use the traditional Chinese Text "ms950 simplified", you must manually specify the upload handler instead of using filereader, otherwise, the token cannot be obtained successfully. The method is as follows:

Fileinputstream FCM = new fileinputstream (filename );
Inputstreamreader reader = new inputstreamreader (FCM, "ms950 ");

Then, the reader reads the correct Chinese characters.

Web I/O Networks
If your web program uses TCP, you can obtain the inputstream and outputstream objects through the getinputstream () and getoutputstream () provided by socket. If you are on a Thai version of Windows and want to use the traditional Chinese Text TCP network stream of the ms950 protocol, you can use the above techniques to download it. The method is as follows:

Inputstream is = mysocket. getinputstream ();
Inputstreamreader reader = new inputstreamreader (is, "ms950 ");

If your web program uses UDP, you must convert the Chinese string to (or from) byte bytes. Please refer to the next section "bytes in the string and byte character columns 」.

If your web program uses RMI, you don't have to worry about this part of memory. The string uses Unicode to directly upload images to another JRE on the network, you do not need to renew your subscription.

Keep criminal cases on site
If you don't know how I/O resources are used for source or processing, you 'd better not use reader or writer, however, inputstream and outputstream should be used directly because of the loss or loss of information caused by the reader and writer, it is better to keep the integrity of the information intact and wait for a further solution.

Bytes bytes in the string and byte bytes Column
Java. Lang. String is different from Java string objects. Since Java string objects are in the JRE, Unicode character encoding must be used. If you need to merge string objects and byte bytes columns, you can use:

String (byte [] bytes, int offset, int length, string ENC );

Or

String (byte [] bytes, string ENC );

To use the byte bytes column of the ENC encoded bytes to convert it into a unicode String object. You can also use the string object:

Byte [] getbytes (string ENC)

To convert a String object into a byte bytes column.

In addition, you can also concatenate bytearrayinputstream or bytearrayoutputstream to inputstreamreader or outputstreamwriter to achieve Zookeeper's purpose.

Http://macrochen.blogdriver.com/macrochen/414524.html

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.