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

public static String encode(String plain) {
    return encode(plain, StandardCharsets.UTF_8);
}

From source file:com.lantos.base64password.CreateAndValidatePassword.java

public static String base64Decode(String string) {
    byte[] tmp = new org.apache.commons.codec.binary.Base64().decode(string);
    String newString = new String(tmp, StandardCharsets.UTF_8);
    return newString;
}

From source file:Main.java

/**
 * TODO Finish JavaDoc/* w w  w. j av  a  2  s  . c  o m*/
 *
 * @param byteArray
 * @return
 */
public static String decompress(byte[] byteArray) {

    StringBuilder stringBuilder = new StringBuilder();
    String line;
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
            new GZIPInputStream(new ByteArrayInputStream(byteArray)), StandardCharsets.UTF_8))) {
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return stringBuilder.toString();
}

From source file:Main.java

public static Object convertToPojoUsingString(String xml, Class... type) {
    Object result;/*w w  w .  j  ava 2  s. c o  m*/
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        result = unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

public static synchronized StringBuilder readTempFile(String filename) {
    File tempFile;/*from   w w  w .j  a  v a2  s .  c  o  m*/
    FileInputStream is = null;

    tempFile = new File(filename);

    String strLine = "";
    StringBuilder text = new StringBuilder();

    /** Reading contents of the temporary file, if already exists */
    try {
        is = new FileInputStream(tempFile);
        //            FileReader fReader = new FileReader(tempFile);
        BufferedReader bReader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));

        /** Reading the contents of the file , line by line */
        while ((strLine = bReader.readLine()) != null) {
            text.append(strLine + "\n");
        }

        bReader.close(); //close bufferedreader
        is.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return text;

}

From source file:Main.java

public static void printNodesAndAttributes(String xmlStr) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from  ww  w.  j  a  va  2  s. c om*/
        logger.info("Xml processing:");
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            logger.info("Node type: " + node.getNodeType() + " Node name: " + node.getNodeName());
            logger.info("    Attributes: " + attributesStr(node));
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
}

From source file:Main.java

public static Node getNode(String xmlStr, String nodeName, Map<String, String> attributesMap) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Node returnNode = null;//w  ww .  ja  va  2s .c o m
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNode = node;
                    break;
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNode;
}

From source file:com.ibm.watson.app.qaclassifier.tools.FileUtils.java

public static String loadFromFilesystemOrClasspath(String path) throws IOException {
    return loadFromFilesystemOrClasspath(path, StandardCharsets.UTF_8);
}

From source file:Main.java

public static ArrayList<Node> getNodeList(String xmlStr, String nodeName, Map<String, String> attributesMap) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    ArrayList<Node> returnNodeList = new ArrayList<Node>();
    try {//from ww  w  .  j a va 2  s  .com
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNodeList.add(node);
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNodeList;
}

From source file:io.bitgrillr.gocddockerexecplugin.SystemHelper.java

static String getSystemUid() throws IOException, InterruptedException {
    Process id = Runtime.getRuntime().exec(new String[] { "bash", "-c", "echo \"$(id -u):$(id -g)\"" });
    id.waitFor();/*  w  w w .  j a v  a2s. c o  m*/
    return StringUtils.chomp(IOUtils.toString(id.getInputStream(), StandardCharsets.UTF_8));
}