Problems writing to a unix pipe through Java
I am writing to a framebuffer located at "/dev/fb0". Everything works fine until I try to write again to the pipe using an OutputStream, which hangs the program. I have resolved this by closing the output stream and then recreating it, but this seems awfully slow and blunt.
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.clo开发者_StackOverflowse();
out = new FileOutputStream(pipe);
}
}
Any ideas?
Thanks.
Firstly, unless something really weird is going on, "/dev/fb0" is a device file not a pipe. [This is a nitpick, but if you use the wrong terminology, 1) people won't understand you and 2) you will have difficulty searching for answers.]
Secondly, this looks like a weird way to interact with a framebuffer!!
I suspect that the problem is that you need to do the equivalent of a POSIX lseek
call to set the stream position to zero each time you draw a frame. I've found two ways to do this:
Use RandomAccessFile instead of OutputStream / FileOutputStream, and call seek(long) to seek the file.
Call FileOutputStream.getChannel(), and then use position(long) to seek the file.
Changing the Output Stream to RandomAccessFile fixed all of my problems. I bet the stream wasn't working because it can't seek to position 0. Thanks to all who replied.
What if you flush your output with flush (from OutputStream)?
精彩评论