/** * Encodes a value of type {@link Length}. * * @param output * The {@link OutputStream} to write the value to. * @param value * The value to encode. * @throws NullPointerException * Thrown if either {@link output} or {@link value} is * <i>null</i>. */ public static void encode(OutputStream output, Length value) throws NullPointerException { Assert.AssertNotNull(output, "output"); Assert.AssertNotNull(value, "value"); if (value.isIndefinite()) { output.append(0x80); } else { final int lengthValue = value.value(); if (lengthValue <= 0x7F) { output.append(lengthValue); } else { output.append(0x80 | encodedLength(lengthValue)); encode(output, lengthValue); } } }
/** * Determines the encoded length for a value of type {@link Length}, which * is used when the content of a container is being encoded. * * @param value * The value to get the encoded length for. * @return The encoded length of the passed value. * @throws NullPointerException * Thrown if {@link value} is <i>null</i>. */ public static int encodedLength(Length value) throws NullPointerException { Assert.AssertNotNull(value, "value"); return 1 + ((value.value() < 0x80 || value.isIndefinite()) ? 0 : encodedLength(value.value())); }