Example usage for java.nio.charset StandardCharsets UTF_8

List of usage examples for java.nio.charset StandardCharsets UTF_8

Introduction

In this page you can find the example usage for java.nio.charset StandardCharsets UTF_8.

Prototype

Charset UTF_8

To view the source code for java.nio.charset StandardCharsets UTF_8.

Click Source Link

Document

Eight-bit UCS Transformation Format.

Usage

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes/*from   w ww . j a  va  2 s  .c o m*/
 * @return the array as a string with {@code UTF-8} encoding
 */
public static String inflate(final byte[] bytes) {
    final Inflater inflater = new Inflater(true);
    final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH];

    final byte[] extendedBytes = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
    extendedBytes[bytes.length] = 0;

    inflater.setInput(extendedBytes);

    try {
        final int resultLength = inflater.inflate(xmlMessageBytes);
        inflater.end();

        if (!inflater.finished()) {
            throw new RuntimeException("buffer not large enough.");
        }

        inflater.end();
        return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8);
    } catch (final DataFormatException e) {
        return null;
    }
}

From source file:io.kamax.mxisd.util.RestClientUtils.java

public static HttpPost post(String url, String body) {
    StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8);
    entity.setContentType(ContentType.APPLICATION_JSON.toString());
    HttpPost req = new HttpPost(url);
    req.setEntity(entity);//from ww  w.  ja va 2  s. c  om
    return req;
}

From source file:Main.java

public static String format(Document document) throws TransformerException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Transformer transformer = TRANSFORMER.get();
    transformer.transform(new DOMSource(document), new StreamResult(out));
    transformer.reset();//from  w ww.  j a  v  a 2 s  .  c o  m
    return new String(out.toByteArray(), StandardCharsets.UTF_8);
}

From source file:org.elasticsearch.xpack.watcher.common.http.auth.basic.ApplicableBasicAuth.java

public static String headerValue(String username, char[] password) {
    return "Basic " + Base64.getEncoder()
            .encodeToString((username + ":" + new String(password)).getBytes(StandardCharsets.UTF_8));
}

From source file:cd.go.notification.gitter.utils.Util.java

public static String readResource(String resourceFile) {
    try (InputStream reader = GetViewRequestExecutor.class.getResourceAsStream(resourceFile)) {
        return IOUtils.toString(reader, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException("Could not find resource " + resourceFile, e);
    }//from  www  .  j  a v  a  2s .c  o  m
}

From source file:de.micromata.genome.util.runtime.GroovyUtils.java

/**
 * Convert to string./*from w  w w .  j a va 2s  . com*/
 *
 * @param is the is
 * @return the string
 */
public static String convertToString(InputStream is) {
    try {
        return IOUtils.toString(is, StandardCharsets.UTF_8.name());
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.jivesoftware.sdk.util.JiveSDKUtils.java

public static String encodeUrl(String url) {
    try {//from   w  w w  .java2  s.  com
        return URLEncoder.encode(url, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException uee) {
        log.warn("Failed encoding URL using UTF-8 charset" + uee.getMessage());
        //noinspection deprecation
        return url;
    } // end try/catch
}

From source file:Main.java

public static void writeToInternalStorage(Context context, String fileName, String json) {

    FileOutputStream outputStream = null;
    try {/*from   w  w  w .j a v a  2 s  .  c o  m*/
        outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        for (byte s : json.getBytes(StandardCharsets.UTF_8)) {
            outputStream.write(s);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
    }
}

From source file:com.diffplug.gradle.ConfigMisc.java

/** Creates an XML string from a groovy.util.Node. */
public static Supplier<byte[]> xml(Supplier<Node> node) {
    return () -> {
        Node root = node.get();/*  w  w  w .j a  v a  2s .co  m*/
        return XmlUtil.serialize(root).getBytes(StandardCharsets.UTF_8);
    };
}

From source file:cd.go.authentication.ldap.utils.Util.java

public static String readResource(String resourceFile) {
    try (InputStreamReader reader = new InputStreamReader(Util.class.getResourceAsStream(resourceFile),
            StandardCharsets.UTF_8)) {
        return IOUtils.toString(reader);
    } catch (IOException e) {
        throw new RuntimeException("Could not find resource " + resourceFile, e);
    }/* ww w.  j  a  va  2  s.  c  om*/
}