public final class CapturingReader extends Reader
An example use for this class can be logging in an HTTP filter or HTTP client. Instead of copying the contents of a reader to memory, logging the
contents, and then passing a StringReader
with the copied contents, you can create a capturing reader with a callback that performs the
logging.
CapturingReader
supports mark(int)
and reset()
if its backing reader 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 |
CapturingReader.Config
Configuration for
capturing readers . |
Constructor and Description |
---|
CapturingReader(Reader input,
CapturingReader.Config config)
Creates a new capturing reader.
|
Modifier and Type | Method and Description |
---|---|
String |
captured()
Returns the contents that have been captured.
|
void |
close() |
static CapturingReader.Config.Builder |
config()
Creates a builder for capturing reader configurations.
|
boolean |
isClosed()
Returns whether or not this reader has been closed.
|
boolean |
isConsumed()
Returns whether or not this reader has been fully consumed.
|
void |
mark(int readlimit) |
boolean |
markSupported() |
int |
read() |
int |
read(char[] c) |
int |
read(char[] c,
int off,
int len) |
void |
reset() |
long |
totalChars()
Returns the total number of characters that have been read.
|
public CapturingReader(Reader input, CapturingReader.Config config)
input
- The reader to capture from.config
- The configuration to use.NullPointerException
- If the given reader or config is null
.public int read() throws IOException
read
in class Reader
IOException
public int read(char[] c) throws IOException
read
in class Reader
IOException
public int read(char[] c, int off, int len) throws IOException
read
in class Reader
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
public void mark(int readlimit) throws IOException
mark
in class Reader
IOException
public void reset() throws IOException
reset
in class Reader
IOException
public boolean markSupported()
markSupported
in class Reader
public String captured()
public long totalChars()
public boolean isConsumed()
-1
.true
if this reader has been fully consumed, or false
otherwise.public boolean isClosed()
true
if this reader has been closed, or false
otherwise.public static CapturingReader.Config.Builder config()
Copyright © 2020–2024. All rights reserved.