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
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
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int count
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int mark
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int pos
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Constructors
ByteArrayInputStream (byte[] buf)
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)
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 ()
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 ()
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)
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 ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.markSupported()); // true
int read ()
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)
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 ()
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)
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 ()
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)
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