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
All Methods Instance Methods Concrete Methods 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, transferTo
-
-
-
-
Field Detail
-
in
private final InputStream in
The source input stream to read from.
-
scaleFactor
private final InputStreamTracker.ScaleFactor scaleFactor
A scale factor to apply when counting bytes.
-
numRead
private long numRead
The number of bytes that has been read from the underlying input stream so far.
-
numReadAtMark
private long numReadAtMark
The number of bytes that had been read whenmark(int)
was called.
-
-
Constructor Detail
-
InputStreamTracker
public InputStreamTracker(InputStream in)
Create a new input stream tracker.- Parameters:
in
- The input stream to read from
-
InputStreamTracker
public InputStreamTracker(InputStream in, InputStreamTracker.ScaleFactor scaleFactor)
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 Detail
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- 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
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
-