I would go like this:
1) have a method to read a preset amount of bytes from the stream:
private byte[] read(int size) throws IOException {
byte[] result=new byte[size];
for (int index=0;index<size;index++) {
result[index]=in.readByte();
}
return result;
}
2) you will need a method to reconstruct the 2 bytes of length marker - I assume here a PC byte order, i.e. LSB - MSB:
private int byteArrayToWord(byte[] value) {
int ret = ((value[0] & 0xFF) ) | ((value[1] & 0xFF) << 8);
return ret;
}
3) then read your stuff as:
byte[] magic=read(4);
int len=byteArrayToWord(read(2));
String response=new String(read(len),StandardCharsets.UTF_16); // check the actual charset
Note the string constructor, which allows you to create a string from a byte array using the given encoding. This is the only really tricky part I guess :)