create an ArrayList of bytes

前端 未结 3 1100
灰色年华
灰色年华 2021-01-05 06:01

I want to read bytes from a wave file into an array. Since the number of bytes read depends upon the size of the wave file, I\'m creating a byte array with a maximum size of

3条回答
  •  难免孤独
    2021-01-05 06:50

    ArrayList isn't the solution, ByteArrayOutputStream is the solution. Create a ByteArrayOutputStream write your bytes to it, and then invoke toByteArray() to get the bytes.

    Example of what your code should look like:

    in = new BufferedInputStream(inputStream, 1024*32);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] dataBuffer = new byte[1024 * 16];
    int size = 0;
    while ((size = in.read(dataBuffer)) != -1) {
        out.write(dataBuffer, 0, size);
    }
    byte[] bytes = out.toByteArray();
    

提交回复
热议问题