private static byte[] readBytes(InputStream is)
    throws IOException
  {
    int size = readInteger(is);
    int read;
    if (size == -1)
      return null;

    byte[] bytes = new byte[size];
    int total = 0;
    while (total < bytes.length && (read = is.read(bytes, total, bytes.length - total)) != -1)
      total += read;

    if (total < bytes.length)
      throw new IOException("Failed to read enough bytes: " + total);

    int cr = is.read();
    int lf = is.read();
    if (cr != CR || lf != LF)
      throw new IOException("Improper line ending: " + cr + ", " + lf);

    return bytes;
  }