2011-10-04 2 views
0

이것은 나의 이전 질문 here에 대한 후속 조치입니다.독자의 버퍼 크기에 따른 입출력 출력

크기가 1024 * 32 인 예제 에서처럼 웨이브 파일이어야하는 결과 파일이 너무 많습니다. 32 바이트 또는 더 작은 크기 같은 작은 크기를 사용하는 경우 1 바이트는

fstr.write(this.stream.read()); 

과 같이 완벽하게 작동합니다. 코드에 따라

: 입력이 32K의 배수가 아닌 경우

import java.io.*; 

class ErrorThread extends Thread { 
    InputStream stream = null; 

    public ErrorThread(InputStream stream) { 
    this.stream = stream; 
    } 

    public void run() { 
    try { 
     byte[] buf = new byte[32 * 1024]; 
     int nRead = 0; 
     while ((nRead = this.stream.read()) != -1) { 

     } 
     this.stream.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

class InputThread extends Thread { 
    InputStream stream = null; 

    public InputThread(InputStream stream) { 
    this.stream = stream; 
    } 

    public void run() { 
    try { 
     FileOutputStream fstr = new FileOutputStream("test.wav"); 
     int nRead = 0; 
     byte[] buf = new byte[1024 * 32]; 
     while ((nRead = this.stream.read(buf, 0, buf.length)) != -1) { 
     fstr.write(buf, 0 , buf.length); 
     } 
     this.stream.close(); 
     fstr.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

public class Test { 
    public static void main(String[] args) { 
    try { 
     Process p = new ProcessBuilder("lame", "--decode", "test.mp3", "-").start(); 
     ErrorThread et = new ErrorThread(p.getErrorStream()); 
     InputThread it = new InputThread(p.getInputStream()); 
     et.start(); 
     it.start(); 
     p.waitFor(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

답변

1
fstr.write(buf, 0 , buf.length); 

fstr.write(buf, 0 , nRead); 

해야한다, 아웃 버퍼에서 먹다 남은 음식을 기록하고 있습니다.