2011-06-14 39 views
6

我想從波形文件中讀取字節到數組中。由於讀取的字節數取決於wave文件的大小,因此我創建了一個最大大小爲1000000的字節數組。但是這會導致數組末尾出現空值。所以,我想創建一個動態增加的數組,我發現ArrayList是解決方案。但AudioInputStream類的read()函數只將字節讀入一個字節數組!我如何將值傳遞給ArrayList?創建一個ArrayList的字節

+0

你想用這個字節數組做什麼?也許你甚至不需要那麼大的臨時緩衝區。 – pcjuzer 2011-06-14 08:57:25

回答

13

你可以有一個像字節數組:

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

要將其轉換回陣列

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

(然後,你就必須寫一個轉換器轉換Byte[]byte[])。

編輯:您使用List<Byte>錯了,我就告訴你如何閱讀AudioInputStream簡單地ByteArrayOutputStream

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

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

byte[] soundBytes = baos.toByteArray(); 

PS如果frameSizeIOException時引發不等於1。因此使用一個字節緩衝器讀取數據,像這樣:

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

*****。java:41:找不到符號 symbol:method read(java.util.List ) location:class javax.sound.sampled.AudioInputStream ais.read(buffer ); – 2011-06-14 08:48:50

+0

這不會編譯:沒有自動/內置的方式來將'List '轉換爲'byte []'。除此之外,使用'List '' **非常**太空效率低。 – 2011-06-14 08:53:30

+0

@Joachim Sauer,你是對的,我忘了需要從'Byte'轉換爲字節。修改答案。 – 2011-06-14 08:58:46

17

ArrayList不是解決辦法,ByteArrayOutputStream是解決方案。創建ByteArrayOutputStream將字節寫入它,然後調用toByteArray()來獲取字節。

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

像這樣的東西應該做的:你的代碼應該是什麼樣子

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

很抱歉,如果代碼是有點不對。我有一段時間沒有做過Java。

另外,要小心,如果你實現它的while(true)循環:)

編輯:下面是這樣做的另一種方式,每次讀取多個字節:

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

請注意,這兩個read()方法都會拋出一個IOException異常 - 處理這個問題留給讀者來練習!

+0

他不應該使用'Byte'的'List',並且逐字節讀取非常緩慢。 – Kaj 2011-06-14 08:59:06

+0

他想要一個'List'而不是一個數組。另外,我提供了另一個閱讀大塊的例子。 – 2011-06-14 09:03:40

+0

他想要一個'List',因爲他不知道前面的大小。這是正確的解決方案,儘管它不使用「List」。 – 2011-06-14 09:09:26