Java UnsynchronizedByteArrayOutputStream-class And Method Code Example


Here is an example of how to use the UnsynchronizedByteArrayOutputStream class from the Apache Commons IO library in Java:

import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;

public class Example {
    public static void main(String[] args) {
        UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
        baos.write(new byte[] {1, 2, 3});
        byte[] byteArray = baos.toByteArray();
        System.out.println(Arrays.toString(byteArray)); // prints "[1, 2, 3]"
    }
}

This example creates an instance of UnsynchronizedByteArrayOutputStream, writes some bytes to it, and then retrieves the bytes as an array using the toByteArray method. Note that this class is not thread-safe, unlike the ByteArrayOutputStream class from the Java standard library.