public int decode()

in geronimo-mail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoder.java [332:401]


    public int decode(final byte[] data, int off, final int length, final OutputStream out) throws IOException {
        // make sure we're writing to the correct stream
        this.out = out;

        final int endOffset = off + length;
        int bytesWritten = 0;

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

            // space characters are a pain.  We need to scan ahead until we find a non-space character.
            // if the character is a line terminator, we need to discard the blanks.
            if (ch == ' ') {
                int trailingSpaces = 1;
                // scan forward, counting the characters.
                while (off < endOffset && data[off] == ' ') {
                    // step forward and count this.
                    off++;
                    trailingSpaces++;
                }
                // is this a lineend at the current location?
                if (off >= endOffset || data[off] == '\r' || data[off] == '\n') {
                    // go to the next one
                    continue;
                }
                else {
                    // make sure we account for the spaces in the output count.
                    bytesWritten += trailingSpaces;
                    // write out the blank characters we counted and continue with the non-blank.
                    while (trailingSpaces-- > 0) {
                        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");
                }
                // convert the two bytes back from hex.
                byte b1 = data[off++];
                byte 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");
                    }
                    // 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.
                    b1 = decodingTable[b1];
                    b2 = decodingTable[b2];
                    out.write((b1 << 4) | b2);
                    // 3 bytes in, one byte out
                    bytesWritten++;
                }
            }
            else {
                // simple character, just write it out.
                out.write(ch);
                bytesWritten++;
            }
        }

        return bytesWritten;
    }