简体   繁体   English

Java NIO管道和ByteBuffer

[英]Java NIO Pipe and ByteBuffer

I'm trying to pipe some ByteBuffer in a thread ( IO1 ) to another ( IO2 ). 我想pipe一些ByteBuffer在一个线程( IO1 )到另一个( IO2 )。

http://tutorials.jenkov.com/java-nio/pipe.html http://tutorials.jenkov.com/java-nio/pipe.html

private int bufferSize;
private boolean isRecording;

private Thread IO1;
private Thread IO2;

private ByteBuffer byteBuffer1;
private ByteBuffer byteBuffer2;

private Pipe pipe;
private Pipe.SinkChannel skChannel;
private Pipe.SourceChannel sourceChannel;

            byteBuffer1 = ByteBuffer.allocateDirect(bufferSize);
            byteBuffer2 = ByteBuffer.allocateDirect(bufferSize);

            //An instance of Pipe is created
            try
            {
                pipe = Pipe.open();
                skChannel = pipe.sink();
                sourceChannel = pipe.source();

                IO1.start();
                IO2.start();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

-- -

IO1 = new Thread(new Runnable()
{
    public void run()
    {
        isRecording = true;
        recorder.startRecording();
        try
        {
            int read;
            while (isRecording)
            {
               // byteBuffer1.clear();
                read = recorder.read(byteBuffer1, bufferSize);
                if (AudioRecord.ERROR_INVALID_OPERATION != read)
                {
                       skChannel.write(byteBuffer1);
                       Log.v("========IO1 ", String.valueOf(read));
                       //This triggered almost 20 times/second
                }
            }
            recorder.stop();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
});

skChannel.write(byteBuffer1); and Log.v("========IO1 ", String.valueOf(read)); Log.v("========IO1 ", String.valueOf(read)); is triggered almost 20 times/second, and this is the expected behavior, so far so good. 几乎以每秒20次的速度被触发,这是目前为止预期的行为。

IO2 = new Thread(new Runnable()
{
    public void run()
    { 
        try
        {
            int read;
            while (  (read =sourceChannel.read(byteBuffer2)) >0)
            {
                Log.v("========IO2 ", String.valueOf(read));
                //this triggered only once

                // To do Codec etc.
                //............
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        } 
        Log.v("========IO2 ", "END!!!!!"); //never triggered(this is fine)
    }
});

However, Log.v("========IO2 ", String.valueOf(read)); 但是, Log.v("========IO2 ", String.valueOf(read)); is triggered only once, and I don't know why. 只被触发一次,我不知道为什么。

Can someone tell me how can I obtain the update of Thread IO1 in IO2 ? 有人可以告诉我如何在IO2获取线程IO1的更新吗?

Thanks. 谢谢。

You need to flip() the buffer before writing, and compact() it afterwards. 您需要在写入之前翻转()缓冲区,然后再压缩()。

BUT: In a word, don't. 但是:总之,不要。 Pipes between threads are basically pointless. 线程之间的管道基本上毫无意义。 Use a queue, or have the receiving thread read the sending thread's input directly. 使用队列,或者让接收线程直接读取发送线程的输入。

If you must do this, the basic NIO copy loop goes like this: 如果必须执行此操作,则基本的NIO复制循环如下所示:

while (in.read(buffer) > 0 || buffer.position() > 0) // or whatever your read API needs
{
    buffer.flip();
    out.write(buffer);
    buffer.compact();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM