Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So what are you saying is the problem then? That writing to the BufferedOutputStream for each character is CPU-inefficient and he should be doing this?

    public static void main(String[] args) throws IOException {
        byte[] buffer = new byte[1 << 12];
        for (;;) {
            int nRead = System.in.read(buffer);
            if (nRead == -1) return;
            System.out.write(buffer, 0, nRead);
        }
    }
If so, fair enough, but it's reasonable to go with the simpler solution if you're not given any particular performance requirements.


No, I'm saying you are already using a BufferedInputStream and a BufferedOutputStream. That is the problem: you aren't guaranteeing your output matches your input.

Try doing this:

    dd if=/dev/random of=test_file bs=4000 count=100
    java YourClass < test_file > test_output
    diff test_file test_output


Here's the simpler, likely slower, but nonetheless actually correct solution: https://gist.github.com/cbsmith/9755809


Oh, gotcha. The code above with "byte[] buffer" also works since write(byte[], int, int) flushes.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: