socket.receive(packet);
int size = ByteBuffer.wrap(data).getInt();
LOGGER.log(Level.INFO, "nb bytes received : " + size);
if (size > MAX_SIZE) {
throw new ProtocolException(
"stream probably corrupted : received more than "
+ MAX_SIZE + " bytes (" + size + ")");
}
// receive actual data
data = new byte[size];
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
return new ReceivedMessage(new String(packet.getData()), packet.getSocketAddress());
// ByteBuffer bb = ByteBuffer.allocate(INT_SIZE);
// socket.getChannel().read(bb);
// int size = bb.getInt();
// bb = ByteBuffer.allocate(size);
// socket.getChannel().read(bb);
//
// return bb.asCharBuffer().rewind().toString();
} catch (SocketTimeoutException e) {
throw new TimeoutException("timeout in receive", e);
} catch (IOException e) {
throw new ProtocolException("error in receive", e);
}
}