1

I am attempting to use the BinaryStdOut.java class provided by Robert Sedgwick in his Algorithms 4E textbook. The code for this class is freely available on his website, but for ease of reference, I will show relevant snippets of it here.

In the class mentioned above, a BufferedOutputStream is declared as follows

    private static BufferedOutputStream out = new BufferedOutputStream(System.out);

This should, if my understanding is correct, allow for out.write() to essentially print to the standard out just as if I were to use System.out.print().

In order to test this, I built a program whose main functions was simply as follows

    public static void main(String[] args) {
    BinaryStdOut.write(1);
}

This would pass the integer 1 to the BinaryStdOut's write() method, which is as follows

    public static void write(int x) {
    writeByte((x >>> 24) & 0xff);
    writeByte((x >>> 16) & 0xff);
    writeByte((x >>>  8) & 0xff);
    writeByte((x >>>  0) & 0xff);
}

The writeByte() code is:

private static void writeByte(int x) {
    assert x >= 0 && x < 256;

    // optimized if byte-aligned
    if (N == 0) {
        try { out.write(x); }
        catch (IOException e) { e.printStackTrace(); }
        return;
    }

    // otherwise write one bit at a time
    for (int i = 0; i < 8; i++) {
        boolean bit = ((x >>> (8 - i - 1)) & 1) == 1;
        writeBit(bit);
    }
}

Now my problem is that the test code does not appear to do anything. It compiles and runs successfully, but nothing gets printed in my Terminal as it would if I did a System.out.print() with the same integer.

In order to see if the problem was with the BinaryStdOut class, I copied the BufferedOutputStream declaration right into my main program and then attempted to write directly to that stream, with the same result.

Is there something that I am missing with using BufferedOutputStream.write()?

EDIT: My test program's main function currently looks like this:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    BufferedOutputStream out = new BufferedOutputStream(System.out);
    try {out.write(16);
    out.flush();
    out.close();}
    catch(IOException e){
        System.out.println("error");
    }
}
4

3 回答 3

1

写入缓冲区后,您需要刷新缓冲区:

// optimized if byte-aligned
if (N == 0) {
    try { 
        out.write(x); 
        out.flush();
    }
    catch (IOException e) { e.printStackTrace(); }
    return;
}

您的测试程序似乎没有打印任何内容的原因是因为您正在打印一个DLE,它是一个控制字符,并且不会出现在标准输出中。

于 2013-11-26T05:57:08.697 回答
0

这为我打印了 hello world,您是否选择了一些没有可显示字形的字节?而且您可能不应该那样关闭System.out(在 BuffedOutputStream 上调用 close 会关闭它并释放与流关联的任何系统资源)。

public static void main(String[] args) {
  BufferedOutputStream out = new BufferedOutputStream(
      System.out);
  String msg = "Hello, World!";
  try {
    for (char c : msg.toCharArray()) {
      out.write(c);
    }
    out.flush();
  } catch (IOException e) {
    System.out.println("error");
  }
}
于 2013-11-26T05:59:24.900 回答
0

是的,您需要flush以及为什么需要flush是因为Buffered流使用:

缓冲输出流:

public BufferedOutputStream(OutputStream out) {
    this(out, 8192);//default buffer size
    }
public synchronized void write(int b) throws IOException {
    if (count >= buf.length) {
        flushBuffer();
    }
    buf[count++] = (byte)b;
    }

因此,在缓冲区充满 8KB 数据之前,内容将保持缓冲状态,不会被输出到控制台。

你应该在控制台上write(16)看到 print char(至少是 Eclipse IDE),如果你打印 1601 - 1626 你应该看到A-Z打印的字符。

于 2013-11-26T06:03:22.563 回答