Java : BufferedInputStream with Examples
BufferedInputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most BufferedInputStream methods.
Summary
final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data
Files.write(file, new byte[]{10, 20, 30});
try (final var is = new BufferedInputStream(Files.newInputStream(file))) {
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 byte[] buf
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int count
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int marklimit
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int markpos
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int pos
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterInputStream
Constructors
BufferedInputStream (InputStream in)
final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(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
}
BufferedInputStream (InputStream in, int size)
Please see also : BufferedInputStream(InputStream in)
class MyInputStream extends BufferedInputStream {
MyInputStream(InputStream in) {
super(in);
}
MyInputStream(InputStream in, int size) {
super(in, size);
}
int getBufferSize() {
return buf.length;
}
}
final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.getBufferSize()); // 8192
}
try (final var is = new MyInputStream(new ByteArrayInputStream(buf), 32)) {
System.out.println(is.getBufferSize()); // 32
}
Methods
int available ()
final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(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 var file = Path.of("R:", "java-work", "aaa.data");
Files.write(file, new byte[]{10, 20, 30});
try (final var is = new BufferedInputStream(Files.newInputStream(file))) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
}
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "aaa.data");
Files.write(file, new byte[]{10, 20, 30});
final var is = new BufferedInputStream(Files.newInputStream(file));
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 var 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 ()
final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
System.out.println(is.markSupported()); // true
}
int read ()
final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(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, int off, int len)
final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(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 BufferedInputStream(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 var 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)
final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
try (final var is = new BufferedInputStream(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 FilterInputStream
Methods declared in InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Please see the link below.
Related posts
- API Examples