public ByteArray readByteArray()
          throws IOException {
    int off = 0;
    int len = readInt();
    byte[] bytes = new byte[len];

    while (off < len) {
      off += read(bytes, off, len - off);
    }

    return new ByteArray(bytes);
  }