java - converting streams to blocks of strings and vice-versa -
i have developed java/scala xmpp client app sends data asynchronously using (say) write
method , receives data using listener
method. listener
method receives data discrete xmpp message packets , processes them using processpacket
method (which can modify based on want received data)
i want hook 3rd party library reads data inputstream
, writes outputstream
. specifically, want inputstream
of 3rd party library emulated using data received via listener
method , outputstream
emulated write
method.
what easiest way this? know requires conversion stream chunks of strings , vice-versa. hints appreciated.
the xmpp message packet structure follows (though can changed if needed):
<message = ... = ...><body>data</body></message>
use bytearrayinputstream
create input stream given string. have think encoding, because sending bytes instead of characters.
string text = message.getbody(); // that's need? inputstream = new bytearrayinputstream(text.getbytes("utf-8"));
for other way round, write bytearrayoutputstream
, create new string it's bytes:
string text = new string( baos.tobytearray(), "utf-8" );
again - don't forget think character encoding.
Comments
Post a Comment