A large part of the network program is a simple input and output, that is, from one system to another system. The byte is byte, to a large extent, the data sent by the read server is different from the read file; it is nothing to send to the customer to write a file.
The input and output organizations in Java are different from most other languages. It is built on stream. Different basic flows (such as java.io.fileinputstream and sun.net.telnetOutputStream) are used to read and write specific data resources. However, all basic output streams read data using the same basic method.
Filter streams can be connected to the input stream or output stream. It can modify data that has been read or written (eg, encrypted or compressed data), or simply provide additional methods to convert data read or written into other formats.
Last Reader and Writer can also be linked to the input stream and output streams, allowing the program to read and write text (ie characters) instead of bytes. If you use the correct, Reader and Writer can handle multiple types of character encodings, including multi-byte character sets such as SJIS and UTF-8.
First, output flow
Java's basic output stream is java.io.outputstream.
Public Abstract Class OutputStream
N Public Abstract Void Write (INT B) THROWS IOEXCEPTION
n public void write (byte [] data) throws oException
N public void write (byte [] data, int offset, int layth) THROWS IOEXCEPTION
N public void flush () THROWS IOEXCEPTION
n public void close () THROWS IOEXCEPTION
Subclasses of OutputStream use these methods to write data to the specified media.
I am using it, we understand what they exist, it will better remember them, ok, now I start talking about the origin of the OutputStream class.
Ø Public Abstract Void Write (INT B) THROWS IOEXCEPTION
The basic method of OutputStream is Write (INT B). This method will consider an integer between 0 and 255 as a variable and write the corresponding byte to an output stream. This method states that it is an abstract method because the subcategory needs to change it to process specific media. For example, ByteArrayoutputStream can implement a copy byte to the pure Java code of its array. However, FileOutputStream needs to use code, this code should understand how to write data on the host platform. Note: Although the method uses the shaped value as a variable, it is actually written in an unsigned byte. Java does not have a symbolic byte data type, so this is used herein. The true difference between the unsigned byte and the symbolic byte is the interpretation of the compiler. Both consists of 8 digits, and only 8 digits are transmitted when using INTs to network connection streams using Write (INT B). If INT exceeding the range of 0-255 is passed to Write (int b), the number of low bytes of the number is written, and the remaining three bytes (everyone knows that Java int is 4 bytes, The essence here is to convert int to BYTE).
Ø public void write (byte [] data) throws oews oException and public void write (byte [] data, int offset, int ingth) throws oException
Each time you write a byte is usually very efficient. Therefore, most of the TCP / IP programs store data into a certain length of buffer, that is, cumulative bytes in memory, and send them to the end only when accumulating a certain number of bytes or a certain period of time. destination. So Write (byte [] data) and Write (byte [] DATA, INT Offset, INT LENGTH) are produced. Ø public void flush () THROWS IOException
We can perform buffering operations in software or directly in the Java code, or perform buffering operations in network hardware. It seems that bufferedoutputstream or bufferedWriter links to the bottom stream to achieve stream buffering. Therefore, if the data is being written, the refresh output stream is quite important. For example, assume that a 300-byte request has been written to an HTTP server HTTP server, which is typically wishes to wait for a response between sending more data. However, if the output stream has a 1024-byte buffer, the stream may wait for more data to arrive before sending the data buffer, but these data do not seem to be reached because they have not yet been sent. However, the buffering stream does not send data to the server unless it obtains more data from the underlying flow, but the underlying flow does not send more data unless it obtains data from the server, the server does not send data unless it receives retained In the buffer (dead lock!), The flush () method can solve this deadlock because even if the buffer is not full, he will force the stream of stream transfer data. Note: Whether buffering operation is implemented, which is determined on how you get a reference to streams (for example, whether you want to perform buffering operations for System.out, you will perform buffer). If the refresh stream needs to be refreshed, it must be refreshed, but if the refresh fails will cause unpredictable, the non-repetitive program hangs (Flush () return value is a void), if you don't understand the hang problem, it is very It is difficult to solve this problem. Therefore, they should be refreshed immediately before closing all streams. Otherwise, the remaining data in the buffer may be lost before shutting down the stream.
Ø public void close () THROWS IOException
Finally, after using the end, the close () method should be called to close the stream. It will release all resources related to this stream, such as file handles or ports. Once the output stream is turned off, it is triggered to write data to it. However, some types may allow for a certain operation of the object. A closed ByTearRayoutputStream can still be converted into an actual byte array, and a closed DigestOutputStream can still return a summary.
Second, input flow
Java's basic input is java.io.inputstream
Public Abstract Class InputStream
n public abstract int ied () throws oException
n public int in (byte [] data) throws oException
N public int in (byte [] data, int offset, int length) THROWS IOEXCEPTION
N Public long Skip (long n) THROWS IOEXCEPTION
N public int available () throws oException
n Public Void Close () THROWS IOEXCEPTIONINPUTSTREAM The specific subclasses use these methods to read data from the specified media. But regardless of the resources read, almost these six methods can be used. Sometimes you may even know what type of stream is reading data. If TelnetInputStream is a class that is not explained in a document in the Sun.NET package. Examples of TelnetInputStream are returned by a variety of methods in the java.net package; for the openstram () method of java.net.URL. However, these methods only declare return inputStream, rather than more clear subclass TelNetinputStream, which is also a polymorphism. Examples of subclasss can be used as an example of a superclass.
Come, it will also explain the origin of the method.
Ø Public Abstract void read () throws oException
The basic method of the InputStream class is a READ () method (this is different from OutputStream) without a parameter. The method returns a single byte data from the input stream resource and returns the number of data between 0 and 255, and returns -1 indicates the end of the stream. Because Java has no symbolic data types, the data is returned in an integer type. The READ () method is waiting and blocks the execution of the method and the code of the method until one byte of the data is obtained and the byte is ready to read. Therefore, the input and output may be relatively slow, and if the user needs to complete other important tasks, it is best to try to put I / O in their own thread. The read () method is declared as an abstract method because the child needs to change it to handle a particular medium. Give an example
BYTE [] Input = New byte [10];
For (int i = 0; i INT b = in.read (); IF (b == - 1) Break; INPUT [I] = (Byte) B; } Although the READ () method only reads bytes, it returns an integer value. Therefore, a type conversion process is required before the result is stored in the byte array. Of course, this generates a symbolic byte between -128 to 127 instead of the 0 to 255 between 0 and 255 returned between the -128 to 127. However, there will be no problem with the user clearly uses unsigned or symbolic bytes. Therefore, we can convert an symbolic byte into an unsigned byte (the reason for the conversion is only the integer range of 0-255 can be stored in a BYTE type of Java). INT i = B> = 0? B: 256 B; Here, you have a big unit, indicating that read () returned to the Java's Byte type processing problem, you have to pay attention to A. If you are interested in Java's original data type, you can take a look at my original data type learning note ( undone). Ø public int ø (byte [] data) throws oews oException, public int ie, int ioException Every time you read a byte and a write-byte efficiency is not high, read (byte [] data) and read (byte [] data, int offset, int length) will also be generated. Both methods fill into a plurality of bytes read from the stream into a specified array. Note: These fills to array do not necessarily succeed. A very common situation is that a reading attempt does not fail completely, it will not be completely successful, it may read a part of the byte of the request data, not all bytes. For example, when only 512 bytes have arrived at the server, the user may try to read 1024 bytes from a network stream, while other bytes are still transmitted, these bytes will eventually reach the server, but arrive It is already not available. Therefore, multi-byte reading methods returns the number of bytes actually read. Give an example byte [] input = new byte [1024]; INT BYTESREAD = IN.READ (INPUT); The code segment is trying to read 1024 bytes from the InputStream in to the array input. However, if only 512 bytes can be obtained, these bytes are all bytes to be read, and the BytesRead value is set to 512. But what should we do in order to ensure that all bytes actually read? Look INT BYTESREAD = 0; INT bytetoread = 1024; Byte [] input = new byte [bytetoread]; While (bytesRead BYTESREAD = In.read (Input, BytesRead, bytetoread-bytesread); } Ø public int available () THROWS IOEXCEPTION If the user does not want to read data for some reason, unless all of the data you want can be immediately obtained, this time you can use the number of bytes returned by the AVAILABLE () method to read the minimum byte number, and in actual More bytes can be read, but the byte data that can be read is as much as the number of bytes returned to Available (). Look an example Int bytesavailable = in.available (); Byte [] input = new byte [bytesavaila]; INT Bytetead = In.read (Input, 0, Bytesavailable); // Other code Here we can assert bytesread is just equal to bytesavailable, but you can't assert BYTESREAD> 0 because Available () returns 0 is possible. At the end of the stream: Available () returns 0; Read (Byte [] DATA, INT OFFSET, INT LENGTH) Normally returns -1; The flow is not over, the value of the readable byte is 0 when the value of available () is 0. Read (Byte [] DATA, INT Offset, INT Length will ignore the end of the stream, return 0; Ø Public long Skip (long n) THROWS IOEXCEPTION In a very small case, the user may want to skip the data without reading them. The Skip () method is to implement this function. This method is useful when reading data from the file, and is smaller in the network connection. Because the network connection stream is ordered and is usually very slow, the time consumption of reading data will not exceed the time consumption of the skip data. Files can be randomly accessed, so we can simply implement data jump through the relocation file pointer, rather than skip each byte. Ø public void close () THROWS IOException Like the output flow, the program should be called to turn off the input stream after using the input stream, and you should remember. This method releases all resources related to the input stream, such as file handles and ports. Once the input stream is turned off, IOException is triggered when it is read from it. However, some types of streams may still allow for a certain operation of the object. For example, users typically do not want to obtain packet summary from java.security.digestInputStream unless all data has been read and the input stream is turned off. Seeing this per holiday, there is still three ways, yes, there are three unused methods Public void Mark (int ready) Public void reset () THROWS IOEXCEPTION Public Boolean Marksupported (); These methods allow program backups and reread the already read data. To implement this function, you need to mark the current position in the input stream in the input stream. You can use the reset () method to locate the stream to the mark, and then read will return from the initial Start data. However, from the marker to resemble the stream position point cannot be arbitrarily. The number of bytes allowed to be read before repositioning to the marker is determined by the variable readaheadLimit of Mark (). How long will trigger oException, and any specified time, only one tag can be included in the input stream. If the second tag is marked, the first marked will be overwritten. In fact, the markers and reset positions are implemented by storing the markings from the internal buffer to read each byte. The most troublesome situation is that not all input streams support tags and reset location. So use the Marksupported () method before setting. The flow that does not support markers and reset locations support their streams. Elliotte Rusty Harold Master thinks that these methods are not high, and abstract superclairs that can be functional and more may even be unavailable in most subclasses. It is best to put these three methods in different interfaces. Provides functional detection similar to a Marksupported () method in runtime is a more traditional, non-objective solution. Object-oriented methods will embed this method into the object-oriented system through interfaces and classes to detect all streams at compile. Java.io always supports the markup stream: BufferedInputStream and ByTearRayinputStream.