public static int decode()

in commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/QuotedPrintableDecoder.java [39:84]


    public static int decode(final byte[] data, final OutputStream out) throws IOException {
        var off = 0;
        final var length = data.length;
        final var endOffset = off + length;
        var bytesWritten = 0;

        while (off < endOffset) {
            final var ch = data[off++];

            // space characters were translated to '_' on encode, so we need to translate them back.
            if (ch == '_') {
                out.write(' ');
            } else if (ch == '=') {
                // we found an encoded character. Reduce the 3 char sequence to one.
                // but first, make sure we have two characters to work with.
                if (off + 1 >= endOffset) {
                    throw new IOException("Invalid quoted printable encoding; truncated escape sequence");
                }

                final var b1 = data[off++];
                final var b2 = data[off++];

                // we've found an encoded carriage return. The next char needs to be a newline
                if (b1 == '\r') {
                    if (b2 != '\n') {
                        throw new IOException("Invalid quoted printable encoding; CR must be followed by LF");
                    }
                    // this was a soft linebreak inserted by the encoding. We just toss this away
                    // on decode.
                } else {
                    // this is a hex pair we need to convert back to a single byte.
                    final var c1 = hexToBinary(b1);
                    final var c2 = hexToBinary(b2);
                    out.write(c1 << UPPER_NIBBLE_SHIFT | c2);
                    // 3 bytes in, one byte out
                    bytesWritten++;
                }
            } else {
                // simple character, just write it out.
                out.write(ch);
                bytesWritten++;
            }
        }

        return bytesWritten;
    }