Search code examples
javainputstreamprocessbuilder

I/O output depending on reader's buffer size


this is a followup on my former question here.

The resulting file which should be a wave file is by far too lage when I am using a byte array like in the example with size 1024 * 32. If I am using a smaller size like only 32 bytes or even do a single byte like

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

it works perfectly.

Following code:

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

Solution

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

    should be

        fstr.write(buf, 0 , nRead);
    

    If the input isn't a multiple of 32K, you're writing the leftovers in the buffer out.