通过Java写入unix管道的问题

时间:2010-01-31 00:58:56

标签: java pipe outputstream

我正在写一个位于“/ dev / fb0”的帧缓冲区。一切正常,直到我尝试使用一个挂起程序的OutputStream再次写入管道。我通过关闭输出流然后重新创建它来解决这个问题,但这看起来非常缓慢而且生硬。

Framebuffer.java

public class Framebuffer extends Autobuffer {
private FileOutputStream out = null;
private File pipe = null;

public Framebuffer() {
   super(320, 240);
}

public Framebuffer(File pipe) {
   super(320, 240);
   try {
      out = new FileOutputStream(pipe);
   } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 this.pipe = pipe;
 }

 public void sync() throws IOException {
   out.write(getBytes());
   out.close();
   out = new FileOutputStream(pipe);
 }
 }

public class Framebuffer extends Autobuffer { private FileOutputStream out = null; private File pipe = null; public Framebuffer() { super(320, 240); } public Framebuffer(File pipe) { super(320, 240); try { out = new FileOutputStream(pipe); } catch (FileNotFoundException e) { e.printStackTrace(); } this.pipe = pipe; } public void sync() throws IOException { out.write(getBytes()); out.close(); out = new FileOutputStream(pipe); } }

有什么想法吗?

感谢。

3 个答案:

答案 0 :(得分:2)

首先,除非发生一些非常奇怪的事情,否则“/ dev / fb0”是设备文件而不是管道。 [这是一个挑剔,但如果你使用错误的术语,1)人们不会理解你,2)你将难以寻找答案。]

其次,这看起来像是一种与帧缓冲区交互的奇怪方式!

我怀疑问题是你需要做一个等效的POSIX lseek调用,每次绘制一个帧时将流位置设置为零。我找到了两种方法:

答案 1 :(得分:1)

将输出流更改为RandomAccessFile修复了我的所有问题。我敢打赌,流不能正常工作,因为它无法寻找位置0.感谢所有回复的人。

答案 2 :(得分:0)

如果使用flush(来自OutputStream)刷新输出怎么办?