Example usage for java.util Base64 getDecoder

List of usage examples for java.util Base64 getDecoder

Introduction

In this page you can find the example usage for java.util Base64 getDecoder.

Prototype

public static Decoder getDecoder() 

Source Link

Document

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);//from  w  w  w.ja  v a 2 s.  c  o m
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:ClientEncryptionServerMultipart.java

public static void main(String... args) {

    String privateKeyPath = "PATH/.ssh/id_rsa";
    String publicKeyId = "04:92:7b:23:bc:08:4f:d7:3b:5a:38:9e:4a:17:2e:df";
    String multipartServer = "https://MANTA_MULTIPART_SERVER";

    ConfigContext config = new ChainedConfigContext(new DefaultsConfigContext(), new EnvVarConfigContext(),
            new MapConfigContext(System.getProperties())).setMantaURL(multipartServer)
                    .setMantaUser(mantaUsername).setMantaKeyPath(privateKeyPath).setMantaKeyId(publicKeyId)
                    .setClientEncryptionEnabled(true).setEncryptionAlgorithm("AES256/CTR/NoPadding")
                    .setEncryptionAuthenticationMode(EncryptionAuthenticationMode.Optional)
                    .setPermitUnencryptedDownloads(false).setEncryptionKeyId("simple/example")
                    .setEncryptionPrivateKeyBytes(
                            Base64.getDecoder().decode("RkZGRkZGRkJEOTY3ODNDNkM5MUUyMjIyMTExMTIyMjI="));

    try (MantaClient client = new MantaClient(config)) {
        EncryptedServerSideMultipartManager multipart = new EncryptedServerSideMultipartManager(client);
        multipartUpload(multipart);//w ww . j  ava  2  s.co  m
    }
}

From source file:io.fabric8.apiman.gateway.ApimanGatewayStarter.java

/**
 * Main entry point for the Apiman Gateway micro service.
 * @param args the arguments//from  www  .  j a va2  s  .  co  m
 * @throws Exception when any unhandled exception occurs
 */
public static final void main(String[] args) throws Exception {

    String isTestModeString = Systems.getEnvVarOrSystemProperty(APIMAN_GATEWAY_TESTMODE, "false");
    boolean isTestMode = "true".equalsIgnoreCase(isTestModeString);
    if (isTestMode)
        log.info("Apiman Gateway Running in TestMode");

    String isSslString = Systems.getEnvVarOrSystemProperty(APIMAN_GATEWAY_SSL, "false");
    isSsl = "true".equalsIgnoreCase(isSslString);
    log.info("Apiman Gateway running in SSL: " + isSsl);
    String protocol = "http";
    if (isSsl)
        protocol = "https";

    URL elasticEndpoint = null;

    File gatewayConfigFile = new File(APIMAN_GATEWAY_PROPERTIES);
    String esUsername = null;
    String esPassword = null;
    if (gatewayConfigFile.exists()) {
        PropertiesConfiguration config = new PropertiesConfiguration(gatewayConfigFile);
        esUsername = config.getString("es.username");
        esPassword = config.getString("es.password");
        if (Utils.isNotNullOrEmpty(esPassword))
            esPassword = new String(Base64.getDecoder().decode(esPassword), StandardCharsets.UTF_8).trim();
        setConfigProp(APIMAN_GATEWAY_ES_USERNAME, esUsername);
        setConfigProp(APIMAN_GATEWAY_ES_PASSWORD, esPassword);
    }
    log.info(esUsername + esPassword);

    // Require ElasticSearch and the Gateway Services to to be up before proceeding
    if (isTestMode) {
        URL url = new URL(protocol + "://localhost:9200");
        elasticEndpoint = waitForDependency(url, "elasticsearch-v1", "status", "200", esUsername, esPassword);
    } else {
        String defaultEsUrl = protocol + "://elasticsearch-v1:9200";
        String esURL = Systems.getEnvVarOrSystemProperty(APIMAN_GATEWAY_ELASTICSEARCH_URL, defaultEsUrl);
        URL url = new URL(esURL);
        elasticEndpoint = waitForDependency(url, "elasticsearch-v1", "status", "200", esUsername, esPassword);
        log.info("Found " + elasticEndpoint);
    }

    File usersFile = new File(APIMAN_GATEWAY_USER_PATH);
    if (usersFile.exists()) {
        setConfigProp(Users.USERS_FILE_PROP, APIMAN_GATEWAY_USER_PATH);
    }

    log.info("** ******************************************** **");

    Fabric8GatewayMicroService microService = new Fabric8GatewayMicroService(elasticEndpoint);

    if (isSsl) {
        microService.startSsl();
        microService.joinSsl();
    } else {
        microService.start();
        microService.join();
    }
}

From source file:io.fabric8.apiman.ApimanStarter.java

/**
 * Main entry point for the API Manager micro service.
 * @param args the arguments/*from   w  w  w  .j av a 2  s . c o m*/
 * @throws Exception when any unhandled exception occurs
 */
public static final void main(String[] args) throws Exception {

    Fabric8ManagerApiMicroService microService = new Fabric8ManagerApiMicroService();

    boolean isTestMode = getSystemPropertyOrEnvVar(APIMAN_TESTMODE, false);
    if (isTestMode)
        log.info("Apiman running in TestMode");

    boolean isSsl = getSystemPropertyOrEnvVar(APIMAN_SSL, false);
    log.info("Apiman running in SSL: " + isSsl);
    String protocol = "http";
    if (isSsl)
        protocol = "https";

    File apimanConfigFile = new File(APIMAN_PROPERTIES);
    String esUsername = null;
    String esPassword = null;
    if (apimanConfigFile.exists()) {
        PropertiesConfiguration config = new PropertiesConfiguration(apimanConfigFile);
        esUsername = config.getString("es.username");
        esPassword = config.getString("es.password");
        if (Utils.isNotNullOrEmpty(esPassword))
            esPassword = new String(Base64.getDecoder().decode(esPassword), StandardCharsets.UTF_8).trim();
        setConfigProp(APIMAN_ELASTICSEARCH_USERNAME, esUsername);
        setConfigProp(APIMAN_ELASTICSEARCH_PASSWORD, esPassword);
    }
    URL elasticEndpoint = null;
    // Require ElasticSearch and the Gateway Services to to be up before proceeding
    if (isTestMode) {
        URL url = new URL("https://localhost:9200");
        elasticEndpoint = waitForDependency(url, "", "elasticsearch-v1", "status", "200", esUsername,
                esPassword);
    } else {
        String defaultEsUrl = protocol + "://elasticsearch-v1:9200";
        String esURL = getSystemPropertyOrEnvVar(APIMAN_ELASTICSEARCH_URL, defaultEsUrl);
        URL url = new URL(esURL);
        elasticEndpoint = waitForDependency(url, "", "elasticsearch-v1", "status", "200", esUsername,
                esPassword);
        log.info("Found " + elasticEndpoint);
        String defaultGatewayUrl = protocol + "://apiman-gateway:7777";
        gatewayUrl = getSystemPropertyOrEnvVar(APIMAN_GATEWAY_URL, defaultGatewayUrl);

        URL gatewayEndpoint = waitForDependency(new URL(gatewayUrl), "/api/system/status", "apiman-gateway",
                "up", "true", null, null);
        log.info("Found " + gatewayEndpoint);
    }

    setConfigProp("apiman.plugins.repositories", "http://repo1.maven.org/maven2/");
    setConfigProp("apiman-manager.plugins.registries",
            "http://cdn.rawgit.com/apiman/apiman-plugin-registry/1.2.6.Final/registry.json");
    setFabric8Props(elasticEndpoint);
    if (isSsl) {
        microService.startSsl();
        microService.joinSsl();
    } else {
        microService.start();
        microService.join();
    }
}

From source file:net.es.nsi.topology.translator.http.Decoder.java

public static Document decode(String source) throws IOException {
    byte[] encoded = Base64.getDecoder().decode(source);
    byte[] xml = decompress(encoded);

    try {//from   ww w  .j a  va  2  s  .  c o m
        Document doc = DomParser.xml2Dom(new ByteArrayInputStream(xml));
        return doc;
    } catch (ParserConfigurationException | SAXException ex) {
        log.error("decode: failed to parse document", ex);
        throw new IOException(ex.getMessage(), ex.getCause());
    }
}

From source file:com.github.wasiqb.coteafs.appium.utils.ScreenRecorder.java

/**
 * @author wasiqb/*  ww w .  j a v a 2  s . c o m*/
 * @since Oct 13, 2018
 * @param content
 * @param setting
 */
public static void saveRecording(final String content, final RecordSetting setting) {
    final byte[] decode = Base64.getDecoder().decode(content);
    try {
        final String path = setting.getPath();
        final String prefix = setting.getPrefix();
        final SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd-HHmmss");
        final String timeStamp = date.format(Calendar.getInstance().getTime());
        final String fileName = format("%s/%s-%s.%s", path, prefix, timeStamp, "mp4");
        LOG.info(format("Saving video recording to [%s] path...", fileName));
        writeByteArrayToFile(new File(fileName), decode);
    } catch (final IOException e) {
        LOG.error("Error occurred while saving video recording...");
        LOG.catching(e);
    }
}

From source file:me.camerongray.teamlocker.server.RequestCredentials.java

public RequestCredentials(Request request) {
    String[] credentials = new String(Base64.getDecoder()
            .decode(StringUtils.substringAfter(request.headers("Authorization"), "Basic").trim())).split(":");
    this.username = credentials[0];
    this.password = (credentials.length > 1) ? credentials[1] : "";
}

From source file:it.infn.mw.iam.util.ssh.RSAPublicKeyUtils.java

private static String buildMD5Fingerprint(String key) throws InvalidSshKeyException {

    String fingerprint = null;// w  w  w.  j a  v a2 s .c om

    try {

        byte[] decodedKey = Base64.getDecoder().decode(key);
        byte[] digest = MessageDigest.getInstance(MessageDigestAlgorithms.MD5).digest(decodedKey);
        fingerprint = Hex.encodeHexString(digest);

    } catch (Exception e) {

        throw new InvalidSshKeyException("Error during fingerprint generation: RSA key is not base64 encoded",
                e);
    }

    return fingerprint;
}

From source file:org.finra.herd.core.HerdStringUtils.java

/**
 * Decodes and return the base64 encoded string.
 *
 * @param base64EncodedText the base64 encoded string
 *
 * @return the decoded string/*www.j  a va  2  s  .  com*/
 */
public static String decodeBase64(String base64EncodedText) {
    return StringUtils.toEncodedString(
            Base64.getDecoder().decode(base64EncodedText.getBytes(StandardCharsets.UTF_8)),
            StandardCharsets.UTF_8);
}

From source file:rmblworx.tools.timey.gui.AnalyzeTestHelper.java

/**
 * Base64-dekodiert {@code content} und schreibt den Inhalt in die Datei.
 * uerst ntzlich, um den ber die Travis-Konsole ausgegebenen Screenshot-Inhalt wiederherzustellen.
 * @param path Pfad zur Datei//from  ww  w.  j a  v a 2 s  .  c  om
 * @param content Base64-kodierter Dateiinhalt
 * @throws IOException
 */
public static void writeBase64EncodedScreenshotContentToFile(final String path, final String content)
        throws IOException {
    FileUtils.writeByteArrayToFile(new File(path), Base64.getDecoder().decode(content));
}