Create ArrayList from Bytes

I want to read bytes from a wave file to an array. Since the number of bytes read depends on the size of the wave file, I create an array of bytes with a maximum size of 1,000,000. But this leads to empty values ​​at the end of the array. So, I wanted to create a dynamically increasing array, and I found that the ArrayList solution is a solution. But the read () function of the AudioInputStream class only reads bytes into an array of bytes! How to pass values ​​to an ArrayList?

+6
source share
3 answers

You can have an array of bytes like:

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

To convert it to arrays

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

(Then you will need to write a converter to convert Byte[] to Byte[] ).

EDIT: You are using List<Byte> incorrectly, I will 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 frameSize if frameSize not 1 . Therefore, use a byte buffer to read data, for example:

 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(); 
+13
source

ArrayList not a solution, ByteArrayOutputStream is a solution. Create a ByteArrayOutputStream by injecting your bytes into it, and then call toByteArray() to get the bytes.

An example of what your code looks 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(); 
+17
source

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 been doing Java for a while.

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

Edit: And here is an alternative way to do this 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 - this remains as an exercise for the reader!

+4
source

Source: https://habr.com/ru/post/890481/


All Articles