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

/**
 * Read the text from a file/*  www. ja  va  2 s .com*/
 *
 * @param file the file to read text from
 * @return the loaded text
 */
public static String loadTextFromFile(File file) {

    if (file.exists()) {

        InputStreamReader isr = null;
        FileInputStream fis = null;
        try {

            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis, StandardCharsets.UTF_8);

            StringBuilder stringBuilder = new StringBuilder();

            int i;
            while ((i = isr.read()) != -1) {
                stringBuilder.append((char) i);
            }
            return stringBuilder.toString();

        } catch (IOException ignored) {
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }
    }

    return "";
}

From source file:Main.java

public static String readFromInternalStorage(Context context, String fileName) {

    File file = new File(context.getFilesDir(), fileName);

    try {//from  w w w  .ja  va2s  .  co m
        FileInputStream fis = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        String line;

        StringBuilder stringBuilder = new StringBuilder();
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        String json = stringBuilder.toString();

        br.close();
        in.close();
        fis.close();

        return json;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }

    return null;
}

From source file:com.blackducksoftware.integration.hub.detect.detector.clang.CompileCommandsJsonFile.java

public static List<CompileCommand> parseJsonCompilationDatabaseFile(final Gson gson,
        final File compileCommandsJsonFile) throws IOException {
    final String compileCommandsJson = FileUtils.readFileToString(compileCommandsJsonFile,
            StandardCharsets.UTF_8);
    final CompileCommandJsonData[] compileCommands = gson.fromJson(compileCommandsJson,
            CompileCommandJsonData[].class);
    return Arrays.stream(compileCommands).map(rawCommand -> new CompileCommand(rawCommand))
            .collect(Collectors.toList());
}

From source file:de.saly.json.jsr353.benchmark.data.CreateJsonTestFiles.java

public static void create(String path) throws Exception {

    if (path == null || path.length() == 0) {
        path = "./generated/";
    }//from   w w w.  j  a v a2  s .c  o m

    final File dir = new File(path).getAbsoluteFile();
    dir.mkdirs();

    System.out.println("Generating benchmark data " + dir.getAbsolutePath());

    create(path, 1, StandardCharsets.UTF_8);
    create(path, 1, StandardCharsets.UTF_16);

    create(path, 10, StandardCharsets.UTF_8);
    create(path, 10, StandardCharsets.UTF_16);

    create(path, 100, StandardCharsets.UTF_8);
    create(path, 100, StandardCharsets.UTF_16);

    create(path, 1000, StandardCharsets.UTF_8);
    create(path, 1000, StandardCharsets.UTF_16);

    create(path, 10000, StandardCharsets.UTF_8);
    create(path, 10000, StandardCharsets.UTF_16);

    create(path, 100000, StandardCharsets.UTF_8);
    create(path, 100000, StandardCharsets.UTF_16);

    create(path, 10000000, StandardCharsets.UTF_8); // 10gb
    createBigStack(path, 10000000 * 10, StandardCharsets.UTF_8);

    System.out.println("Finished.");
    System.out.println();
}

From source file:aiai.apps.commons.utils.SecUtils.java

public static String getSignature(String data, PrivateKey privateKey, boolean isChuncked)
        throws GeneralSecurityException {
    Signature signer = Signature.getInstance("SHA256withRSA");
    signer.initSign(privateKey);//from  w w w  .j  a  va 2 s . co m
    signer.update(data.getBytes(StandardCharsets.UTF_8));
    return StringUtils.newStringUsAscii(Base64.encodeBase64(signer.sign(), isChuncked));
}

From source file:costumetrade.common.http.HttpClientUtilsWrapper.java

public static HttpResponse doPost(HttpMessage message) throws UnsupportedOperationException, IOException {

    logger.info("?{}....", message.getUrl());

    try (CloseableHttpClient httpclient = HttpClients.custom().build();) {
        HttpPost request = new HttpPost(message.getUrl());
        request.setEntity(message.getHttpEntity());
        try (CloseableHttpResponse response = httpclient.execute(request);) {
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("?{}???{}", message.getUrl(), statusCode);

            if (HttpStatus.SC_OK == statusCode) {
                String result = IOUtils.toString(response.getEntity().getContent(),
                        StandardCharsets.UTF_8.name());
                logger.info("?:{}", result);
                return new HttpResponse(statusCode, result, message.getCallbackData());
            }//from  ww w .  j  av a  2  s  .c  om
            return new HttpResponse(statusCode, null, message.getCallbackData());
        }
    }
}

From source file:support.AuthManager.java

public static String md5Custom(String st) {
    MessageDigest messageDigest = null;
    byte[] digest = new byte[0];

    try {//from  w w w.  j  av a2  s .  c  o m
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(st.getBytes(StandardCharsets.UTF_8));
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    BigInteger bigInt = new BigInteger(1, digest);
    String md5Hex = bigInt.toString(16);

    while (md5Hex.length() < 32) {
        md5Hex = "0" + md5Hex;
    }

    return md5Hex;
}

From source file:com.raphfrk.craftproxyclient.json.JSONManager.java

public static byte[] JSONToBytes(JSONObject obj) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamWriter wos = new OutputStreamWriter(bos, StandardCharsets.UTF_8);
    BufferedWriter br = new BufferedWriter(wos);
    try {//from www.  ja  va  2 s  . co  m
        obj.writeJSONString(br);
        br.close();
    } catch (IOException e) {
        return null;
    }
    return bos.toByteArray();
}

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

public static String decodeUrl(String url) {
    try {//from  w  ww.  j  a va  2s .co m
        return URLDecoder.decode(url, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException uee) {
        //TODO: LOGGER
        System.err.println("Failed decoding URL using UTF-8 charset" + uee.getMessage());
        //noinspection deprecation
        return url;
    }
}

From source file:io.undertow.testutils.HttpClientUtils.java

public static String readResponse(InputStream stream) throws IOException {

    byte[] data = new byte[100];
    int read;//w w w . j  a v a 2 s.  c o  m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((read = stream.read(data)) != -1) {
        out.write(data, 0, read);
    }
    return new String(out.toByteArray(), StandardCharsets.UTF_8);
}