There will be something like:
String s = new String( bytearray, 0, lenOfValidData, "US-ASCII");
do what you want (change encoding to any encoding)?
Update:
Based on your comments, you can try:
socket.receive(packet);
String strPacket = new String( packet.getData(), 0, packet.getLength(), "US-ASCII");
receiver.onReceive( strPacket);
Java, , packet.getLength() ( , ). , :
String strPacket = new String( packet.getData(),
0,
Math.min( packet.getLength(), packet.getData().length),
"US-ASCII");
.