Java : FilterInputStream with Examples
FilterInputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most FilterInputStream methods.
Summary
A FilterInputStream wraps some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // -1
}
Fields
protected InputStream in
The input stream to be filtered.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
InputStream getIn() {
return in;
}
}
final var byteArrayInputStream = new ByteArrayInputStream(new byte[0]);
try (final var is = new MyInputStream(byteArrayInputStream)) {
System.out.println(is.getIn() == byteArrayInputStream); // true
}
Constructors
FilterInputStream (InputStream in)
Creates a FilterInputStream by assigning the argument in to the field this.in so as to remember it for later use.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // -1
}
Methods
int available ()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.available()); // 3
System.out.println(is.read()); // 10
System.out.println(is.available()); // 2
System.out.println(is.read()); // 20
System.out.println(is.available()); // 1
System.out.println(is.read()); // 30
System.out.println(is.available()); // 0
System.out.println(is.read()); // -1
}
void close ()
Closes this input stream and releases any system resources associated with the stream.
final byte[] buf = {10, 20, 30};
try (final FilterInputStream is = new BufferedInputStream(
new ByteArrayInputStream(buf))) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
}
// An example without a try-with-resources statement.
final byte[] buf = {10, 20, 30};
final FilterInputStream is = new BufferedInputStream(new ByteArrayInputStream(buf));
try {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
} finally {
is.close();
}
void mark (int readlimit)
Marks the current position in this input stream.
final byte[] buf = {10, 20, 30, 40, 50};
try (final FilterInputStream is = new BufferedInputStream(
new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
is.mark(4);
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
is.reset();
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
}
boolean markSupported ()
Tests if this input stream supports the mark and reset methods.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.markSupported()); // true
}
int read ()
Reads the next byte of data from the input stream.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // -1
}
int read (byte[] b)
Reads up to b.length bytes of data from this input stream into an array of bytes.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final var is = new MyInputStream(new ByteArrayInputStream(bytes))) {
final var b1 = new byte[3];
System.out.println(is.read(b1)); // 3
System.out.println(Arrays.toString(b1)); // [10, 20, 30]
final var b2 = new byte[3];
System.out.println(is.read(b2)); // 2
System.out.println(Arrays.toString(b2)); // [40, 50, 0]
final var b3 = new byte[3];
System.out.println(is.read(b3)); // -1
System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}
int read (byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
final var b1 = new byte[3];
System.out.println(is.read(b1, 0, 1)); // 1
System.out.println(Arrays.toString(b1)); // [10, 0, 0]
final var b2 = new byte[3];
System.out.println(is.read(b2, 0, 2)); // 2
System.out.println(Arrays.toString(b2)); // [20, 30, 0]
final var b3 = new byte[3];
System.out.println(is.read(b3, 0, 3)); // -1
System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
final var b1 = new byte[3];
System.out.println(is.read(b1, 2, 1)); // 1
System.out.println(Arrays.toString(b1)); // [0, 0, 10]
final var b2 = new byte[3];
System.out.println(is.read(b2, 1, 2)); // 2
System.out.println(Arrays.toString(b2)); // [0, 20, 30]
final var b3 = new byte[3];
System.out.println(is.read(b3, 0, 3)); // -1
System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}
void reset ()
Repositions this stream to the position at the time the mark method was last called on this input stream.
final byte[] buf = {10, 20, 30, 40, 50};
try (final FilterInputStream is = new BufferedInputStream(
new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
is.mark(4);
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
is.reset();
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
}
long skip (long n)
Skips over and discards n bytes of data from the input stream.
class MyInputStream extends FilterInputStream {
MyInputStream(InputStream in) {
super(in);
}
}
final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.read()); // 10
System.out.println(is.skip(1)); // 1
System.out.println(is.read()); // 30
System.out.println(is.skip(2)); // 2
System.out.println(is.read()); // 60
System.out.println(is.skip(3)); // 3
System.out.println(is.read()); // 100
System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1
}
Methods declared in InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Please see the link below.
Related posts
- API Examples