Example usage for org.apache.commons.codec.binary Base64 encodeBase64String

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64String

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64String.

Prototype

public static String encodeBase64String(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:fi.aalto.seqpig.io.BamStorer.java

public BamStorer(String samfileheaderfilename) {

    String str = "";
    this.samfileheader = "";

    try {/*from  www  .  j a  v a2 s .c  o m*/
        Configuration conf = UDFContext.getUDFContext().getJobConf();

        // see https://issues.apache.org/jira/browse/PIG-2576
        if (conf == null || conf.get("mapred.task.id") == null) {
            // we are running on the frontend
            decodeSAMFileHeader();
            return;
        }

        URI uri = new URI(samfileheaderfilename);
        FileSystem fs = FileSystem.get(uri, conf);

        BufferedReader in = new BufferedReader(new InputStreamReader(fs.open(new Path(samfileheaderfilename))));

        while (true) {
            str = in.readLine();

            if (str == null)
                break;
            else
                this.samfileheader += str + "\n";
        }

        in.close();
    } catch (Exception e) {
        System.out.println("ERROR: could not read BAM header from file " + samfileheaderfilename);
        System.out.println("exception was: " + e.toString());
    }

    try {
        Base64 codec = new Base64();
        Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());

        ByteArrayOutputStream bstream = new ByteArrayOutputStream();
        ObjectOutputStream ostream = new ObjectOutputStream(bstream);
        ostream.writeObject(this.samfileheader);
        ostream.close();
        String datastr = codec.encodeBase64String(bstream.toByteArray());
        p.setProperty("samfileheader", datastr);
    } catch (Exception e) {
        System.out.println("ERROR: Unable to store SAMFileHeader in BamStorer!");
    }

    this.samfileheader_decoded = getSAMFileHeader();
}

From source file:fi.aalto.seqpig.io.BamStorer.java

@Override
public void checkSchema(ResourceSchema s) throws IOException {

    selectedBAMAttributes = new HashMap<String, Integer>();
    allBAMFieldNames = new HashMap<String, Integer>();
    String[] fieldNames = s.fieldNames();

    for (int i = 0; i < fieldNames.length; i++) {
        System.out.println("field: " + fieldNames[i]);
        allBAMFieldNames.put(fieldNames[i], new Integer(i));

        if (fieldNames[i].equalsIgnoreCase("RG") || fieldNames[i].equalsIgnoreCase("LB")
                || fieldNames[i].equalsIgnoreCase("PU") || fieldNames[i].equalsIgnoreCase("PG")
                || fieldNames[i].equalsIgnoreCase("AS") || fieldNames[i].equalsIgnoreCase("SQ")
                || fieldNames[i].equalsIgnoreCase("MQ") || fieldNames[i].equalsIgnoreCase("NM")
                || fieldNames[i].equalsIgnoreCase("H0") || fieldNames[i].equalsIgnoreCase("H1")
                || fieldNames[i].equalsIgnoreCase("H2") || fieldNames[i].equalsIgnoreCase("UQ")
                || fieldNames[i].equalsIgnoreCase("PQ") || fieldNames[i].equalsIgnoreCase("NH")
                || fieldNames[i].equalsIgnoreCase("IH") || fieldNames[i].equalsIgnoreCase("HI")
                || fieldNames[i].equalsIgnoreCase("MD") || fieldNames[i].equalsIgnoreCase("CS")
                || fieldNames[i].equalsIgnoreCase("CQ") || fieldNames[i].equalsIgnoreCase("CM")
                || fieldNames[i].equalsIgnoreCase("R2") || fieldNames[i].equalsIgnoreCase("Q2")
                || fieldNames[i].equalsIgnoreCase("S2") || fieldNames[i].equalsIgnoreCase("CC")
                || fieldNames[i].equalsIgnoreCase("CP") || fieldNames[i].equalsIgnoreCase("SM")
                || fieldNames[i].equalsIgnoreCase("AM") || fieldNames[i].equalsIgnoreCase("MF")
                || fieldNames[i].equalsIgnoreCase("E2") || fieldNames[i].equalsIgnoreCase("U2")
                || fieldNames[i].equalsIgnoreCase("OQ")) {

            System.out.println("selected attribute: " + fieldNames[i] + " i: " + i);
            selectedBAMAttributes.put(fieldNames[i], new Integer(i));
        }//  w  w w  .  jav a  2 s . c om
    }

    if (!(allBAMFieldNames.containsKey("name") && allBAMFieldNames.containsKey("start")
            && allBAMFieldNames.containsKey("end") && allBAMFieldNames.containsKey("read")
            && allBAMFieldNames.containsKey("cigar") && allBAMFieldNames.containsKey("basequal")
            && allBAMFieldNames.containsKey("flags") && allBAMFieldNames.containsKey("insertsize")
            && allBAMFieldNames.containsKey("mapqual") && allBAMFieldNames.containsKey("matestart")
            && allBAMFieldNames.containsKey("materefindex") && allBAMFieldNames.containsKey("refindex")))
        throw new IOException("Error: Incorrect BAM tuple-field name or compulsory field missing");

    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(selectedBAMAttributes);
    ostream.close();
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("selectedBAMAttributes", datastr);

    bstream = new ByteArrayOutputStream();
    ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allBAMFieldNames);
    ostream.close();
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allBAMFieldNames", datastr);
}

From source file:fi.aalto.seqpig.io.SamStorer.java

public SamStorer(String samfileheaderfilename) {

    String str = "";
    this.samfileheader = "";

    try {/*from w w  w. j  a  va2 s  .c om*/
        Configuration conf = UDFContext.getUDFContext().getJobConf();

        // see https://issues.apache.org/jira/browse/PIG-2576
        if (conf == null || conf.get("mapred.task.id") == null) {
            // we are running on the frontend
            decodeSAMFileHeader();
            return;
        }

        URI uri = new URI(samfileheaderfilename);
        FileSystem fs = FileSystem.get(uri, conf);

        BufferedReader in = new BufferedReader(new InputStreamReader(fs.open(new Path(samfileheaderfilename))));

        while (true) {
            str = in.readLine();

            if (str == null)
                break;
            else
                this.samfileheader += str + "\n";
        }

        in.close();
    } catch (Exception e) {
        System.out.println("ERROR: could not read SAM header from file " + samfileheaderfilename);
        System.out.println("exception was: " + e.toString());
    }

    try {
        Base64 codec = new Base64();
        Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());

        ByteArrayOutputStream bstream = new ByteArrayOutputStream();
        ObjectOutputStream ostream = new ObjectOutputStream(bstream);
        ostream.writeObject(this.samfileheader);
        ostream.close();

        String datastr = codec.encodeBase64String(bstream.toByteArray());
        p.setProperty("samfileheader", datastr);
    } catch (Exception e) {
        System.out.println("ERROR: Unable to store SAMFileHeader in BamStorer!");
    }

    this.samfileheader_decoded = getSAMFileHeader();
}

From source file:fi.aalto.seqpig.io.SamStorer.java

@Override
public void checkSchema(ResourceSchema s) throws IOException {

    selectedSAMAttributes = new HashMap<String, Integer>();
    allSAMFieldNames = new HashMap<String, Integer>();
    String[] fieldNames = s.fieldNames();

    for (int i = 0; i < fieldNames.length; i++) {
        System.out.println("field: " + fieldNames[i]);
        allSAMFieldNames.put(fieldNames[i], new Integer(i));

        if (fieldNames[i].equalsIgnoreCase("RG") || fieldNames[i].equalsIgnoreCase("LB")
                || fieldNames[i].equalsIgnoreCase("PU") || fieldNames[i].equalsIgnoreCase("PG")
                || fieldNames[i].equalsIgnoreCase("AS") || fieldNames[i].equalsIgnoreCase("SQ")
                || fieldNames[i].equalsIgnoreCase("MQ") || fieldNames[i].equalsIgnoreCase("NM")
                || fieldNames[i].equalsIgnoreCase("H0") || fieldNames[i].equalsIgnoreCase("H1")
                || fieldNames[i].equalsIgnoreCase("H2") || fieldNames[i].equalsIgnoreCase("UQ")
                || fieldNames[i].equalsIgnoreCase("PQ") || fieldNames[i].equalsIgnoreCase("NH")
                || fieldNames[i].equalsIgnoreCase("IH") || fieldNames[i].equalsIgnoreCase("HI")
                || fieldNames[i].equalsIgnoreCase("MD") || fieldNames[i].equalsIgnoreCase("CS")
                || fieldNames[i].equalsIgnoreCase("CQ") || fieldNames[i].equalsIgnoreCase("CM")
                || fieldNames[i].equalsIgnoreCase("R2") || fieldNames[i].equalsIgnoreCase("Q2")
                || fieldNames[i].equalsIgnoreCase("S2") || fieldNames[i].equalsIgnoreCase("CC")
                || fieldNames[i].equalsIgnoreCase("CP") || fieldNames[i].equalsIgnoreCase("SM")
                || fieldNames[i].equalsIgnoreCase("AM") || fieldNames[i].equalsIgnoreCase("MF")
                || fieldNames[i].equalsIgnoreCase("E2") || fieldNames[i].equalsIgnoreCase("U2")
                || fieldNames[i].equalsIgnoreCase("OQ")) {

            System.out.println("selected attribute: " + fieldNames[i] + " i: " + i);
            selectedSAMAttributes.put(fieldNames[i], new Integer(i));
        }//w  ww  .  ja  v a  2  s .  c  o  m
    }

    if (!(allSAMFieldNames.containsKey("name") && allSAMFieldNames.containsKey("start")
            && allSAMFieldNames.containsKey("end") && allSAMFieldNames.containsKey("read")
            && allSAMFieldNames.containsKey("cigar") && allSAMFieldNames.containsKey("basequal")
            && allSAMFieldNames.containsKey("flags") && allSAMFieldNames.containsKey("insertsize")
            && allSAMFieldNames.containsKey("mapqual") && allSAMFieldNames.containsKey("matestart")
            && allSAMFieldNames.containsKey("materefindex") && allSAMFieldNames.containsKey("refindex")))
        throw new IOException("Error: Incorrect SAM tuple-field name or compulsory field missing");

    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(selectedSAMAttributes);
    ostream.close();
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("selectedSAMAttributes", datastr);

    bstream = new ByteArrayOutputStream();
    ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allSAMFieldNames);
    ostream.close();
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allSAMFieldNames", datastr);
}

From source file:com.sshutils.utils.CryptHelper.java

public static String encrypt(String strToEncrypt) {
    try {//from w  ww.  j  av a 2s  . c om

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:br.com.topsys.cd.applet.AutenticacaoApplet.java

@Override
public void complemento(CertificadoDigital certificadoDigital) {
    try {// w  w w .j av  a2  s.  c om

        JSObject window = JSObject.getWindow(this);
        window.call("setCertificado", new Object[] {
                Base64.encodeBase64String(certificadoDigital.getX509Certificado().getEncoded()) });
        this.closeSuccess();

    } catch (ExcecaoCertificado ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "Aviso", JOptionPane.ERROR_MESSAGE);
        this.closeError();
    }

}

From source file:com.connorbrezinsky.msg.security.Hash.java

/**
 * Computes a salted PBKDF2 hash of given plaintext password suitable for
 * storing in a database. Empty passwords are not supported.
 *///from w  w  w.  j ava 2s  .  com
public static String getSaltedHash(String password) throws Exception {
    byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
    // store the salt with the password
    return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
}

From source file:com.bloatit.framework.utils.Hash.java

/**
 * From a password and a salt, returns the corresponding digest
 * //from www . ja v a  2s.  co  m
 * @param password The password to encrypt
 * @param salt The salt
 * @return String The digested password in base64
 */
public static String calculateHash(final String password, final String salt) {
    byte[] value = (password + salt).getBytes();
    for (int i = 0; i < ITERATION_NUMBER; i++) {
        value = DigestUtils.sha512(value);
    }
    return Base64.encodeBase64String(value);
}

From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java

public static String encrypt(String key1, String iv1, String value) {
    try {// w  w  w  .  jav  a  2s .  c  o m
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        //System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);/*ww  w.j  a va2  s. c  o  m*/

    return encryptedToken;
}