Problem
I read first byte in my socket client to check connection:
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN);
...
int dataInt = clientSocket.getInputStream().read();
Documentation:
Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the stream has been reached.
after that I want to splice this byte with next input string. I convert this integer byte to characters
b.putInt(dataInt);
byte[] dataByte = b.array();
final String data;
and check it. If dataInt != -1, I have to return this croped byte into new string:
if(dataInt != -1)
{
String c = new String(dataByte);
Log.v("MSG", c);
data = c + inToServer.readLine();
}
else
{
data = inToServer.readLine();
}
Why I see in log "MSG, ������MY MESSAGE"? How to get a string correctly?
Upd, how I send my messages:
byte[] buf = str.getBytes("UTF-8");
outToServer.write(buf, 0, buf.length);
outToServer.writeBytes("\n");
outToServer.flush();