CPD Results

The following document contains the results of PMD's CPD

Duplications

FileLine
javax/mail/internet/MimeBodyPart.java94
javax/mail/internet/MimeMessage.java611
    }

    /**
     * Return the content size of this message.  This is obtained
     * either from the size of the content field (if available) or
     * from the contentStream, IFF the contentStream returns a positive
     * size.  Returns -1 if the size is not available.
     *
     * @return Size of the content in bytes.
     * @exception MessagingException
     */
    public int getSize() throws MessagingException {
        if (content != null) {
            return content.length;
        }
        if (contentStream != null) {
            try {
                int size = contentStream.available();
                if (size > 0) {
                    return size;
                }
            } catch (IOException e) {
                // ignore
            }
        }
        return -1;
    }

    /**
     * Retrieve the line count for the current message.  Returns
     * -1 if the count cannot be determined.
     *
     * The Sun implementation always returns -1, so this version
     * does too.
     *
     * @return The content line count (always -1 in this implementation).
     * @exception MessagingException
     */
    public int getLineCount() throws MessagingException {
        return -1;
    }

    /**
     * Returns the current content type (defined in the "Content-Type"
     * header.  If not available, "text/plain" is the default.
     *
     * @return The String name of the message content type.
     * @exception MessagingException
     */
    public String getContentType() throws MessagingException {
        String value = getSingleHeader("Content-Type");
        if (value == null) {
            value = "text/plain";
        }
        return value;
    }


    /**
     * Tests to see if this message has a mime-type match with the
     * given type name.
     *
     * @param type   The tested type name.
     *
     * @return If this is a type match on the primary and secondare portion of the types.
     * @exception MessagingException
     */
    public boolean isMimeType(String type) throws MessagingException {
        return new ContentType(getContentType()).match(type);
    }

    /**
     * Retrieve the message "Content-Disposition" header field.
     * This value represents how the part should be represented to
     * the user.
     *
     * @return The string value of the Content-Disposition field.
     * @exception MessagingException
     */
    public String getDisposition() throws MessagingException {
        String disp = getSingleHeader("Content-Disposition");
        if (disp != null) {
            return new ContentDisposition(disp).getDisposition();
        }
        return null;
    }


    /**
     * Set a new dispostion value for the "Content-Disposition" field.
     * If the new value is null, the header is removed.
     *
     * @param disposition
     *               The new disposition value.
     *
     * @exception MessagingException
     */
    public void setDisposition(String disposition) throws MessagingException {
        if (disposition == null) {
            removeHeader("Content-Disposition");
        }
        else {
            // the disposition has parameters, which we'll attempt to preserve in any existing header.
            String currentHeader = getSingleHeader("Content-Disposition");
            if (currentHeader != null) {
                ContentDisposition content = new ContentDisposition(currentHeader);
                content.setDisposition(disposition);
                setHeader("Content-Disposition", content.toString());
            }
            else {
                // set using the raw string.
                setHeader("Content-Disposition", disposition);
            }
        }
    }

    /**
     * Decode the Content-Transfer-Encoding header to determine
     * the transfer encoding type.
     *
     * @return The string name of the required encoding.
     * @exception MessagingException
     */
    public String getEncoding() throws MessagingException {
        // this might require some parsing to sort out.
        String encoding = getSingleHeader("Content-Transfer-Encoding");
        if (encoding != null) {
            // we need to parse this into ATOMs and other constituent parts.  We want the first
            // ATOM token on the string.
            HeaderTokenizer tokenizer = new HeaderTokenizer(encoding, HeaderTokenizer.MIME);

            Token token = tokenizer.next();
            while (token.getType() != Token.EOF) {
                // if this is an ATOM type, return it.
                if (token.getType() == Token.ATOM) {
                    return token.getValue();
                }
            }
            // not ATOMs found, just return the entire header value....somebody might be able to make sense of
            // this.
            return encoding;
        }
        // no header, nothing to return.
        return null;
    }

    /**
     * Retrieve the value of the "Content-ID" header.  Returns null
     * if the header does not exist.
     *
     * @return The current header value or null.
     * @exception MessagingException
     */
    public String getContentID() throws MessagingException {
        return getSingleHeader("Content-ID");
    }

    public void setContentID(String cid) throws MessagingException {
        setOrRemoveHeader("Content-ID", cid);
    }

    public String getContentMD5() throws MessagingException {
        return getSingleHeader("Content-MD5");
    }

    public void setContentMD5(String md5) throws MessagingException {

FileLine
org/apache/geronimo/mail/util/Base64Encoder.java146
org/apache/geronimo/mail/util/Base64Encoder.java348
    public int decode(byte[] data, int off, int length, byte[] out) throws IOException
    {
        byte[]    bytes;
        byte    b1, b2, b3, b4;
        int        outLen = 0;

        int        end = off + length;

        while (end > 0)
        {
            if (!ignore((char)data[end - 1]))
            {
                break;
            }

            end--;
        }

        int  i = off;
        int  finish = end - 4;

        while (i < finish)
        {
            while ((i < finish) && ignore((char)data[i]))
            {
                i++;
            }

            b1 = decodingTable[data[i++]];

            while ((i < finish) && ignore((char)data[i]))
            {
                i++;
            }

            b2 = decodingTable[data[i++]];

            while ((i < finish) && ignore((char)data[i]))
            {
                i++;
            }

            b3 = decodingTable[data[i++]];

            while ((i < finish) && ignore((char)data[i]))
            {
                i++;
            }

            b4 = decodingTable[data[i++]];

            out[outLen++] = (byte)((b1 << 2) | (b2 >> 4));

FileLine
javax/mail/internet/MimeBodyPart.java451
javax/mail/internet/MimeMessage.java1163
        out.flush();
    }


    /**
     * Retrieve all headers that match a given name.
     *
     * @param name   The target name.
     *
     * @return The set of headers that match the given name.  These headers
     *         will be the decoded() header values if these are RFC 2047
     *         encoded.
     * @exception MessagingException
     */
    public String[] getHeader(String name) throws MessagingException {
        return headers.getHeader(name);
    }

    /**
     * Get all headers that match a particular name, as a single string.
     * Individual headers are separated by the provided delimiter.   If
     * the delimiter is null, only the first header is returned.
     *
     * @param name      The source header name.
     * @param delimiter The delimiter string to be used between headers.  If null, only
     *                  the first is returned.
     *
     * @return The headers concatenated as a single string.
     * @exception MessagingException
     */
    public String getHeader(String name, String delimiter) throws MessagingException {
        return headers.getHeader(name, delimiter);
    }

    /**
     * Set a new value for a named header.
     *
     * @param name   The name of the target header.
     * @param value  The new value for the header.
     *
     * @exception MessagingException
     */
    public void setHeader(String name, String value) throws MessagingException {
        headers.setHeader(name, value);
    }

    /**
     * Conditionally set or remove a named header.  If the new value
     * is null, the header is removed.
     *
     * @param name   The header name.
     * @param value  The new header value.  A null value causes the header to be
     *               removed.
     *
     * @exception MessagingException
     */
    private void setOrRemoveHeader(String name, String value) throws MessagingException {
        if (value == null) {
            headers.removeHeader(name);
        }
        else {
            headers.setHeader(name, value);
        }
    }

    /**
     * Add a new value to an existing header.  The added value is
     * created as an additional header of the same type and value.
     *
     * @param name   The name of the target header.
     * @param value  The removed header.
     *
     * @exception MessagingException
     */
    public void addHeader(String name, String value) throws MessagingException {
        headers.addHeader(name, value);
    }

    /**
     * Remove a header with the given name.
     *
     * @param name   The name of the removed header.
     *
     * @exception MessagingException
     */
    public void removeHeader(String name) throws MessagingException {
        headers.removeHeader(name);
    }

    /**
     * Retrieve the complete list of message headers, as an enumeration.
     *
     * @return An Enumeration of the message headers.
     * @exception MessagingException
     */
    public Enumeration getAllHeaders() throws MessagingException {
        return headers.getAllHeaders();
    }

    public Enumeration getMatchingHeaders(String[] names) throws MessagingException {

FileLine
javax/mail/internet/MimeUtility.java131
javax/mail/internet/MimeUtility.java234
        int offset = 0;
        int endOffset = text.length();

        int startWhiteSpace = -1;
        int endWhiteSpace = -1;

        StringBuffer decodedText = new StringBuffer(text.length());

        boolean previousTokenEncoded = false;

        while (offset < endOffset) {
            char ch = text.charAt(offset);

            // is this a whitespace character?
            if (linearWhiteSpace.indexOf(ch) != -1) {
                startWhiteSpace = offset;
                while (offset < endOffset) {
                    // step over the white space characters.
                    ch = text.charAt(offset);
                    if (linearWhiteSpace.indexOf(ch) != -1) {
                        offset++;
                    }
                    else {
                        // record the location of the first non lwsp and drop down to process the
                        // token characters.
                        endWhiteSpace = offset;
                        break;
                    }
                }
            }
            else {
                // we're at the start of a word token.  We potentially need to break this up into subtokens
                int wordStart = offset;

                while (offset < endOffset) {
                    // step over the white space characters.
                    ch = text.charAt(offset);
                    if (linearWhiteSpace.indexOf(ch) == -1) {
                        offset++;
                    }
                    else {
                        break;
                    }

                    //NB:  Trailing whitespace on these header strings will just be discarded.
                }
                // pull out the word token.
                String word = text.substring(wordStart, offset);

FileLine
javax/mail/internet/MimeBodyPart.java360
javax/mail/internet/MimeMessage.java891
    }

    public InputStream getInputStream() throws MessagingException, IOException {
        return getDataHandler().getInputStream();
    }

    protected InputStream getContentStream() throws MessagingException {
        if (contentStream != null) {
            return contentStream;
        }

        if (content != null) {
            return new ByteArrayInputStream(content);
        } else {
            throw new MessagingException("No content");
        }
    }

    public InputStream getRawInputStream() throws MessagingException {
        return getContentStream();
    }

    public synchronized DataHandler getDataHandler() throws MessagingException {
        if (dh == null) {
            dh = new DataHandler(new MimePartDataSource(this));
        }
        return dh;
    }

    public Object getContent() throws MessagingException, IOException {
        return getDataHandler().getContent();
    }

    public void setDataHandler(DataHandler handler) throws MessagingException {
        dh = handler;
        // if we have a handler override, then we need to invalidate any content
        // headers that define the types.  This information will be derived from the
        // data heander unless subsequently overridden.
        removeHeader("Content-Type");
        removeHeader("Content-Transfer-Encoding");
    }

    public void setContent(Object content, String type) throws MessagingException {

FileLine
javax/mail/internet/MimeBodyPart.java261
javax/mail/internet/MimeMessage.java778
    }

    public String getDescription() throws MessagingException {
        String description = getSingleHeader("Content-Description");
        if (description != null) {
            try {
                // this could be both folded and encoded.  Return this to usable form.
                return MimeUtility.decodeText(MimeUtility.unfold(description));
            } catch (UnsupportedEncodingException e) {
                // ignore
            }
        }
        // return the raw version for any errors.
        return description;
    }

    public void setDescription(String description) throws MessagingException {
        setDescription(description, null);
    }

    public void setDescription(String description, String charset) throws MessagingException {
        if (description == null) {
            removeHeader("Content-Description");
        }
        else {
            try {
                setHeader("Content-Description", MimeUtility.fold(21, MimeUtility.encodeText(description, charset, null)));
            } catch (UnsupportedEncodingException e) {
                throw new MessagingException(e.getMessage(), e);
            }
        }

    }

    public String[] getContentLanguage() throws MessagingException {

FileLine
org/apache/geronimo/mail/util/Base64DecoderStream.java98
org/apache/geronimo/mail/util/UUDecoderStream.java86
    }


    /**
     * Retrieve a single byte from the decoded characters buffer.
     *
     * @return The decoded character or -1 if there was an EOF condition.
     */
    private int getByte() throws IOException {
        if (!dataAvailable()) {
            if (!decodeStreamData()) {
                return -1;
            }
        }
        decodedCount--;
        return decodedChars[decodedIndex++];
    }

    private int getBytes(byte[] data, int offset, int length) throws IOException {

        int readCharacters = 0;
        while (length > 0) {
            // need data?  Try to get some
            if (!dataAvailable()) {
                // if we can't get this, return a count of how much we did get (which may be -1).
                if (!decodeStreamData()) {
                    return readCharacters > 0 ? readCharacters : -1;
                }
            }

            // now copy some of the data from the decoded buffer to the target buffer
            int copyCount = Math.min(decodedCount, length);
            System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount);
            decodedIndex += copyCount;
            decodedCount -= copyCount;
            offset += copyCount;
            length -= copyCount;
            readCharacters += copyCount;
        }
        return readCharacters;
    }

    /**
     * Verify that the first line of the buffer is a valid begin
     * marker.
     *
     * @exception IOException
     */
    private void checkBegin() throws IOException {

FileLine
org/apache/geronimo/mail/util/Hex.java57
org/apache/geronimo/mail/util/XText.java72
            throw new RuntimeException("exception encoding xtext string: " + e);
        }

        return bOut.toByteArray();
    }

    /**
     * xtext encode the byte data writing it to the given output stream.
     *
     * @return the number of bytes produced.
     */
    public static int encode(
        byte[]         data,
        OutputStream   out)
        throws IOException
    {
        return encoder.encode(data, 0, data.length, out);
    }

    /**
     * extext encode the byte data writing it to the given output stream.
     *
     * @return the number of bytes produced.
     */
    public static int encode(
        byte[]         data,
        int            off,
        int            length,
        OutputStream   out)
        throws IOException
    {
        return encoder.encode(data, 0, data.length, out);
    }

    /**
     * decode the xtext encoded input data. It is assumed the input data is valid.
     *
     * @return a byte array representing the decoded data.
     */
    public static byte[] decode(
        byte[]    data)
    {
        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();

        try
        {
            encoder.decode(data, 0, data.length, bOut);
        }
        catch (IOException e)
        {
            throw new RuntimeException("exception decoding xtext string: " + e);

FileLine
javax/mail/internet/AddressParser.java1641
javax/mail/internet/AddressParser.java1682
            StringBuffer buffer = new StringBuffer(s.length() + 2);
            buffer.append('"');
            buffer.append(s);
            buffer.append('"');
            return buffer.toString();
        }

        // get a buffer sufficiently large for the string, two quote characters, and a "reasonable"
        // number of escaped values.
        StringBuffer buffer = new StringBuffer(s.length() + 10);
        buffer.append('"');

        // now check all of the characters.
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            // character requiring escaping?
            if (ch == '\\' || ch == '"') {
                // add an extra backslash
                buffer.append('\\');
            }
            // and add on the character
            buffer.append(ch);
        }
        buffer.append('"');
        return buffer.toString();
    }

    public class TokenStream {

FileLine
javax/mail/internet/MimeBodyPart.java522
javax/mail/internet/MimeMessage.java1338
        DataHandler handler = getDataHandler();

        try {
            // figure out the content type.  If not set, we'll need to figure this out.
            String type = dh.getContentType();
            // parse this content type out so we can do matches/compares.
            ContentType content = new ContentType(type);

            // is this a multipart content?
            if (content.match("multipart/*")) {
                // the content is suppose to be a MimeMultipart.  Ping it to update it's headers as well.
                try {
                    MimeMultipart part = (MimeMultipart)handler.getContent();
                    part.updateHeaders();
                } catch (ClassCastException e) {
                    throw new MessagingException("Message content is not MimeMultipart", e);
                }
            }
            else if (!content.match("message/rfc822")) {
                // simple part, we need to update the header type information
                // if no encoding is set yet, figure this out from the data handler content.
                if (getSingleHeader("Content-Transfer-Encoding") == null) {
                    setHeader("Content-Transfer-Encoding", MimeUtility.getEncoding(handler));
                }

                // is a content type header set?  Check the property to see if we need to set this.
                if (getSingleHeader("Content-Type") == null) {