In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the usage of reset () and mark () command of InputStream input stream in linux system". In daily operation, it is believed that many people have doubts about the usage of reset () and mark () command of InputStream input stream in linux system. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer your doubts about the use of the reset () and mark () commands of the InputStream input stream in the linux system. Next, please follow the editor to study!
Today to write a read-write program, the application of InputStream's reset method is found to have failed, and then checked the JDK source code, found that BufferInputStream rewrote the parent class FilterInputStream's mark and resetf methods, it has the ability to support mark and reset methods. FileInputStream, on the other hand, does not override these two methods of the parent class InputStream, which does not have the ability of mark and reset methods.
In the JDK source code, write to public synchronized void mark (int readlimit) to mark the current position in the input stream. Subsequent calls to the reset method relocate the stream to the last marked position so that subsequent reads can reread the same bytes.
The readlimit parameter gives the number of bytes the current input stream is allowed to read before the tag position becomes illegal.
Mark is like a bookmark. It is used for markup. When you call reset later, you can go back to the place where mark was passed. The mark method has a parameter, and with this integer parameter, you tell the system that you want the mark to remain valid until so many characters are read. For example, mark (10), when the read () is less than 10 characters, the data that has been read can be read again after the reset () operation. If more than 10 data have been read, the previous data cannot be read correctly after the reset () operation, because the mark tag is invalid at this time.
The following is the default implementation of these two methods in BufferInputStream and the parent class FilterInputStream / / FilterInputStream.java
Public synchronized void mark (int readlimit) {in.mark (readlimit)
}
Public synchronized void reset () throws IOException {in.reset ()
}
Public boolean markSupported () {
Return in.markSupported ()
}
/ / BufferedInputStream.java
Public synchronized void mark (int readlimit) {marklimit = readlimit
Markpos = pos
}
Public synchronized void reset () throws IOException {getBufIfOpen (); / / Cause exception if closedif (markpos < 0)
Throw new IOException ("Resetting to invalid mark"); pos = markpos
}
Public boolean markSupported () {
Return true
} / / support mark operation
FileInputStream does not override the default implementation of the parent class InputStream, which is as follows:
/ / InputStream.java
Public synchronized void mark (int readlimit) {} / / empty implementation does not implement anything public synchronized void reset () throws IOException {throw new IOException ("mark/reset not supported");} / / does not support reset operation
Public boolean markSupported () {
Return false
} / / mark operation is not supported
Here is a simple program for setting up reset and mark.
Import java.io.BufferedInputStream
Import java.io.File
Import java.io.FileInputStream
Import java.io.IOException
Import java.io.InputStream
Public class TestInputStream {
Public void read (InputStream in) throws IOException {if (in = = null) {
Return
}
Int len=0
In.mark (1)
In.read () / / first read
In.reset (); / / can be read again
In.read () / / the result of the second read is the same as the first read. Because only one is read, there is no integer that exceeds the mark setting. So mark is valid}
Public static void main (String [] args) throws IOException {TestInputStream test = new TestInputStream (); String fileName = "F:/Google.txt"
InputStream in1 = new FileInputStream (new File (fileName)); if (! in1.markSupported ()) {
In1 = new BufferedInputStream (in1)
}
Test.read (in1)
Byte [] buf = new byte [100]
While (in1.read (buf, 0, buf.length)! =-1) {System.out.println (buf)
}
System.out.println ("Success!")
}
}
Summary:
Mark
Public void mark (int readlimit)
The general convention of mark is that if method markSupported returns true, the input stream always records all bytes read after calling mark, and is always ready to provide the same bytes again when method reset is called (whenever). However, if you can read more bytes from the stream than readlimit before calling reset, you do not need the stream to record any data.
Parameters:
Readlimit-the maximum limit of bytes that can be read before the tag position expires.
4 reset
Public void reset () throws IOException
Relocate this stream to where it was when the mark method was last called on this input stream.
The general agreement of reset is:
1. If the method markSupported returns true, then:
If the method mark is not called after the stream is created, or if the number of bytes read from the stream after the last call to mark is greater than the parameter at the time of the last call to mark, IOException may be thrown.
If such an IOException is not thrown, the stream is reset to such a state that all bytes read since the most recent call to mark (or from the beginning of the file if mark has not been called) are re-provided to subsequent callers of the read method, followed by any bytes that will be the next input data from the time reset is called.
2. If the method markSupported returns false, then:
A call to reset may throw an IOException.
If no IOException is thrown, the stream is reset to a fixed state, depending on the specific type of input stream and how it is created. The bytes provided to subsequent callers of the read method depend on the specific type of input stream.
The InputStream-like method reset does nothing except throw IOException.
Throw:
IOException-if the stream is not marked or the tag is invalid.
At this point, the study of "the use of the reset () and mark () commands of the InputStream input stream in the linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.