The problem of re-reading after the Java input stream has finished reading

Source: Internet
Author: User
When the file stream is positioned to the end, the data can no longer be read by the input stream. If you use the Read () method to return 1, if you call the available () method to return 0, to re-read, you use the mark and reset methods. To start reading again from the beginning of 0, use the Mark method before reading the input stream, and the parameter readlimit is defined by itself. If you call the Mark method after reading the nth Word, after the input stream is read, the call reset begins to read from the new nth byte.    Public void mark (int readlimit)              mark the current position in this input stream. Subsequent calls to the  reset  method reposition the stream at the last marked position so that subsequent reads re-read the same byte. The             readlimit  parameter tells the input stream the number of bytes allowed to read before the mark position is invalidated. The general agreement for             mark  is: If the method  marksupported   returns to  true, the input stream always logs all bytes read after calling  mark , and is ready to supply the same bytes again when the method  reset  is invoked (whenever). However, if you can read more than  readlimit  bytes from the stream before you invoke  reset , you do not need the stream to record any data. The mark mark is invalid if the size of the data read after Mark is greater than the maximum of the buffer size of the Readlimit and Bufferedinputstream classes. The              tag has a closed stream that is not valid for it.             inputsThe tream   mark  method does not perform any action.     parameter:           readlimit -  The maximum limit for bytes can be read before the mark position is invalidated.    Note: Mark and reset methods can only be used in Bufferedinputstream, Bufferedinputstream class calls Mark (Int readlimit) The number of byte tags that are read after the method is invalidated is the maximum value in both the buffer sizes of the readlimit and Bufferedinputstream classes, rather than the total determined by readlimit. This is not mentioned in the Java document.    Example:   public previewimage (Inputstream reader2,outputstream os) throws  ioexception{          BufferedInputStream reader =  New bufferedinputstream (reader2);          byte[]   Buffer for b  =  new  byte[1024  *  5];   //output stream  5kb          int  len;            reader.mark (reader.available () +1);          while   (  (Len  =  reader.read (b))    !=  -1)   {               //os.write (B, 0, len);            }             reader.reset ();           while   (  (Len  =  reader.read (b))    !=  -1)   {               Os.write (B, 0, len);            }             os.flush ();            os.close ();      }  
Related Article

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.