code: private int getLengthFromByteArray(byte[] bin){
          ByteBuffer bb = ByteBuffer.wrap(bin);
          bb.order(ByteOrder.BIG_ENDIAN);
          return bb.getShort();
      }
docs: Returns length of packet by reading byte array values.