Hi,
I'm experiencing an interesting problem. I am trying to send a file over a socket connection as a byte stream, but also send with it some control messages as text ('data incoming', number of bytes to read from the stream etc). This all works fine - when I need to read the control messages, I fill a byte buffer from the known start byte of this message until the line feed character ('\n'; UTF-8 character 10), and then convert that byte series into a
string.
An interesting problem however is that the rest of the program has been written to use a BufferedReader over the socket stream, and I don't really want to change all instances of the BufferedReader's use to my slightly ad hoc string converting mechanism described above.
The reason I created the mechanism above to start with is because, if a BufferedReader and BufferedInputStream are created over the same InputStream, the buffers are not synchronised. I.e. If the two are initialised by:
and the code
is called, when the subsequent code
is called, the five bytes read by the BufferedInputStream will be read again.
Really, I believe this is a problem with individual buffering for the two streams. If I could do something like this, where the BufferedReader and BufferedInputStream share the buffer:
that would be perfect, but it doesn't look like you can.
Even if you read some bytes using the byte stream, and then create a new reader object to read some characters, the bytes for the characters seem to have been 'consumed' by the buffered input stream (not because they are read (read() is called), just because they are buffered (as far as I can tell)), and so are gone by the time that the reader tries to read them. And since I am sending files, I suspect that the input stream needs to be buffered for performance reasons.
Does anyone have any thoughts on ways to accomplish what I am looking to do - have a buffered reader and input stream over the same incoming socket connection, which are synchronised such that one starts reading where the other left off?
Thanks
