Java String Encode by Charset encodeBase64(String s, Charset cs)

Here you can find the source of encodeBase64(String s, Charset cs)

Description

encode Base

License

Apache License

Declaration

public static String encodeBase64(String s, Charset cs) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

public class Main {
    public static String encodeBase64(String s, Charset cs) {
        String out = null;//from   w ww  .j a  v  a  2 s  . c  o  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            OutputStream os = MimeUtility.encode(baos, "BASE64");
            os.write(s.getBytes(cs));
            out = new String(baos.toByteArray(), cs);
        } catch (IOException ignore) {
            ;
        } catch (MessagingException ignore) {
            ;
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ignore) {
                    ;
                }
            }
        }
        return out;
    }
}

Related

  1. encode(final String str, final Charset charset)
  2. encode(String charsetName, char[] chars, int offset, int length)
  3. encode(String s, Charset encoding)
  4. encode(String text, Charset charset)
  5. encode(String value, Charset charset)
  6. encodeCHARSET(String string, Charset charset)
  7. encodeFormFields(final String content, final Charset charset)
  8. encodeOneChar(CharsetEncoder encoder, int uchar)
  9. encoding(final String content, final Charset charset, final boolean blankAsPlus)