SoFunction
Updated on 2025-03-02

ByteArrayOutputStream Usage in Java

In JavaByteArrayOutputStreamIs a byte array output stream that allows the application to write data in bytes into a byte array buffer.

The following is correctByteArrayOutputStreamThe detailed introduction of the structure method, method, usage example and operation results.

1. ByteArrayOutputStream Overview

ByteArrayOutputStreamClass is located inIn the package, inheritedOutputStreamkind.

It does not interact directly with external devices (such as files), but instead creates a byte array buffer in memory, all written toByteArrayOutputStreamThe data will be stored in this buffer.

2. ByteArrayOutputStream construction method

ByteArrayOutputStreamThe following construction methods are provided:

1.ByteArrayOutputStream()

  • Function: Create a newByteArrayOutputStream, its buffer size is 32 bytes.

2.ByteArrayOutputStream(int size)

  • parameter:sizeSpecifies the size of the buffer.
  • Function: Create a newByteArrayOutputStream, its buffer size issizeParameter Specifies.

3. Common methods of ByteArrayOutputStream

Here are someByteArrayOutputStreamCommon 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 arrayoffBeginninglenBytes are written to this byte array output stream.

3.void writeTo(OutputStream out)

  • parameter:outis 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 useByteArrayOutputStreamExample:

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

ByteArrayOutputStreamis 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.

ByteArrayOutputStreamProvides a variety of methods to manipulate byte array buffers, including writing data, getting data, converting it into a string, etc. useByteArrayOutputStreamByte operations can be performed efficiently without worrying about buffer size management, becauseByteArrayOutputStreamThe 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.