Java : FilterInputStream - API使用例
FilterInputStream (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
FilterInputStream は、InputStream オブジェクトをラップするだけのクラスです。
BufferedInputStream や DataInputStream などのスーパークラスとして使われています。
FilterInputStream は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。
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
}
フィールド
protected InputStream in
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
}
コンストラクタ
FilterInputStream (InputStream in)
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 available ()
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 ()
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]
}
// try-with-resources文を使わない例です。
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)
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 ()
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 ()
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)
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)
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 ()
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)
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
}
InputStreamで宣言されたメソッド
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
「Java API 使用例 : InputStream」をご参照ください。