Example usage for javax.mail.internet MimeUtility encode

List of usage examples for javax.mail.internet MimeUtility encode

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility encode.

Prototype

public static OutputStream encode(OutputStream os, String encoding) throws MessagingException 

Source Link

Document

Wrap an encoder around the given output stream.

Usage

From source file:com.glaf.core.security.DigestUtil.java

public static void digestFile(String filename, String algorithm) {
    byte[] b = new byte[65536];
    int read = 0;
    FileInputStream fis = null;/*from  w w w  .j a v  a 2 s  .  co m*/
    FileOutputStream fos = null;
    OutputStream encodedStream = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        fis = new FileInputStream(filename);
        while (fis.available() > 0) {
            read = fis.read(b);
            md.update(b, 0, read);
        }
        byte[] digest = md.digest();
        StringBuffer fileNameBuffer = new StringBuffer(256).append(filename).append('.').append(algorithm);
        fos = new FileOutputStream(fileNameBuffer.toString());
        encodedStream = MimeUtility.encode(fos, "base64");
        encodedStream.write(digest);
        fos.flush();
    } catch (Exception ex) {
        throw new RuntimeException("Error computing Digest: " + ex);
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(encodedStream);
    }
}

From source file:org.apache.jetspeed.util.Base64.java

public static String encodeAsString(byte[] plaindata) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream inStream = new ByteArrayOutputStream();

    inStream.write(plaindata, 0, plaindata.length);

    // pad//  w w  w . j  a v  a  2  s . c  o  m
    if ((plaindata.length % 3) == 1) {
        inStream.write(0);
        inStream.write(0);
    } else if ((plaindata.length % 3) == 2) {
        inStream.write(0);
    }

    inStream.writeTo(MimeUtility.encode(out, "base64"));
    return out.toString();
}

From source file:com.adaptris.core.services.Base64EncodeService.java

/**
 * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage)
 *///from   ww w . j a  va2 s. c  o m
public void doService(AdaptrisMessage msg) throws ServiceException {

    OutputStream out = null;
    InputStream in = null;
    try {
        in = msg.getInputStream();
        out = MimeUtility.encode(msg.getOutputStream(), MimeConstants.ENCODING_BASE64);
        IOUtils.copy(in, out);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.glaf.core.security.DigestUtil.java

public static String digestString(String password, String algorithm) {
    if (password == null || password.trim().length() == 0) {
        return password;
    }/*  w ww  .  jav a  2s  .c o  m*/
    if (conf.getBoolean("password.enc", true)) {
        MessageDigest md = null;
        ByteArrayOutputStream bos = null;
        OutputStream encodedStream = null;
        try {
            md = MessageDigest.getInstance(algorithm);
            byte[] digest = md.digest(password.getBytes("UTF-8"));
            bos = new ByteArrayOutputStream();
            encodedStream = MimeUtility.encode(bos, "base64");
            encodedStream.write(digest);
            return bos.toString("UTF-8");
        } catch (IOException ioe) {
            throw new RuntimeException("Fatal error: " + ioe);
        } catch (NoSuchAlgorithmException ae) {
            throw new RuntimeException("Fatal error: " + ae);
        } catch (MessagingException me) {
            throw new RuntimeException("Fatal error: " + me);
        } finally {
            IOUtils.closeQuietly(bos);
            IOUtils.closeQuietly(encodedStream);
        }
    }
    return password;
}

From source file:net.sf.vntconverter.VntConverter.java

/**
 * Enkodiert einen Java-Unicode-String in einen UTF-8-QUOTED-PRINTABLE-String.
 *//*from   w w  w .j a  v a 2 s  .c  om*/
public String encode(String in) {
    try {
        InputStream input = new ReaderInputStream(new StringReader(in), "UTF-8");
        StringWriter sw = new StringWriter();
        OutputStream output = MimeUtility.encode(new WriterOutputStream(sw), "quoted-printable");
        copyAndClose(input, output);
        return sw.toString().replaceAll("=\\x0D\\x0A", "").replaceAll("\\x0D\\x0A", "=0D=0A");
    } catch (Exception e) {
        throw new RuntimeException("Exception caught in VntConverter.encode(in):", e);
    }
}

From source file:com.adaptris.core.services.aggregator.MimeAggregator.java

private static byte[] encodeData(byte[] data, String encoding, InternetHeaders hdrs)
        throws MessagingException, IOException {
    if (!isBlank(encoding)) {
        hdrs.setHeader(HEADER_CONTENT_ENCODING, encoding);
    }//from   www .  j av  a 2 s. c om
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
            OutputStream encodedOut = MimeUtility.encode(out, encoding)) {
        encodedOut.write(data);
        return out.toByteArray();
    }
}

From source file:com.adaptris.mail.MailSenderImp.java

/**
 * Return the encoded payload./*from ww  w.  j a  va  2 s  .c o  m*/
 * <p>
 * In the situation where we are directly creating the <code>MimeBodypart</code> with a set of InternetHeaders and a byte payload,
 * we have to encode the payload ourselves, as no encoding appears to be done when the body part is written. This is implied by
 * the documentation for MimeBodyPart.
 * </p>
 *
 * @return the bytes
 * @param unencoded the payload in its raw form.
 * @param header the existing InternetHeaders so that the appropriate content transfer encoding header can be set.
 * @param encoding for this particular part of the message
 * @see MimeBodypart#MimeBodyPart(InternetHeaders, byte[])
 */
private byte[] encode(byte[] unencoded, InternetHeaders header, String encoding)
        throws MessagingException, IOException {

    byte[] encoded = unencoded;
    if (encoding != null) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream encodedOut = MimeUtility.encode(out, encoding);
        encodedOut.write(unencoded);
        encodedOut.flush();
        encodedOut.close();
        encoded = out.toByteArray();
        header.setHeader(Mail.CONTENT_ENCODING, encoding);
    }
    return encoded;
}

From source file:com.adaptris.util.text.mime.MultiPartOutput.java

private static OutputStream wrap(OutputStream original, String encoding, InternetHeaders hdrs)
        throws MessagingException {
    OutputStream encodedOut = original;
    if (encoding != null) {
        encodedOut = MimeUtility.encode(original, encoding);
        hdrs.setHeader(HEADER_CONTENT_ENCODING, encoding);
    }// ww  w  . j  ava 2  s  . co  m
    return encodedOut;
}

From source file:com.aliyun.odps.local.common.utils.LocalRunUtils.java

public static String toReadableString(byte[] b) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream printableos = MimeUtility.encode(baos, "quoted-printable");
    printableos.write(b);// w w  w. j  av  a  2 s.  c o  m
    printableos.close();
    return new String(baos.toByteArray(), Charset.forName("UTF-8"));
}

From source file:com.zacwolf.commons.email.Email.java

private static void dataurlEncode(final org.jsoup.nodes.Element img, final EmailAttachment attachment)
        throws IOException, MessagingException {
    String mime_type = attachment.contenttype;
    InputStream is = attachment.getDataHandler().getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream b64os = MimeUtility.encode(baos, "base64");
    IOUtils.copy(is, b64os);//from w w w.j a v  a  2 s  . co  m
    b64os.close();
    img.attr("src", "data:" + mime_type + ";base64," + new String(baos.toByteArray()));
}