Java : InputStream with Examples
InputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most InputStream methods.
Summary
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
Files.write(file, new byte[]{10, 20, 30});
try (final var is = Files.newInputStream(file)) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
}
Constructors
InputStream ()
I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
int available ()
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(is.available()); // 5
System.out.println(is.read()); // 10
System.out.println(is.available()); // 4
System.out.println(is.read()); // 20
System.out.println(is.available()); // 3
System.out.println(is.read()); // 30
System.out.println(is.available()); // 2
System.out.println(is.read()); // 40
System.out.println(is.available()); // 1
System.out.println(is.read()); // 50
System.out.println(is.available()); // 0
System.out.println(is.read()); // -1
}
final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};
try (final InputStream is = new SequenceInputStream(
new ByteArrayInputStream(bytes1),
new ByteArrayInputStream(bytes2))) {
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()); // 40
System.out.println(is.available()); // 1
System.out.println(is.read()); // 50
System.out.println(is.available()); // 0
System.out.println(is.read()); // -1
}
void close ()
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
Files.write(file, new byte[]{1, 2, 3});
try (final var is = Files.newInputStream(file)) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
}
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
Files.write(file, new byte[]{1, 2, 3});
final var is = Files.newInputStream(file);
try {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
} finally {
is.close();
}
void mark (int readlimit)
final byte[] buf = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(
new ByteArrayInputStream(buf), 2)) {
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
}
final byte[] buf = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(buf), 2)) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
is.mark(3);
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
try {
is.reset();
} catch (IOException e) {
System.out.println(e);
}
// Result
// ↓
//java.io.IOException: Mark invalid
}
boolean markSupported ()
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(is.markSupported()); // true
}
final var file = Path.of("R:", "java-work", "test.data");
try (final var is = Files.newInputStream(file)) {
System.out.println(is.markSupported()); // false
}
final var file = Path.of("R:", "java-work", "test.data");
try (final InputStream is = new BufferedInputStream(Files.newInputStream(file))) {
System.out.println(is.markSupported()); // true
}
final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};
try (final InputStream is = new SequenceInputStream(
new ByteArrayInputStream(bytes1),
new ByteArrayInputStream(bytes2))) {
System.out.println(is.markSupported()); // false
}
static InputStream nullInputStream ()
try (final var is = InputStream.nullInputStream()) {
System.out.println(is.read()); // -1
System.out.println(Arrays.toString(is.readAllBytes())); // []
}
abstract int read ()
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
}
int read (byte[] b)
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(is.read(b)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.read(b)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[3];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0]
System.out.println(is.read(b)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]
System.out.println(is.read(b)); // 2
System.out.println(Arrays.toString(b)); // [40, 50, 30]
}
int read (byte[] b, int off, int len)
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]
System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]
System.out.println(is.read(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
System.out.println(is.read(b, 0, 5)); // -1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.read(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]
System.out.println(is.read(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]
System.out.println(is.read(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]
System.out.println(is.read(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]
System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
}
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0]
System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]
//is.read(b, 0, 4); // IndexOutOfBoundsException
//is.read(b, 3, 1); // IndexOutOfBoundsException
}
byte[] readAllBytes ()
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
}
final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};
try (final InputStream is = new SequenceInputStream(
new ByteArrayInputStream(bytes1),
new ByteArrayInputStream(bytes2))) {
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
}
int readNBytes (byte[] b, int off, int len)
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]
System.out.println(is.readNBytes(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
System.out.println(is.readNBytes(b, 0, 5)); // 0
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]
System.out.println(is.readNBytes(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]
System.out.println(is.readNBytes(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]
System.out.println(is.readNBytes(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]
System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
}
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(b)); // [0, 0, 0]
System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]
//is.readNBytes(b, 0, 4); // IndexOutOfBoundsException
//is.readNBytes(b, 3, 1); // IndexOutOfBoundsException
}
byte[] readNBytes (int len)
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(is.readNBytes(0))); // []
System.out.println(Arrays.toString(is.readNBytes(1))); // [10]
System.out.println(Arrays.toString(is.readNBytes(2))); // [20, 30]
System.out.println(Arrays.toString(is.readNBytes(3))); // [40, 50, 60]
System.out.println(Arrays.toString(is.readNBytes(1))); // [70]
System.out.println(Arrays.toString(is.readNBytes(1))); // [80]
System.out.println(Arrays.toString(is.readNBytes(1))); // []
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(Arrays.toString(is.readNBytes(100))); // [10, 20, 30, 40, 50]
}
void reset ()
final byte[] buf = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(
new ByteArrayInputStream(buf), 2)) {
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
}
final byte[] buf = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(buf), 2)) {
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
is.mark(3);
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
try {
is.reset();
} catch (IOException e) {
System.out.println(e);
}
// Result
// ↓
//java.io.IOException: Mark invalid
}
long skip (long n)
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
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
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(is.read()); // 10
System.out.println(is.skip(10)); // 4
System.out.println(is.read()); // -1
System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(
new ByteArrayInputStream(bytes), 2)) {
is.mark(1);
System.out.println(is.skip(4)); // 2
System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
}
void skipNBytes (long n)
final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
System.out.println(is.read()); // 10
is.skipNBytes(1);
System.out.println(is.read()); // 30
is.skipNBytes(2);
System.out.println(is.read()); // 60
is.skipNBytes(3);
System.out.println(is.read()); // 100
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
is.skipNBytes(10);
} catch (EOFException e) {
System.out.println("EOFException! : " + e.getMessage());
}
// Result
// ↓
//EOFException! : null
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(
new ByteArrayInputStream(bytes), 2)) {
is.mark(1);
is.skipNBytes(4);
System.out.println(Arrays.toString(is.readAllBytes())); // [50]
}
long transferTo (OutputStream out)
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new ByteArrayInputStream(bytes)) {
try (final var os = new ByteArrayOutputStream()) {
System.out.println(Arrays.toString(os.toByteArray())); // []
System.out.println(is.transferTo(os)); // 5
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20, 30, 40, 50]
}
}
Related posts
- API Examples
- Closeable
- DataInput
- DataOutput
- Flushable
- InputStream
- IOException
- OutputStream
- Reader
- Writer