I want to create an InputStream that is limited to a certain range of bytes in file, e.g. to bytes from position 0 to 100. So that the client code should see EOF once 100th byte is reached.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
read()method ofInputStreamreads a single byte at a time. You could write a subclass ofInputStreamthat maintains an internal counter; each timeread()is called, update the counter. If you have hit your maximum, do not allow any further reads (return -1 or something like that).You will also need to ensure that the other methods for reading
read_int, etc are unsupported (ex: Override them and just throw UnsupportedOperationException());I don’t know what your use case is, but as a bonus you may want to implement buffering as well.