简体   繁体   中英

Reassign input/output stream?

I am trying to connect to a terminal emulator using a library in android, this will connect to a serial device and should show me sent/received data. To attach to a terminal session I need to provide an inputstream to setTermIn(InputStream) and an outputstream to setTermOut(OutputStream) .

I initialize and attach some streams like so in onCreate() , these are just initial streams and are not attached to the data I want to be sending/receiving.

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);

I now want to assign the streams to data as I send and receive it, but I think I am doing it wrong. In The sendData() method, which I call every time I press enter, I have:

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

and in the onReceiveData() method, called every time data is received over serial:

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

I am not seeing any data on my terminal screen, but I am sending and receiving it over serial successfully. So my question is, should I be setting the streams every single time I am sending and receiving data or just set them once. Also do I need to attach them to the terminal session again mEmulatorView.attachSession(session) somewhere or should the new streams automatically be sent to the screen?

My theory is that my terminal is attached to the old streams and that is why I can't see data on the terminal screen. Would this be correct?

I tried to set the new input/output streams in each method just once using a boolean and an if statement, but then I get warning messages in logcat

RuntimeException 'sending message to a Handler on a dead thread'

I've edited it to write and rad now based on answered but I notice that the library has it's own write method to feed data to the terminal, so I don't even know what the streams are for if that is that case, and I need this write to write to the emulator?

public void write(byte[] data,
              int offset,
              int count)
Write data to the terminal output. The written data will be consumed by the emulation     client as input.
write itself runs on the main thread. The default implementation writes the data into a     circular buffer and signals the writer thread to copy it from there to the OutputStream.

Subclasses may override this method to modify the output before writing it to the  stream, but implementations in derived classes should call through to this method to do the  actual writing.

Parameters:
data - An array of bytes to write to the terminal.
offset - The offset into the array at which the data starts.
count - The number of bytes to be written.

Objects in java are passed by reference, hence if you do

bos = new ByteArrayOutputStream(data.length)

You're essentially throwing away the previous outputstream and creating a new one.

Try keeping reference to your input and output stream and write data into it, eg:

bos.write(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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