private static byte[] receiveBoundedBytes(final InputStream steam) throws IOException {
      int toRead = 4; // 32-bit integer
      byte[] sizeBytes = new byte[toRead];
      if (steam.read(sizeBytes, 0, toRead) != toRead) {
        throw new IOException("Premature end of stream");
      }
      toRead =
          Math.min(
              ByteBuffer.wrap(sizeBytes).order(ByteOrder.LITTLE_ENDIAN).getInt(),
              1 << 20 /* 1 MiB */);
      byte[] byteData = new byte[toRead];
      if (steam.read(byteData, 0, toRead) != toRead) {
        throw new IOException("Unexpected end of stream");
      }
      return byteData;
    }
Simple thread class that receives command data.
java/ui/src/main/java/com/google/security/zynamics/bindiff/socketserver/SocketServer.java