In JavaByteArrayOutputStream
Is a byte array output stream that allows the application to write data in bytes into a byte array buffer.
The following is correctByteArrayOutputStream
The detailed introduction of the structure method, method, usage example and operation results.
1. ByteArrayOutputStream Overview
ByteArrayOutputStream
Class is located inIn the package, inherited
OutputStream
kind.
It does not interact directly with external devices (such as files), but instead creates a byte array buffer in memory, all written toByteArrayOutputStream
The data will be stored in this buffer.
2. ByteArrayOutputStream construction method
ByteArrayOutputStream
The following construction methods are provided:
1.ByteArrayOutputStream()
- Function: Create a new
ByteArrayOutputStream
, its buffer size is 32 bytes.
2.ByteArrayOutputStream(int size)
- parameter:
size
Specifies the size of the buffer. - Function: Create a new
ByteArrayOutputStream
, its buffer size issize
Parameter Specifies.
3. Common methods of ByteArrayOutputStream
Here are someByteArrayOutputStream
Common methods:
1.void write(int b)
- Function: Write the specified byte to this byte array output stream.
2.void write(byte[] b, int off, int len)
- Function: to offset from the specified byte array
off
Beginninglen
Bytes are written to this byte array output stream.
3.void writeTo(OutputStream out)
- parameter:
out
is the output stream to write data. - Function: Write the entire contents of this byte array output stream into the specified output stream parameters.
4.byte[] toByteArray()
- Return Value: A byte array containing all the contents of this byte array output stream.
- Function: Create a newly allocated byte array whose size is the current size of this output stream and the valid content of the buffer has been copied into the array.
5.String toString()
- Return value: Convert buffer content to string.
- Function: Use the platform's default character set to convert buffer content into strings.
6.int size()
- Return value: The current size of the buffer.
- Function: Returns the current size of the buffer.
4. Code examples
The following is a useByteArrayOutputStream
Example:
import ; import ; public class ByteArrayOutputStreamExample { public static void main(String[] args) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String text = "Hello, World!"; try { // Convert a string to a byte array and write to ByteArrayOutputStream (()); // Output original content ("Original content:"); (text); // Get the byte array in ByteArrayOutputStream byte[] bytes = (); // Convert the byte array back to a string and output it String output = new String(bytes); ("Content from ByteArrayOutputStream:"); (output); // Use the toString method to directly get the string String toStringOutput = (); ("Content from toString method:"); (toStringOutput); // Get the current size int size = (); ("Size of ByteArrayOutputStream:"); (size); } catch (IOException e) { (); } } }
V. Operation results
Original content:
Hello, World!
Content from ByteArrayOutputStream:
Hello, World!
Content from toString method:
Hello, World!
Size of ByteArrayOutputStream:
13
6. Summary
ByteArrayOutputStream
is a very useful class that allows us to build byte arrays in memory without interacting with file systems or other external devices.
This makes it very suitable for situations where byte arrays need to be built dynamically, such as packet construction in network communication, string-to-byte array conversion, etc.
ByteArrayOutputStream
Provides a variety of methods to manipulate byte array buffers, including writing data, getting data, converting it into a string, etc. useByteArrayOutputStream
Byte operations can be performed efficiently without worrying about buffer size management, becauseByteArrayOutputStream
The buffer expansion will be processed automatically.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.