Java : FilterReader with Examples

FilterReader (Java SE 22 & JDK 22) with Examples.
You will find code examples on most FilterReader methods.


Summary

Abstract class for reading filtered character streams. The abstract class FilterReader itself provides default methods that pass all requests to the contained stream. Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields.

Class diagram

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abc"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}

Fields

protected Reader in

The underlying character-input stream.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }

    Reader getIn() {
        return in;
    }
}

final var sr = new StringReader("abc");
try (final var reader = new MyReader(sr)) {
    System.out.println(reader.getIn() == sr); // true
}

Fields declared in Reader

lock

Please see the link below.

Constructors

FilterReader (Reader in)

Creates a new filtered reader.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abc"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}

Methods

void close ()

Closes the stream and releases any system resources associated with it.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abc"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}
// An example without a try-with-resources statement.
class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

final var reader = new MyReader(new StringReader("abc"));
try {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
} finally {
    reader.close();
}

void mark (int readAheadLimit)

Marks the present position in the stream.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new BufferedReader(new StringReader("abcde")))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

boolean markSupported ()

Tells whether this stream supports the mark() operation.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

{
    final var path = Path.of("R:", "java-work", "aaa.txt");
    System.out.println(path); // R:\java-work\aaa.txt

    Files.writeString(path, "abcd");

    try (final var reader = new MyReader(Files.newBufferedReader(path))) {
        System.out.println(reader.markSupported()); // true
    }
}
{
    try (final var reader = new MyReader(new StringReader("abcd"))) {
        System.out.println(reader.markSupported()); // true
    }
}
{
    final var url = URI.create("https://example.com/").toURL();
    try (final var reader = new MyReader(new InputStreamReader(url.openStream()))) {
        System.out.println(reader.markSupported()); // false
    }
}

int read ()

Reads a single character.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abc"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}

int read (char[] cbuf, int off, int len)

Reads characters into a portion of an array.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abcdefg"))) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [a,  ,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 2)); // 2
    System.out.println(Arrays.toString(cbuf)); // [b, c,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [d, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 4)); // 1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 5)); // -1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]
}

try (final var reader = new MyReader(new StringReader("abcde"))) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 4, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  ,  , a]

    System.out.println(reader.read(cbuf, 3, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  , b, a]

    System.out.println(reader.read(cbuf, 2, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  , c, b, a]

    System.out.println(reader.read(cbuf, 1, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ , d, c, b, a]

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [e, d, c, b, a]
}

try (final var reader = new MyReader(new StringReader("abcdefg"))) {
    final var cbuf = new char[3];

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [a, b, c]

    //reader.read(cbuf, 0, 4); // IndexOutOfBoundsException
    //reader.read(cbuf, 3, 1); // IndexOutOfBoundsException
}

boolean ready ()

Tells whether this stream is ready to be read.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, "abc");

try (final var reader = new MyReader(Files.newBufferedReader(path))) {
    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // b

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.ready()); // false
    System.out.println(reader.read()); // -1
}

void reset ()

Resets the stream.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new BufferedReader(new StringReader("abcde")))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

long skip (long n)

Skips characters.

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abcdefghij"))) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(1)); // 1
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.skip(2)); // 2
    System.out.printf("%c%n", reader.read()); // f

    System.out.println(reader.skip(3)); // 3
    System.out.printf("%c%n", reader.read()); // j

    System.out.println(reader.skip(1)); // 0
    System.out.println(reader.read()); // -1
}

try (final var reader = new MyReader(new StringReader("abcde"))) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(10)); // 4
    System.out.println(reader.read()); // -1
}

Methods declared in Reader

nullReader, read, read, transferTo

Please see the link below.


Related posts

To top of page