Although the use-case is not completely clear - what is the underlying stream, etc, but among a few options you have wrapping the output stream into a buffered one seems to make sense, please see a short example below. On FileOutputStream, with 400k small arrays, it gives 20x performance boost:
// Buffered write, time elapsed: 51
// Raw write, time elapsed: 1050
import java.io.*;
import java.util.*;
public class Test {
public static void main(String [] args) throws Exception {
int max = 400000;
List<byte[]> arrays = new ArrayList<>(max);
for(int i=0; i< max; i++) {
arrays.add(new byte[40]);
}
try(FileOutputStream fout = new FileOutputStream("/tmp/deleteme")) {
long start = System.currentTimeMillis();
writeBuffered(arrays, fout);
System.out.println("Buffered write, time elapsed: " + (System.currentTimeMillis() - start));
}
try(FileOutputStream fout = new FileOutputStream("/tmp/deleteme")) {
long start = System.currentTimeMillis();
writeRaw(arrays, fout);
System.out.println("Raw write, time elapsed: " + (System.currentTimeMillis() - start));
}
}
static void writeRaw(List<byte[]> arrays, OutputStream out) throws IOException {
for (byte[] packet: arrays) {
out.write(packet);
}
}
static void writeBuffered(List<byte[]> arrays, OutputStream out) throws IOException {
BufferedOutputStream bout = new BufferedOutputStream(out);
for (byte[] packet: arrays) {
bout.write(packet);
}
}
}