Java : InputStream - API Examples

InputStream (Java SE 17 & JDK 17) API Examples.
You will find code examples on most InputStream methods.


Summary

This abstract class is the superclass of all classes representing an input stream of bytes.

Class diagram

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 ()

Constructor for subclasses to call.

I think it's rare to create a subclass of InputStream. Therefore, the code example is omitted.

Methods

int available ()

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

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 ()

Closes this input stream and releases any system resources associated with the stream.

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)

Marks the current position in this input stream.

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 ()

Tests if this input stream supports the mark and reset methods.

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 ()

Returns a new InputStream that reads no bytes.

final var is = InputStream.nullInputStream();

System.out.println(is.read()); // -1
System.out.println(Arrays.toString(is.readAllBytes())); // []

is.close();

//is.read(); // IOException: Stream closed

abstract int read ()

Reads the next byte of data from the input stream.

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)

Reads some number of bytes from the input stream and stores them into the buffer array 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)

Reads up to len bytes of data from the input stream into an array of bytes.

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 ()

Reads all remaining bytes from the input stream.

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)

Reads the requested number of bytes from the input stream into the given byte array.

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)

Reads up to a specified number of bytes from the input stream.

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 ()

Repositions this stream to the position at the time the mark method was last called on this input stream.

Please see mark(int readlimit).

long skip (long n)

Skips over and discards n bytes of data from this input stream.

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)

Skips over and discards exactly n bytes of data from this input stream.

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

    //is.skipNBytes(1); // EOFException
}
final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    //is.skipNBytes(10); // EOFException
}
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)

Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.

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

To top of page