public final class CapturingInputStream extends InputStream
An example use for this class can be logging in an HTTP filter or HTTP client. Instead of copying the contents of an input stream to memory,
logging the contents, and then passing a ByteArrayInputStream
with the copied contents, you can create a capturing input stream with a
callback that performs the logging.
CapturingInputStream
supports mark(int)
and reset()
if its backing input stream does. When reset()
is called, it
will "uncapture" any contents up to the previous mark. It will still only once call any of the callbacks.
Modifier and Type | Class and Description |
---|---|
static class |
CapturingInputStream.Config
Configuration for
capturing input streams . |
Constructor and Description |
---|
CapturingInputStream(InputStream input,
CapturingInputStream.Config config)
Creates a new capturing input stream.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
captured()
Returns the contents that have been captured.
|
String |
captured(Charset charset)
Returns the contents that have been captured, as a string.
|
void |
close() |
static CapturingInputStream.Config.Builder |
config()
Creates a builder for capturing input stream configurations.
|
boolean |
isClosed()
Returns whether or not this input stream has been closed.
|
boolean |
isConsumed()
Returns whether or not this input stream has been fully consumed.
|
void |
mark(int readlimit) |
boolean |
markSupported() |
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int off,
int len) |
void |
reset() |
long |
totalBytes()
Returns the total number of bytes that have been read.
|
available, skip
public CapturingInputStream(InputStream input, CapturingInputStream.Config config)
input
- The input stream to capture from.config
- The configuration to use.NullPointerException
- If the given input stream or config is null
.public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b) throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public void mark(int readlimit)
mark
in class InputStream
public void reset() throws IOException
reset
in class InputStream
IOException
public boolean markSupported()
markSupported
in class InputStream
public byte[] captured()
public String captured(Charset charset)
charset
- The charset to use.public long totalBytes()
public boolean isConsumed()
-1
.true
if this input stream has been fully consumed, or false
otherwise.public boolean isClosed()
true
if this input stream has been closed, or false
otherwise.public static CapturingInputStream.Config.Builder config()
Copyright © 2020–2024. All rights reserved.