I am working on an Android application (obviously in Java) and have recently updated my UDP reader code. In both versions, I configured several buffers and received a UDP packet:
byte[] buf = new byte[10000];
short[] soundData = new short[1000];
DatagramPacket packet = new DatagramPacket (buf, buf.length);
socket.receive (packet);
In the original version, I put the data back one byte at a time (this is actually 16 PCM audio data):
for (int i = 0; i < count; i++)
soundData[i] = (short) (((buf[k++]&0xff) << 8) + (buf[k++]&0xff));
In the updated version, I used some cool Java tools that I did not know about when I started:
bBuffer = ByteBuffer.wrap (buf);
sBuffer = bBuffer.asShortBuffer();
sBuffer.get (soundData, 0, count);
"" ( ). , , - , , . , JVM-, , , , , , .
, Java NIO, , , mo 'betta' .
- , Java UDP- " "??
,
R.