CKTIS / Java
io.InputStream: practical reference
io.InputStream: practical reference: InputStream is the base byte-input abstraction; read returns 0..255 or -1 at end of stream, and bulk reads may be partial.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.io.InputStreamInputStream is the base byte-input abstraction; read returns 0..255 or -1 at end of stream, and bulk reads may be partial.
Use InputStream for byte data and loop until -1 while accepting that each read may return fewer bytes than requested.
A single read(byte[]) need not fill its buffer; available is not total length, and failing to close may retain an external resource.
A zero-length request can return zero. readNBytes and skip can complete partially at end of input.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
java.io.InputStream input = new java.io.ByteArrayInputStream(new byte[] {7});
System.out.println(input.read());
}
}
Result
7
The example exercises the documented element in a small deterministic program and compares exact standard output.