Java IO (ix) Other byte stream

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/7063161.html

1. Preface

The previous chapters have introduced all paired (input and output) byte streams in the Java IO Package, and this chapter describes some of the remaining byte streams, including: Linenumberinputstream, Sequenceinputstream, StringBufferInputStream. In JDK8 's version, only the middle sequenceinputstream were not discarded, and the other two were indicated to be obsolete.

2.LineNumberInputStream

This input stream can keep track of the number of rows in the inflow by adding function functions. The line number starts at 0 and increases by 1 each time. By the way, this class exists in JDK1.0. The reason for abandonment is also given in the comments of the class: This class incorrectly assumes that bytes is sufficient to represent characters. In JDK1.1, which is the later version of this class, a better way to manipulate a character stream is to create a new stream of characters that contains a class for calculating line numbers. This is the linenumberreader that appears in the JDK1.1, this section does not speak, is the content of the character stream.

Linenumberinputstream inherits from FilterInputStream, and the class structure is as follows:

As you can see, for the abstract parent class InputStream, it has more than two methods for set and get line numbers. The following is a first look at the basic data flow method implementation.

There are four variables in the structure before the

, and two of them should be well understood, and Mark begins with the InputStream mark () method to record it. LineNumber is the line number of records, pushback what role? Look at the read logic. Pushback by default, when it is -1,-1, it reads one, not 1, and returns the staged value. Read a byte will determine whether it is \ n, is the line number directly add 1, if it is \ r, let pushback read another, if it is \ n reset to-1. This paragraph is very round, the popular saying is: Read () method is reading a byte, we all know that the stream read can not be read, but to judge the time of the line break is troublesome, \ r When you need to determine whether the next is \ n, so need to read a byte, this time to save this byte, In the existence of pushback, pushback is generally-1, which means that directly read one on it, not 1 is said before the judgment is \ r, but the next is not \ n, so save a bit of this non-\ n character, this time directly returned. There is a hole in the top of the place, it judged the \ r will be executed again, because there is no break. This results in the following example:

    @Testpublic void Test () throws IOException {Bytearrayinputstream Bais = new Bytearrayinputstream ("123\r\t\n456\r\n789\r \ n ". GetBytes ()); Linenumberinputstream Lnis = new Linenumberinputstream (Bais); byte[] buffer = new byte[1024]; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (), int length;while ((length = lnis.read (buffer))! =-1) { Baos.write (buffer, 0, length);} System.out.println (New String (Baos.tobytearray ())); System.out.println (Lnis.getlinenumber ());}

There was a blank line. Actually the surface is really \r\t\n, this produces two carriage returns, actually the direct output is also two carriage return. (-。 -)!!! The real reason for the possible obsolescence is that byte cannot represent all characters.

3.SequenceInputStream

A sequenceinputstream represents a logical connection to another input stream. It starts with an ordered input stream, starting from the first until it reaches the end of the file, then reads from the second file, and so on until the last contained input stream reaches the end of the file.

This flow is simple and structured as follows:

is to accept a set of sequential input streams, read the time, read it all.

Like a baton, read one after another, know that all reading is complete.

    @Testpublic void Test2 () throws IOException {Bytearrayinputstream Bais = new Bytearrayinputstream ("Hello". GetBytes ()); Bytearrayinputstream Bais2 = new Bytearrayinputstream (", Zhang San". GetBytes ()); Sequenceinputstream sis = new Sequenceinputstream (Bais, BAIS2); byte[] buffer = new byte[1024]; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (), int length;while ((length = sis.read (buffer))! =-1) {Baos.write (buffer, 0, length);} System.out.println (New String (Baos.tobytearray ()));}

4.StringBufferInputStream

This stream is also an obsolete method, for the reason that this class does not correctly convert characters to bytes. In JDK 1.1, the preferred way to create a stream from a string is through the StringReader class.

This class allows an application to create an input stream where the bytes read are provided by the contents of the string. The application can also use Bytearrayinputstream to read bytes from a byte array. This class uses only the lower 8 bits of each character in the string. The structure is also very simple:

Buffer is the cached string, and count is the length of the string. The Read () method reads the string as bytes:

Although the official gave the two streams are discarded, that is, the conversion between the characters and bytes, but the individual still do not understand where the problem, the coding or know very little, the current biggest confusion is that all flows are 1 for the end marker, c file stream EOF is 1, Why not be afraid of a byte in the middle is exactly-1? Here is an article about this question: http://blog.csdn.net/jkler_doyourself/article/details/5645925. Not fully understood, but-1 is 0xFFFFFFFF, and many streams are &0xff when read, tested as follows:

    @Testpublic void Test3 () {byte[] a = new byte[]{-1}; Bytearrayinputstream bis = new Bytearrayinputstream (a); System.out.println (Bis.read ());}

-1 read out is 255. The root of the problem is not that reading to 1, the main judgment or not, is whether the value can be fetched, or whether it is known to end. This method is to determine whether the flow is over, return-1. In other cases, the &0xff will not appear as-1, 1 is 0xFFFFFFFF. This is also a more important means. Of course, the value does not change, because you turn to byte again and back to 1, and return to int also circumvent this problem, take the 0~255 range of int, so you can use read ()!=-1 to determine the end, and then by the strong reversal byte into the original value. Personally, I understand that. If there is any mistake, please advise.

Java IO (ix) Other byte stream

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.