lojix/power_model/src/main/com/thesett/aima/attribute/time/ByteBufferUtils.java
public static ByteBuffer putPaddedInt32AsString(ByteBuffer buffer, int value, int length)
    {
        // Ensure there is sufficient space in the buffer to hold the result.
        int charsRequired = BitHackUtils.getCharacterCountInt32(value);
        length = (charsRequired < length) ? length : charsRequired;

        // Take an explicit index into the buffer to start writing to, as the numbers will be written backwards.
        int index = buffer.position() + length - 1;

        // Record the start position, to remember if a minus sign was written or not, so that it does not get
        // overwritten by the zero padding.
        int start = buffer.position();

        // Advance the buffer position manually, as the characters will be written to specific indexes backwards.
        buffer.position(buffer.position() + length);

        // Take care of the minus sign for negative numbers.
        if (value < 0)
        {
            buffer.put(MINUS_ASCII);
            start++; // Stop padding code overwriting minus sign.
        }
        else
        {
            value = -value;
        }

        // Write the digits least significant to most significant into the buffer. As the number was converted to be
        // negative the remainders will be negative too.
        do
        {
            int remainder = value % 10;
            value = value / 10;

            buffer.put(index--, ((byte) (ZERO_ASCII - remainder)));
        }
        while (value != 0);

        // Write out the padding zeros.
        while (index >= start)
        {
            buffer.put(index--, ZERO_ASCII);
        }

        return buffer;
    }
public static ByteBuffer putPaddedInt32AsString(ByteBuffer buffer, int value, int length)
    {
        // Ensure there is sufficient space in the buffer to hold the result.
        int charsRequired = BitHackUtils.getCharacterCountInt32(value);
        length = (charsRequired < length) ? length : charsRequired;

        // Take an explicit index into the buffer to start writing to, as the numbers will be written backwards.
        int index = buffer.position() + length - 1;

        // Record the start position, to remember if a minus sign was written or not, so that it does not get
        // overwritten by the zero padding.
        int start = buffer.position();

        // Advance the buffer position manually, as the characters will be written to specific indexes backwards.
        buffer.position(buffer.position() + length);

        // Take care of the minus sign for negative numbers.
        if (value < 0)
        {
            buffer.put(MINUS_ASCII);
            start++; // Stop padding code overwriting minus sign.
        }
        else
        {
            value = -value;
        }

        // Write the digits least significant to most significant into the buffer. As the number was converted to be
        // negative the remainders will be negative too.
        do
        {
            int remainder = value % 10;
            value = value / 10;

            buffer.put(index--, ((byte) (ZERO_ASCII - remainder)));
        }
        while (value != 0);

        // Write out the padding zeros.
        while (index >= start)
        {
            buffer.put(index--, ZERO_ASCII);
        }

        return buffer;
    }
Writes the specified integer value as an ASCII string into the specified byte buffer. If the integer value is
shorted than the specified length, the number will be padded with leading zeros, so that it fills the required
length. If there is insufficient space in the buffer to write the value into, then the buffer size is increased
using the supplied byte buffer pool.

@param  buffer The byte buffer to write to.
@param  value  The value to write.
@param  length The length to pad the number to with leading zeros.

@return The byte buffer with the value written to it. This may be a different buffer to the one passed in if the
buffer had to be copied in order to increase its size.