Since you have byte[]
data (array), there's a simple way of reading the data directly into a byte[]
.
InputStream stream = socket.getInputStream();
byte[] data = new byte[30];
int count = stream.read(data);
This would read this at once and return the number of occurences read.