Java : ByteArrayInputStream con ejemplos
ByteArrayInputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ByteArrayInputStream.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Un ByteArrayInputStream contiene un búfer interno que contiene bytes que pueden leerse desde el flujo. Un contador interno lleva un registro del siguiente byte que debe suministrar el método de lectura. (Traducción automática)
Note : Closing a ByteArrayInputStream has no effect.
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
Fields
protected byte[] buf
Una matriz de bytes proporcionada por el creador de la transmisión. (Traducción automática)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int count
El índice es uno mayor que el último byte válido en el búfer de flujo de entrada. (Traducción automática)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int mark
La posición actualmente marcada en la secuencia. (Traducción automática)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int pos
El índice del próximo byte que se leerá desde el búfer de flujo de entrada. (Traducción automática)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Constructors
ByteArrayInputStream (byte[] buf)
Crea un ByteArrayInputStream para que utilice buf como su matriz de búfer. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
ByteArrayInputStream (byte[] buf, int offset, int length)
Crea ByteArrayInputStream que utiliza buf como su matriz de búfer. (Traducción automática)
final byte[] buf = {10, 20, 30, 40};
final var is1 = new ByteArrayInputStream(buf, 0, 0);
System.out.println(Arrays.toString(is1.readAllBytes())); // []
final var is2 = new ByteArrayInputStream(buf, 0, 1);
System.out.println(Arrays.toString(is2.readAllBytes())); // [10]
final var is3 = new ByteArrayInputStream(buf, 0, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [10, 20]
final var is4 = new ByteArrayInputStream(buf, 0, 3);
System.out.println(Arrays.toString(is4.readAllBytes())); // [10, 20, 30]
final var is5 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is5.readAllBytes())); // [10, 20, 30, 40]
final var is6 = new ByteArrayInputStream(buf, 0, 5);
System.out.println(Arrays.toString(is6.readAllBytes())); // [10, 20, 30, 40]
final byte[] buf = {10, 20, 30, 40};
final var is1 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is1.readAllBytes())); // [10, 20, 30, 40]
final var is2 = new ByteArrayInputStream(buf, 1, 3);
System.out.println(Arrays.toString(is2.readAllBytes())); // [20, 30, 40]
final var is3 = new ByteArrayInputStream(buf, 2, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [30, 40]
final var is4 = new ByteArrayInputStream(buf, 3, 1);
System.out.println(Arrays.toString(is4.readAllBytes())); // [40]
final var is5 = new ByteArrayInputStream(buf, 4, 0);
System.out.println(Arrays.toString(is5.readAllBytes())); // []
Methods
int available ()
Devuelve la cantidad de bytes restantes que se pueden leer (o omitir) de este flujo de entrada. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
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
void close ()
Cerrar un ByteArrayInputStream no tiene ningún efecto. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
// Closing a stream has no effect.
is.close();
// The methods in this class can be called after the stream has been closed.
System.out.println(Arrays.toString(is.readAllBytes())); // [10, 20, 30, 40, 50]
void mark (int readAheadLimit)
Establezca la posición marcada actual en la secuencia. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
// Note: The readAheadLimit for this class has no meaning.
is.mark(0);
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()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
boolean markSupported ()
Comprueba si este InputStream admite marcar/reiniciar. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.markSupported()); // true
int read ()
Lee el siguiente byte de datos de este flujo de entrada. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = 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()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
int read (byte[] b, int off, int len)
Lee hasta len bytes de datos en una matriz de bytes desde este flujo de entrada. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
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[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
final var is = new ByteArrayInputStream(buf);
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
void reset ()
Restablece el buffer a la posición marcada. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
// Note: The readAheadLimit for this class has no meaning.
is.mark(0);
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()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
long skip (long n)
Omite n bytes de entrada de este flujo de entrada. (Traducción automática)
final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
final var is = 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
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
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
Methods declared in InputStream
nullInputStream, read, readNBytes, skipNBytes, transferTo
Consulte el siguiente enlace.
Related posts
- Ejemplos de API