@Override
    public void serialize(Object o, SerializationContext context) {
        BitField fieldParams = (BitField)context.getAnnotation(BitField.class);
        int length = BitUtility.getBitLength(fieldParams);
        if (length < 0) {
            throw new RuntimeException("Field length must be specified.");
        }
        
        long value;
        if (o instanceof Integer) {
            value = (Integer)o;
        } else if (o instanceof Byte) {
            value = (Byte)o;
        } else if (o instanceof Long) {
            value = (Long)o;
        } else {
            throw new RuntimeException("Value has unknown type [" + o.getClass().getName() + "].");
        }
        
        BitBuffer bitBuffer = context.getBuffer();
        byte[] buffer = bitBuffer.getBuffer();
        long offsetPosition = bitBuffer.getBitOffset() + bitBuffer.getBitPosition();
        
        int valueLength = length;
        while (valueLength > 0) {
            int bytePosition = (int)(offsetPosition >> 3);
            int bitPtr = valueLength;
            int freeBits = 8 - (int)(offsetPosition & 0x07);
            int rot = bitPtr - freeBits;
            byte mask = -1;
            if (valueLength < 8) {
                mask <<= 8 - valueLength;
            }
            mask = (byte)((mask & 0xff) >>> (offsetPosition & 0x07));
            
            byte data = (rot > 0) ? (byte)(value >> rot) : (byte)(value << -rot);
            data &= mask;
            buffer[bytePosition] &= ~mask;
            buffer[bytePosition] |= data;
            
            valueLength -= freeBits;
            offsetPosition += freeBits;
        }
        bitBuffer.moveBitPosition(length);
        
    }