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:com.yahoo.maven.visitor.SuccessfulCompilationAsserter.java

public void assertNoCompilationErrors() throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null,
            StandardCharsets.UTF_8)) {
        List<File> javaFiles = new ArrayList<>();
        try (Stream<Path> stream = Files.walk(scratchSpace)) {
            Iterator<Path> iterator = stream.iterator();
            while (iterator.hasNext()) {
                Path path = iterator.next();
                if (Files.isRegularFile(path) && "java".equals(FilenameUtils.getExtension(path.toString()))) {
                    javaFiles.add(path.toFile());
                }//from w ww.j av a  2 s .c o  m
            }
        }
        Iterable<? extends JavaFileObject> compilationUnits = fileManager
                .getJavaFileObjectsFromFiles(javaFiles);
        compiler.getTask(null, fileManager, diagnostics,
                Arrays.asList("-classpath", System.getProperty("java.class.path")), null, compilationUnits)
                .call();
    }
    int errors = 0;
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
        if (Diagnostic.Kind.ERROR.equals(diagnostic.getKind())) {
            System.err.println(diagnostic);
            errors++;
        }
    }
    assertEquals(0, errors);
}

From source file:com.digitalpebble.stormcrawler.aws.bolt.CloudSearchUtils.java

/** Returns a normalised doc ID based on the URL of a document **/
public static String getID(String url) {

    // the document needs an ID
    // see/* w  ww.j  a v a 2s.c  om*/
    // http://docs.aws.amazon.com/cloudsearch/latest/developerguide/preparing-data.html#creating-document-batches
    // A unique ID for the document. A document ID can contain any
    // letter or number and the following characters: _ - = # ; : / ? @
    // &. Document IDs must be at least 1 and no more than 128
    // characters long.
    byte[] dig = digester.digest(url.getBytes(StandardCharsets.UTF_8));
    String ID = Hex.encodeHexString(dig);
    // is that even possible?
    if (ID.length() > 128) {
        throw new RuntimeException("ID larger than max 128 chars");
    }
    return ID;
}

From source file:com.sdl.odata.renderer.util.PrettyPrinter.java

/**
 * Pretty-print a given XML./*from w  w w .  java 2 s .c  om*/
 *
 * @param xml The not-formatted XML.
 * @return The pretty-printed XML.
 */
public static String prettyPrintXml(String xml) throws TransformerException, IOException {

    Source xmlInput = new StreamSource(new StringReader(xml));
    try (StringWriter stringWriter = new StringWriter()) {
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", DEFAULT_INDENT);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    }
}

From source file:com.linecorp.bot.client.LineSignatureValidatorTest.java

@Test
public void validateSignature() throws Exception {
    LineSignatureValidator lineSignatureValidator = new LineSignatureValidator(
            channelSecret.getBytes(StandardCharsets.UTF_8));

    String httpRequestBody = "{}";
    assertThat(lineSignatureValidator.validateSignature(httpRequestBody.getBytes(StandardCharsets.UTF_8),
            "3q8QXTAGaey18yL8FWTqdVlbMr6hcuNvM4tefa0o9nA=")).isTrue();
    assertThat(lineSignatureValidator.validateSignature(httpRequestBody.getBytes(StandardCharsets.UTF_8),
            "596359635963")).isFalse();
}

From source file:com.byteatebit.common.io.TestAbsoluteClasspathStreamSource.java

@Test
public void testGetInputStream() throws IOException {
    IStreamSource streamSource = new AbsoluteClasspathStreamSource("io/classpath_resource.txt");
    try (InputStream in = streamSource.getInputStream()) {
        String message = IOUtils.toString(in, StandardCharsets.UTF_8);
        Assert.assertEquals("Sample classpath resource", message);
    }//  w  w  w  . j  a v a  2  s  .c  om
}

From source file:com.aqnote.shared.cryptology.cert.io.PKCSTransformer.java

public static String getCrtFileString(X509Certificate cert) throws Exception {
    CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
    PEMWriter pemWriter = new PEMWriter(new PrintWriter(cbb.getOutputStream()));
    cbb.getOutputStream().flush();//from ww w.j  a  v a  2  s . co  m
    cbb.getOutputStream().close();
    pemWriter.writeObject(cert);
    pemWriter.flush();
    pemWriter.close();
    String crtFile = StreamUtil.stream2Bytes(cbb.getInputStream(), StandardCharsets.UTF_8);
    cbb.getInputStream().close();
    cbb.clear();
    return crtFile;
}

From source file:ninja.siden.internal.Testing.java

static String read(HttpResponse response) throws Exception {
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        return "";
    }/* w  ww. j a  va2  s  . c  o  m*/
    try (Scanner scanner = new Scanner(entity.getContent(), StandardCharsets.UTF_8.name())) {
        return scanner.useDelimiter("\\A").next();
    }
}

From source file:org.elasticsearch.upgrades.WatcherRestartIT.java

private void ensureWatcherStopped() throws Exception {
    assertBusy(() -> {/*  w w  w.  j a v  a2s .c  om*/
        Response stats = client().performRequest(new Request("GET", "_xpack/watcher/stats"));
        String responseBody = EntityUtils.toString(stats.getEntity(), StandardCharsets.UTF_8);
        assertThat(responseBody, containsString("\"watcher_state\":\"stopped\""));
        assertThat(responseBody, not(containsString("\"watcher_state\":\"starting\"")));
        assertThat(responseBody, not(containsString("\"watcher_state\":\"started\"")));
        assertThat(responseBody, not(containsString("\"watcher_state\":\"stopping\"")));
    });
}

From source file:com.thoughtworks.go.config.IdFileService.java

public void store(String data) {
    try {/* ww  w. jav  a  2s  .c  om*/
        file.delete();
        FileUtils.writeStringToFile(file, data, StandardCharsets.UTF_8);
    } catch (IOException ioe) {
        throw bomb(String.format("Couldn't save %s to filesystem", file.getName()), ioe);
    }
}

From source file:org.openlmis.fulfillment.service.request.RequestHelper.java

/**
 * Creates a {@link URI} from the given string representation and with the given parameters.
 */// w ww  .  j a  v  a  2  s  .c  o  m
public static URI createUri(String url, RequestParameters parameters) {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().uri(URI.create(url));

    RequestParameters.init().setAll(parameters).forEach(e -> e.getValue().forEach(one -> {
        try {
            builder.queryParam(e.getKey(),
                    UriUtils.encodeQueryParam(String.valueOf(one), StandardCharsets.UTF_8.name()));
        } catch (UnsupportedEncodingException ex) {
            throw new EncodingException(ex);
        }
    }));

    return builder.build(true).toUri();
}