java - 将 ByteInputStream 分配给 InputStream,保持绑定(bind)到终端仿真器

标签 java android stream inputstream

我正在尝试使用 android 中的库连接到终端仿真器,这将连接到串行设备并且应该显示发送/接收的数据。要附加到终端 session ,我需要向 setTermIn(InputStream) 提供一个 inputStream 并向 setTermOut(OutputStream)< 提供一个 outputStream/.

我在 onCreate() 中初始化并附加了一些流,这些只是初始流,没有附加到我想要发送/接收的数据。

private OutputStream bos;
private InputStream bis;

...

byte[] a = new byte[4096];
bis = new ByteArrayInputStream(a);
bos = new ByteArrayOutputStream();
session.setTermIn(bis);
session.setTermOut(bos);
/* Attach the TermSession to the EmulatorView. */
mEmulatorView.attachSession(session);

我现在想在发送和接收数据时将流分配给数据。在我每次按下回车键时调用的 sendData() 方法中,我有:

public void sendData(byte[] data)
{
        bos = new ByteArrayOutputStream(data.length);         
}

并且在 onReceiveData() 方法中,每次通过串行接收数据时调用:

public void onDataReceived(int id, byte[] data)
{
        bis = new ByteArrayInputStream(data);           
}

但是 ByteArrayInputStream 只能拥有提供给它的数据,因此需要在发送和接收数据时不断地创建它。现在的问题是,我希望这些数据出现在终端上,但是当我将 bis 分配给数据时,它不再像我调用 mEmulatorView.attachSession(session);/p>

有没有办法在不破坏 bis 与终端的绑定(bind)的情况下更新 bis 指向的内容?

编辑: 此外,如果我尝试再次调用 attach,我会收到错误消息,尽管理想情况下我不想再次调用它等等。

 SerialTerminalActivity.this.runOnUiThread(new Runnable() {
            public void run() {

                mSession.setTermIn(bis);
                mSession.setTermOut(bos);
                mEmulatorView.attachSession(mSession);
            }
          });

尽管那可能是我在那里的编码。 http://i.imgur.com/de8D5.png

最佳答案

包装 ByteArrayInputStream 添加您需要的功能。

举个例子:

public class MyBAIsWrapper implements InputStream {

   private ByteArrayInputStream wrapped;

   public MyBAIsWrapper(byte[] data) {
       wrapped=new ByteArrayInputStream(data);
   }

   //added method to refresh with new data
   public void renew(byte[] newData) {
       wrapped=new ByteArrayInpurStream(newData);
   }

   //implement the InputStreamMethods calling the corresponding methos on wrapped
   public int read() throws IOException {
      return wrapped.read();
   }

   public int read(byte[] b) throws IOException {
       return wrapped.read(b);
   }

   //and so on

}

然后,更改您的初始化代码:

byte[] a = new byte[4096];
bis = new MyBAIsWrapper(a);
session.setTermIn(bis);
//here, you could do somethin similar for OoutpuStream if needed, or keep the same initialization...
bos = new ByteArrayOutputStream();
session.setTermOut(bos);
/* Attach the TermSession to the EmulatorView. */
mEmulatorView.attachSession(session);

并更改onDataReceived方法更新输入流数据:

public void onDataReceived(int id, byte[] data)
{
    //cast added to keep original code structure 
    //I recomend define the bis attribute as the MyBAIsWrapper type in this case
    ((MyBAIsWrapper)bis).renew(data);
}

关于java - 将 ByteInputStream 分配给 InputStream,保持绑定(bind)到终端仿真器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13911931/

相关文章:

c++ - 如何在 C++ 中正确读取和覆盖同一个文件

c++ - 将 C 文件句柄分配给 C++ 文件流

Java URLrewrite 过滤器 - 传递参数

java - 为什么我的测试结果为空,而我已经模拟了依赖关系?

java - 使用Java加密/解密pdf文件

android - 在受信任的 Web Activity 中隐藏状态栏

sockets - 使用 Antlr 解析永无止境的流中的数据

java - 如何限制沙盒 Java 代码的性能?

android - Imageview 宽度 match_parent 需要水平居中

android - Android Studio中导入的模块找不到导入的类