0
這是我以前的問題here的後續。I/O輸出取決於讀者的緩衝區大小
當我使用像1024 * 32大小的例子中的字節數組時,生成的文件應該是一個波形文件。 如果我使用的只是32字節的較小尺寸,一個字節就像
fstr.write(this.stream.read());
它很完美。
以下代碼:
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();
}
}
}