Package net.sf.basedb.util
Class InputStreamTracker
java.lang.Object
java.io.InputStream
net.sf.basedb.util.InputStreamTracker
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
ProgressInputStream
public class InputStreamTracker extends InputStream
This class is used to keep track of the number of bytes read from the
underlying input stream. This is very useful for code that needs to
reporter progress but are having difficulties to know the number of
processed bytes.
- Version:
- 2.4
- Author:
- nicklas
- Last modified
- $Date: 2020-11-09 14:40:05 +0100 (Mon, 09 Nov 2020) $
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InputStreamTracker.CurvedScaleFactor
Apply a curved scale factor that has a max upper limit.static class
InputStreamTracker.IdentityScaleFactor
Do not scale number of bytes.static class
InputStreamTracker.LinearScaleFactor
Apply a linear scale factor so that when we have read N actual bytes the getNumRead() method return N*scale bytes.static interface
InputStreamTracker.ScaleFactor
A scale factor can be used to match number of read bytes to something that better resembles actual progress. -
Field Summary
Fields Modifier and Type Field Description private InputStream
in
The source input stream to read from.private long
numRead
The number of bytes that has been read from the underlying input stream so far.private long
numReadAtMark
The number of bytes that had been read whenmark(int)
was called.private InputStreamTracker.ScaleFactor
scaleFactor
A scale factor to apply when counting bytes. -
Constructor Summary
Constructors Constructor Description InputStreamTracker(InputStream in)
Create a new input stream tracker.InputStreamTracker(InputStream in, InputStreamTracker.ScaleFactor scaleFactor)
Create a new input stream tracker. -
Method Summary
Modifier and Type Method Description int
available()
void
close()
long
getNumRead()
Get the number of bytes read or skipped from the input stream.void
mark(int readlimit)
boolean
markSupported()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
void
reset()
long
skip(long n)
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
in
The source input stream to read from. -
scaleFactor
A scale factor to apply when counting bytes. -
numRead
private long numReadThe number of bytes that has been read from the underlying input stream so far. -
numReadAtMark
private long numReadAtMarkThe number of bytes that had been read whenmark(int)
was called.
-
-
Constructor Details
-
InputStreamTracker
Create a new input stream tracker.- Parameters:
in
- The input stream to read from
-
InputStreamTracker
Create a new input stream tracker.- Parameters:
in
- The input stream to read fromscaleFactor
- A scale factor to apply when counting bytes- Since:
- 3.17.1
-
-
Method Details
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit)- Overrides:
mark
in classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classInputStream
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
reset
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
getNumRead
public long getNumRead()Get the number of bytes read or skipped from the input stream. If the underlying stream supportsreset()
this will also reset the number of read bytes to the latestmark(int)
position.- Returns:
- The number of bytes that has been read so far
-