使用 Java 读取/写入 linux 管道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1632069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:21:29  来源:igfitidea点击:

Read/Write to linux Pipe using Java

javapipenamed-pipes

提问by amit.bhayani

My query is on what is the best way to read / write to a linux Pipe in Java? I already am using the java.io.RandomAccessFile like

我的问题是在 Java 中读取/写入 linux Pipe 的最佳方式是什么?我已经在使用 java.io.RandomAccessFile 之类的

   RandomAccessFile file = new RandomAccessFile("/dev/zap/16", "rw");

and then passing it to worker thread which reads it after every 2ms as

然后将其传递给工作线程,工作线程每 2 毫秒读取一次

  byte[] buffer = new byte[16];
  file.read(buffer);

It does read it from Pipe, but I suspect that some bytes are overwritten. Do you know how linux (ubuntu) handles the buffer for pipe's?

它确实从 Pipe 中读取它,但我怀疑某些字节被覆盖了。你知道 linux (ubuntu) 如何处理管道的缓冲区吗?

回答by sfussenegger

I haven't ever tried this myself but what your doing feels just wrong. Linux pipes are First in - First out (FIFO) by definition. Hence you should only be able to read bytes in the same order as you've written them - not randomly. I'd suggest to use a normal Fileinstead and it should work fine.

我自己从来没有尝试过这个,但你所做的感觉是错误的。Linux 管道根据定义是先进先出 (FIFO)。因此,您应该只能以与写入它们相同的顺序读取字节 - 而不是随机读取。我建议改用普通的File,它应该可以正常工作。

回答by Joachim Sauer

Pipes aren't handled in any way special by Java as far as I know. You simply open the file for writing and write to it.

据我所知,Java 并没有以任何特殊的方式处理管道。您只需打开文件进行写入并写入即可。

You can't really "overwrite" anything in a pipe, since you can't seek in a pipe. For the same reason a RandomAccessFileisn't the smartest choice to use (since a pipe is explicitely nota random access file). I'd suggest using a FileOutputStreaminstead.

你不能真正“覆盖”管道中的任何东西,因为你不能在管道中寻找。出于同样的原因, aRandomAccessFile不是最明智的选择(因为管道显然不是随机访问文件)。我建议使用 aFileOutputStream代替。

Also note that read()is not guaranteed to read until the buffer is full! It can read a single byte as well and you need to check its return value and possibly loop to read the full buffer.

另请注意,read()在缓冲区已满之前不能保证读取!它也可以读取单个字节,您需要检查其返回值并可能循环读取完整的缓冲区。

回答by Anurag Uniyal

I think you may not be flushing after writing, so do OutputStream.flush() often and read may be a byte at time, at least to see if your data is getting thru. e.g. to start with open a named pipe in readonly mode(FileInputStream) in process1, open it in write mode(FileOutputStream ) in process2, so anything you write in process2 will be read in process1.

我认为您在写入后可能不会刷新,因此经常使用 OutputStream.flush() 并且读取可能是一个字节,至少可以查看您的数据是否通过。例如,首先在 process1 中以只读模式(FileInputStream)打开命名管道,在 process2 中以写入模式(FileOutputStream)打开它,因此您在 process2 中写入的任何内容都将在 process1 中读取。

also read

还读

http://www.tldp.org/LDP/lpg/node15.html
http://www.unixguide.net/unix/programming/2.10.5.shtmlhttp://www.unixguide.net/unix/programming/2.10.6.shtml

http://www.tldp.org/LDP/lpg/node15.html
http://www.unixguide.net/unix/programming/2.10.5.shtml http://www.unixguide.net/unix/programming/ 2.10.6.shtml