Java : ByteArrayOutputStream con ejemplos
ByteArrayOutputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ByteArrayOutputStream.
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 ByteArrayOutputStream has no effect.
final var os = new ByteArrayOutputStream();
os.write(10);
os.write(20);
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
final var ret = os.toByteArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 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.
Constructors
ByteArrayOutputStream ()
final var os = new ByteArrayOutputStream();
final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // []
os.write(10);
os.write(20);
final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // [10, 20]
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [10, 20, 30, 40, 50]
ByteArrayOutputStream (int size)
class MyOutputStream extends ByteArrayOutputStream {
MyOutputStream() {
super();
}
MyOutputStream(int size) {
super(size);
}
int getCapacity() {
return buf.length;
}
}
final var os1 = new MyOutputStream();
System.out.println(os1.getCapacity()); // 32
final var os2 = new MyOutputStream(128);
System.out.println(os2.getCapacity()); // 128
Methods
void close ()
final var os = new ByteArrayOutputStream();
// Closing a stream has no effect.
os.close();
// The methods in this class can be called after the stream has been closed.
os.write(10);
os.write(20);
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20]
void reset ()
final var os = new ByteArrayOutputStream();
os.write(10);
os.write(20);
final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // [10, 20]
os.reset();
final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // []
os.write(30);
os.write(40);
final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [30, 40]
int size ()
final var os = new ByteArrayOutputStream();
System.out.println(os.size()); // 0
System.out.println(Arrays.toString(os.toByteArray())); // []
os.write(10);
System.out.println(os.size()); // 1
System.out.println(Arrays.toString(os.toByteArray())); // [10]
os.write(20);
System.out.println(os.size()); // 2
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20]
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
System.out.println(os.size()); // 5
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20, 30, 40, 50]
byte[] toByteArray ()
final var os = new ByteArrayOutputStream();
final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // []
os.write(10);
os.write(20);
final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // [10, 20]
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [10, 20, 30, 40, 50]
String toString ()
final var os = new ByteArrayOutputStream();
final var bytes1 = "abcd".getBytes();
System.out.println(Arrays.toString(bytes1)); // [97, 98, 99, 100]
os.writeBytes(bytes1);
final var ret1 = os.toString();
System.out.println(ret1); // abcd
final var bytes2 = "XYZ".getBytes();
System.out.println(Arrays.toString(bytes2)); // [88, 89, 90]
os.writeBytes(bytes2);
final var ret2 = os.toString();
System.out.println(ret2); // abcdXYZ
String toString (int hibyte)
Deprecated.
String toString (String charsetName)
final var os = new ByteArrayOutputStream();
final var bytes = "○△□".getBytes("Shift_JIS");
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, -96]
os.writeBytes(bytes);
final var ret1 = os.toString();
System.out.println(ret1); // "������" ← Garbled text
final var ret2 = os.toString("Shift_JIS");
System.out.println(ret2); // "○△□"
String toString (Charset charset)
final var os = new ByteArrayOutputStream();
final var sjis = Charset.forName("Shift_JIS");
final var bytes = "○△□".getBytes(sjis);
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, -96]
os.writeBytes(bytes);
final var ret1 = os.toString();
System.out.println(ret1); // "������" ← Garbled text
final var ret2 = os.toString(sjis);
System.out.println(ret2); // "○△□"
void write (byte[] b, int off, int len)
final var os = new ByteArrayOutputStream();
final byte[] bytes = {1, 2, 3, 4};
os.write(bytes, 0, 0);
System.out.println(Arrays.toString(os.toByteArray())); // []
os.reset();
os.write(bytes, 0, 1);
System.out.println(Arrays.toString(os.toByteArray())); // [1]
os.reset();
os.write(bytes, 0, 2);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2]
os.reset();
os.write(bytes, 0, 3);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2, 3]
os.reset();
os.write(bytes, 0, 4);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2, 3, 4]
final var os = new ByteArrayOutputStream();
final byte[] bytes = {1, 2, 3, 4};
os.reset();
os.write(bytes, 1, 3);
System.out.println(Arrays.toString(os.toByteArray())); // [2, 3, 4]
os.reset();
os.write(bytes, 2, 2);
System.out.println(Arrays.toString(os.toByteArray())); // [3, 4]
os.reset();
os.write(bytes, 3, 1);
System.out.println(Arrays.toString(os.toByteArray())); // [4]
os.reset();
os.write(bytes, 4, 0);
System.out.println(Arrays.toString(os.toByteArray())); // []
void write (int b)
final var os = new ByteArrayOutputStream();
final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // []
os.write(10);
os.write(20);
final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // [10, 20]
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [10, 20, 30, 40, 50]
void writeBytes (byte[] b)
final var os = new ByteArrayOutputStream();
final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // []
os.write(10);
os.write(20);
final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // [10, 20]
final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);
final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [10, 20, 30, 40, 50]
void writeTo (OutputStream out)
final var src = new ByteArrayOutputStream();
final var dst = new ByteArrayOutputStream();
src.write(10);
src.write(20);
src.write(30);
System.out.println(Arrays.toString(src.toByteArray())); // [10, 20, 30]
System.out.println(Arrays.toString(dst.toByteArray())); // []
src.writeTo(dst);
System.out.println(Arrays.toString(src.toByteArray())); // [10, 20, 30]
System.out.println(Arrays.toString(dst.toByteArray())); // [10, 20, 30]
Methods declared in OutputStream
Related posts
- Ejemplos de API