code: public static void writeBigInteger(BigInteger integer, DataOutputStream out)
              throws IOException {
          if (integer.signum() == -1) {
              throw new IllegalStateException("Negative BigInteger!");
          }
          byte[] buf = integer.toByteArray();
          if (buf.length > Short.MAX_VALUE)
              throw new IllegalStateException("Too long: " + buf.length);
          out.writeShort((short) buf.length);
          out.write(buf);
      }
docs: Write a (reasonably short) BigInteger to a stream.
  
  @param integer the BigInteger to write
  @param out     the stream to write it to
  @throws IOException I/O exception