All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mailster.smtp.util.SharedStreamUtils Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package org.mailster.smtp.util;

import java.io.InputStream;

import javax.mail.util.SharedByteArrayInputStream;


/**
 * Shared streams utility methods.
 *
 * @author De Oliveira Edouard <[email protected]>
 */
public class SharedStreamUtils {

    public final static char[] SMTP_TERMINATOR = {'\r', '\n', '.', '\r', '\n'};

    /**
     * Calls @link {@link #getPrivateInputStream(boolean, InputStream)}
     * with the useCopy parameter set to true.
     */
    public static InputStream getPrivateInputStream(InputStream data) {
        return getPrivateInputStream(true, data);
    }

    /**
     * Provides a private unstuffed {@link InputStream} for each invocation unless
     * useCopy is false in which case the data stream
     * is unstuffed and returned. Unstuffing is made by encapsulating the stream within
     * special streams.
     *
     * @see org.mailster.smtp.util.CharTerminatedInputStream
     * @see org.mailster.smtp.util.DotUnstuffingInputStream
     */
    public static InputStream getPrivateInputStream(boolean useCopy, InputStream data) {
        var in = data;

        if (useCopy) {
            if (data instanceof SharedByteArrayInputStream) {
				in = ((SharedByteArrayInputStream) data).newStream(0, -1);
			} else if (data instanceof SharedTmpFileInputStream) {
				in = ((SharedTmpFileInputStream) data).newStream(0, -1);
			} else {
				throw new IllegalArgumentException("Unexpected data stream type : " + data.getClass().getName());
			}
        }

        in = new CharTerminatedInputStream(in, SMTP_TERMINATOR);
        in = new DotUnstuffingInputStream(in);

        return in;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy