Java 将ByteInputStream分配给InputStream,并保持绑定到终端仿真器
我正在尝试使用android中的库连接到终端仿真器,这将连接到串行设备,并应显示发送/接收的数据。要连接到终端会话,我需要向Java 将ByteInputStream分配给InputStream,并保持绑定到终端仿真器,java,android,stream,inputstream,Java,Android,Stream,Inputstream,我正在尝试使用android中的库连接到终端仿真器,这将连接到串行设备,并应显示发送/接收的数据。要连接到终端会话,我需要向setTermIn(inputStream)提供inputStream,向setterout(outputStream)提供outputStream 我在onCreate()中初始化并附加了一些流,它们只是初始流,没有附加到我想要发送/接收的数据 private OutputStream bos; private InputStream bis; ... byte[]
setTermIn(inputStream)
提供inputStream
,向setterout(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()方法中(我每次按enter键都会调用该方法),我有:
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(会话)时那样被附加代码>
有没有一种方法可以在不破坏bis与终端的绑定的情况下更新bis所指向的内容
编辑:
此外,如果我尝试再次调用attach,我会出错,尽管理想情况下我不想再次调用它,等等
SerialTerminalActivity.this.runOnUiThread(new Runnable() {
public void run() {
mSession.setTermIn(bis);
mSession.setTermOut(bos);
mEmulatorView.attachSession(mSession);
}
});
虽然那可能是我在那里的代码。
包装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);
}
包装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);
}
谢谢,我真的很喜欢你的答案,并使用了它。但是问题仍然存在,所以我认为我的代码中的其他地方有问题。谢谢,我非常喜欢你的答案并使用了它。但是问题仍然存在,因此我认为代码中的其他地方存在问题。