简体   繁体   中英

Convert byte array to OutputStream

I have following code to write byte array to OutputStream in Java program.

public static void write(OutputStream out, ArrayList<byte[]> buffer) throws IOException {
    for (byte[] packet: buffer) {
        out.write(packet);
    }
}

When buffer size is 770,000 and the max length of each element is 40, this loop takes 2840 millisecond.

Is there a more optimized / faster way of doing that convertion?

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM