create an ArrayList of bytes

2019-02-13 16:16发布

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 1000000. But this is resulting in empty values at the end of the array. So, I wanted to create a dynamically increasing array and I found that ArrayList is the solution. But the read() function of the AudioInputStream class reads bytes only into a byte array! How do I pass the values into an ArrayList instead?

3条回答
▲ chillily
2楼-- · 2019-02-13 16:56

You can have an array of byte like:

List<Byte> arrays = new ArrayList<Byte>();

To convert it back to arrays

Byte[] soundBytes = arrays.toArray(new Byte[arrays.size()]);

(Then, you will have to write a converter to transform Byte[] to byte[]).

EDIT: You are using List<Byte> wrong, I'll just show you how to read AudioInputStream simply with ByteArrayOutputStream.

AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read;

while((read = ais.read()) != -1) {
    baos.write(read);
}

byte[] soundBytes = baos.toByteArray();

PS An IOException is thrown if frameSize is not equal to 1. Hence use a byte buffer to read data, like so:

AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;

while((bytesRead = ais.read(buffer)) != -1) {
    baos.write(buffer, 0, bytesRead);
}

byte[] soundBytes = baos.toByteArray();
查看更多
在下西门庆
3楼-- · 2019-02-13 17:02

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();
查看更多
够拽才男人
4楼-- · 2019-02-13 17:06

Something like this should do:

List<Byte> myBytes = new ArrayList<Byte>();

//assuming your javax.sound.sampled.AudioInputStream is called ais

while(true) {
  Byte b = ais.read();
  if (b != -1) { //read() returns -1 when the end of the stream is reached
    myBytes.add(b);
  } else {
    break;
  }
}

Sorry if the code is a bit wrong. I haven't done Java for a while.

Also, be careful if you do implement it as a while(true) loop :)

Edit: And here's an alternative way of doing it that reads more bytes each time:

int arrayLength = 1024;
List<Byte> myBytes = new ArrayList<Byte>();

while(true) {

  Byte[] aBytes = new Byte[arrayLength];
  int length = ais.read(aBytes); //length is the number of bytes read

  if (length == -1) {  //read() returns -1 when the end of the stream is reached
    break; //or return if you implement this as a method
  } else if (length == arrayLength) {  //Array is full
    myBytes.addAll(aBytes);
  } else {  //Array has been filled up to length

    for (int i = 0; i < length; i++) {
      myBytes.add(aBytes[i]);
    }
  }
}

Note that both read() methods throw an IOException - handling this is left as an exercise for the reader!

查看更多
登录 后发表回答