2011-06-14 7 views
6

웨이브 파일의 바이트를 배열로 읽고 싶습니다. 읽은 바이트 수는 웨이브 파일의 크기에 따라 다르므로 최대 크기가 1000000 인 바이트 배열을 만듭니다. 그러나 배열 끝에 빈 값이 표시됩니다. 그래서 동적으로 증가하는 배열을 만들고 싶었고 ArrayList가 해결책이라는 것을 알았습니다. 그러나 AudioInputStream 클래스의 read() 함수는 바이트를 바이트 배열로만 읽습니다! 대신 ArrayList에 값을 전달합니까?바이트의 ArrayList 만들기

+0

바이트 배열로 수행 할 단계는 무엇입니까? 어쩌면 커다란 임시 버퍼가 필요하지 않을 수도 있습니다. – pcjuzer

답변

13

당신이 좋아하는 바이트의 배열을 가질 수 있습니다

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

이 배열

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

로 다시 변환하려면 (그런 다음 byte[]Byte[]를 변환하는 변환기를 작성해야합니다).

편집 : 당신은 List<Byte> 잘못을 사용하고, 당신이 얼마나 간단 ByteArrayOutputStreamAudioInputStream을 읽을 보여 단지 것이다. IOExceptionframeSize 경우는 슬로우

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

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

byte[] soundBytes = baos.toByteArray(); 

PS1은 동일하지 않다. 따라서,과 같이, 데이터를 판독하는 바이트 버퍼를 사용

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 : 기호를 찾을 수 없습니다. 기호 : 메서드 읽기 (java.util.List ) 위치 : 클래스 javax.sound.sampled.AudioInputStream ais.read (buffer); –

+0

이것은 컴파일되지 않습니다 :'List '을'byte []'로 변환하는 자동/내장 방법이 없습니다. 그 외에도'List '을 사용하는 것은 ** 공간이 비효율적입니다 **. –

+0

@ Joachim Sauer, 네 말이 맞아. Byte에서 Byte로 변환해야한다는 걸 잊어 버렸다. 답변 수정. –

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

죄송합니다. 나는 잠시 동안 자바를하지 않았다. 그리고 여기 때마다 더 바이트를 읽어 그것을하는 또 다른 방법 :

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

+0

그는 배열이 아닌'List'를 원합니다. 또한 청크로 읽는 대안적인 예를 제공했습니다. –

+0

그는 크기를 알지 못하기 때문에'List '를 원합니다. 이것은 올바른 해결책입니다. 심지어 'List'도 사용하지 않습니다. –