public static void writeNumber(OutputStream output, long value, int width)
            throws SerializationException {
        if (width < 0) {
            throw new SerializationException("Negative width: " + width);
        }
        if (width < 8 && value >= (1L << (8 * width))) {
            throw new SerializationException(
                    "Number too large, " + value + " does not fit in " + width + " bytes");
        }

        try {
            while (width > 0) {
                long shift = (width - 1) * 8L;
                // Java behaves weirdly if shifting by more than the variable's size
                if (shift < Long.SIZE) {
                    output.write((byte) ((value >> shift) & 0xFF));
                } else {
                    output.write(0);
                }

                width--;
            }
        } catch (IOException e) {
            throw new SerializationException(e);
        }
    }
Write a number to the output stream.
The number is encoded in big endian order.
@param value the value to be written.
@param width the width of the encoded number, in bytes
@throws SerializationException if the number is too large to fit in {@code width} bytes or
{@code width} is negative.
common/src/main/java/org/conscrypt/ct/Serialization.java