Java : InputStream con ejemplos

InputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de InputStream.

Nota :


Summary

Esta clase abstracta es la superclase de todas las clases que representan un flujo de entrada de bytes. (Traducción automática)

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 para que las subclases llamen. (Traducción automática)

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

Methods

int available ()

Devuelve una estimación de la cantidad de bytes que se pueden leer (o omitir) de este flujo de entrada sin bloquearse, que puede ser 0 o 0 cuando se detecta el final del flujo. (Traducción automática)

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

Cierra este flujo de entrada y libera cualquier recurso del sistema asociado con el flujo. (Traducción automática)

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)

Marca la posición actual en este flujo de entrada. (Traducción automática)

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

Comprueba si este flujo de entrada admite los métodos de marcar y restablecer. (Traducción automática)

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

Devuelve un nuevo InputStream que no lee ningún byte. (Traducción automática)

try (final var is = InputStream.nullInputStream()) {
    System.out.println(is.read()); // -1
    System.out.println(Arrays.toString(is.readAllBytes())); // []
}

abstract int read ()

Lee el siguiente byte de datos del flujo de entrada. (Traducción automática)

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)

Lee una cierta cantidad de bytes del flujo de entrada y los almacena en la matriz de búfer b. (Traducción automática)

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)

Lee hasta len bytes de datos del flujo de entrada en una matriz de bytes. (Traducción automática)

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

Lee todos los bytes restantes del flujo de entrada. (Traducción automática)

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)

Lee la cantidad solicitada de bytes del flujo de entrada en la matriz de bytes dada. (Traducción automática)

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)

Lee hasta una cantidad específica de bytes del flujo de entrada. (Traducción automática)

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

Reposiciona este flujo a la posición en el momento en que se llamó por última vez al método de marca en este flujo de entrada. (Traducción automática)

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)

Omite y descarta n bytes de datos de este flujo de entrada. (Traducción automática)

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)

Omite y descarta exactamente n bytes de datos de este flujo de entrada. (Traducción automática)

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)

Lee todos los bytes de este flujo de entrada y escribe los bytes en el flujo de salida dado en el orden en que se leen. (Traducción automática)

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