private static void decode(final String str, int inOffset, final int inLen,
            final byte[] output, int outOffset, final int outLen)
    {
        int stop = (outLen / 3) * 3;
        byte b0, b1, b2, b3;
        try
        {
            while (outOffset < stop)
            {
                b0 = code2nibble[str.charAt(inOffset++)];
                b1 = code2nibble[str.charAt(inOffset++)];
                b2 = code2nibble[str.charAt(inOffset++)];
                b3 = code2nibble[str.charAt(inOffset++)];
                if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
                    throw new IllegalArgumentException("Not B64 encoded");

                output[outOffset++] = (byte) (b0 << 2 | b1 >>> 4);
                output[outOffset++] = (byte) (b1 << 4 | b2 >>> 2);
                output[outOffset++] = (byte) (b2 << 6 | b3);
            }

            if (outLen != outOffset)
            {
                switch (outLen % 3)
                {
                    case 0:
                        break;
                    case 1:
                        b0 = code2nibble[str.charAt(inOffset++)];
                        b1 = code2nibble[str.charAt(inOffset++)];
                        if (b0 < 0 || b1 < 0)
                            throw new IllegalArgumentException("Not B64 encoded");
                        output[outOffset++] = (byte) (b0 << 2 | b1 >>> 4);
                        break;
                    case 2:
                        b0 = code2nibble[str.charAt(inOffset++)];
                        b1 = code2nibble[str.charAt(inOffset++)];
                        b2 = code2nibble[str.charAt(inOffset++)];
                        if (b0 < 0 || b1 < 0 || b2 < 0)
                            throw new IllegalArgumentException("Not B64 encoded");
                        output[outOffset++] = (byte) (b0 << 2 | b1 >>> 4);
                        output[outOffset++] = (byte) (b1 << 4 | b2 >>> 2);
                        break;

                    default:
                        throw new IllegalStateException("should not happen");
                }
            }
        }
        catch (IndexOutOfBoundsException e)
        {
            throw new IllegalArgumentException("char " + inOffset
                    + " was not B64 encoded");
        }
    }
Returns the length of the decoded base64 input (written to the provided {@code output} byte array). The
{@code output} byte array must have enough capacity or it will fail.
protostuff-api/src/main/java/io/protostuff/B64Code.java