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.filter.CoordinateFilter.java

public CoordinateFilter(String samfileheaderfilename, String regions_str) {
    String str = "";
    this.samfileheader = "";

    this.regions_str = regions_str;

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

        if (conf == null) {
            decodeSAMFileHeader();
            return;
        }

        FileSystem fs;

        try {
            if (FileSystem.getDefaultUri(conf) == null || FileSystem.getDefaultUri(conf).toString() == "")
                fs = FileSystem.get(new URI("hdfs://"), conf);
            else
                fs = FileSystem.get(conf);
        } catch (Exception e) {
            fs = FileSystem.get(new URI("hdfs://"), conf);
            System.out.println("WARNING: problems with filesystem config?");
            System.out.println("exception was: " + e.toString());
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(
                fs.open(new Path(fs.getHomeDirectory(), 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);
        p.setProperty("regionsstr", regions_str);
    } catch (Exception e) {
        System.out.println("ERROR: Unable to store SAMFileHeader in CoordinateFilter!");
    }

    this.samfileheader_decoded = getSAMFileHeader();
    populateRegions();
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoDigestService.java

public String md5(String data) {
    return Base64.encodeBase64String(DigestUtils.md5(data));
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * // w w  w .j a  va2s  . c  o  m
 * 
 * @param data  ?
 * @return  ?
 */
public static String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        byte e[] = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:acclaim.Acclaim.java

public Acclaim(String user, String password) {
    String creds = user + ":" + password;
    String base64 = Base64.encodeBase64String(creds.getBytes());
    authString = "Basic " + base64;
}

From source file:com.huawei.ais.demo.ocr.DriverLicenseDemo.java

private static void driverLicenseDemo() throws IOException {
    ///*from  ww  w . ja  va  2 s  .  c  o m*/
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    // 
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/driver-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/driver-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:com.huawei.ais.demo.ocr.VehicleLicenseDemo.java

private static void vehicleLicenseDemo() throws IOException {
    //// w ww.j a  v a  2 s  . c  om
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    //       
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/vehicle-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/vehicle-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:com.brienwheeler.lib.security.HmacSha256.java

public static String base64HmacSha256(String secretKey, String signData) {
    try {/*w w w  .ja  va2  s . c o m*/
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        hmacSha256.init(secretKeySpec);
        return Base64.encodeBase64String(hmacSha256.doFinal(signData.getBytes()));
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

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

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

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

    for (int i = 0; i < fieldNames.length; i++) {
        //System.out.println("field: "+fieldNames[i]);
        allFastqFieldNames.put(fieldNames[i], new Integer(i));
    }/* w w w. j  av  a 2 s  .co m*/

    if (!( /*allFastqFieldNames.containsKey("instrument")
           && allFastqFieldNames.containsKey("run_number")
           && allFastqFieldNames.containsKey("flow_cell_id")
           && allFastqFieldNames.containsKey("lane")
           && allFastqFieldNames.containsKey("tile")
           && allFastqFieldNames.containsKey("xpos")
           && allFastqFieldNames.containsKey("ypos")
           && allFastqFieldNames.containsKey("read")
           && allFastqFieldNames.containsKey("filter")
           && allFastqFieldNames.containsKey("control_number")
           && allFastqFieldNames.containsKey("index_sequence")*/
    allFastqFieldNames.containsKey("sequence") && allFastqFieldNames.containsKey("quality")))
        throw new IOException("Error: Incorrect Fastq tuple-field name or compulsory field missing");

    //BASE64Encoder encode = new BASE64Encoder();
    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;
    //p.setProperty("someproperty", "value");

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allFastqFieldNames);
    ostream.close();
    //datastr = encode.encode(bstream.toByteArray());
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allFastqFieldNames", datastr); //new String(bstream.toByteArray(), "UTF8"));
}

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

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

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

    for (int i = 0; i < fieldNames.length; i++) {
        //System.out.println("field: "+fieldNames[i]);
        allQseqFieldNames.put(fieldNames[i], new Integer(i));
    }//from  w ww .  ja  v a 2s  .co m

    if (!( /*allQseqFieldNames.containsKey("instrument")
           && allQseqFieldNames.containsKey("run_number")
           && allQseqFieldNames.containsKey("flow_cell_id")
           && allQseqFieldNames.containsKey("lane")
           && allQseqFieldNames.containsKey("tile")
           && allQseqFieldNames.containsKey("xpos")
           && allQseqFieldNames.containsKey("ypos")
           && allQseqFieldNames.containsKey("read")
           && allQseqFieldNames.containsKey("filter")
           && allQseqFieldNames.containsKey("control_number")
           && allQseqFieldNames.containsKey("index_sequence")*/
    allQseqFieldNames.containsKey("sequence") && allQseqFieldNames.containsKey("quality")))
        throw new IOException("Error: Incorrect Qseq tuple-field name or compulsory field missing");

    //BASE64Encoder encode = new BASE64Encoder();
    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;
    //p.setProperty("someproperty", "value");

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allQseqFieldNames);
    ostream.close();
    //datastr = encode.encode(bstream.toByteArray());
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allQseqFieldNames", datastr); //new String(bstream.toByteArray(), "UTF8"));
}

From source file:craterdog.utils.Base64Utils.java

/**
 * This function encodes a byte array using base 64 with a specific indentation of new lines.
 *
 * @param bytes The byte array to be encoded.
 * @param indentation The indentation string to be inserted before each new line.
 * @return The base 64 encoded string.// w w w  . java 2 s  . co m
 */
public static String encode(byte[] bytes, String indentation) {
    String encoded = Base64.encodeBase64String(bytes).replaceAll("\\s", ""); // remove all white space
    StringBuilder result = new StringBuilder();
    if (encoded.length() > 80) {
        for (int c = 0; c < encoded.length(); c++) {
            if (indentation != null && c % 80 == 0) {
                // format to indented 80 character blocks
                result.append("\n");
                result.append(indentation);
            }
            result.append(encoded.charAt(c));
        }
    } else {
        result.append(encoded);
    }
    return result.toString();
}