Java : FilterInputStream con ejemplos
FilterInputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de FilterInputStream.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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
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)
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 ()
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]
}
// 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)
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
}
Methods declared in InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Consulte el siguiente enlace.
Related posts
- Ejemplos de API