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.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Encodes the given string and then GZIP compresses it.
 *
 * @param value String input// w  w  w  . j a va2s.  co m
 * @return compressed byte array output
 * @throws IOException
 */
public static byte[] compressStringNonBase64(String value) throws IOException {
    return compressBytesNonBase64(value.getBytes(StandardCharsets.UTF_8));
}

From source file:io.undertow.server.protocol.http.ContentOverrunTestCase.java

@BeforeClass
public static void setup() {
    HttpHandler overlyLong = new HttpHandler() {
        @Override/*from   www .  ja  v a 2  s .c om*/
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.setResponseContentLength(10);
            exchange.getOutputStream().write("Overly long content".getBytes(StandardCharsets.UTF_8));
        }
    };
    HttpHandler responseNotAllowed = new HttpHandler() {
        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.setStatusCode(204);
            exchange.getOutputStream().write("Overly long content".getBytes(StandardCharsets.UTF_8));
        }
    };

    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/204", new BlockingHandler(responseNotAllowed))
            .addPrefixPath("/long", new BlockingHandler(overlyLong)));
}

From source file:fr.eclipseonfire.mjapi.implementations.http.StandardMinecraftProperties.java

@Override
public MinecraftTextures getTexturesProperty() throws IOException {
    Property property = this.getByName("textures");

    if (property == null) {
        return null;
    }//from  w  ww .j a v a  2  s  .c o  m

    String json = new String(Base64.decodeBase64(property.getValue()), StandardCharsets.UTF_8);

    return HttpAccountRepository.GSON.fromJson(json, HttpMinecraftTextures.class);
}

From source file:its.tools.SonarlintDaemon.java

private static String artifactVersion() {
    if (artifactVersion == null) {
        try {//from w ww .  jav  a2s  .c o  m
            for (String l : Files.readAllLines(Paths.get("pom.xml"), StandardCharsets.UTF_8)) {
                String lineTrimmed = l.trim();
                if (lineTrimmed.startsWith("<version>")) {
                    artifactVersion = lineTrimmed.substring("<version>".length(),
                            lineTrimmed.length() - "</version>".length());
                    break;
                }
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
    return artifactVersion;
}

From source file:com.basistech.rosette.api.AbstractTest.java

protected static byte[] gzip(String text) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (GZIPOutputStream out = new GZIPOutputStream(baos)) {
        out.write(text.getBytes(StandardCharsets.UTF_8));
    }/*from  www . ja v a  2  s  .  c o  m*/
    return baos.toByteArray();
}

From source file:io.druid.query.aggregation.datasketches.quantiles.DoublesSketchOperations.java

public static DoublesSketch deserializeFromBase64EncodedString(final String str) {
    return deserializeFromByteArray(Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)));
}

From source file:spring.travel.site.auth.Verifier.java

public boolean verify(String data, String signature) throws AuthException {
    String calculatedSignature = signer.sign(data);
    return compare(calculatedSignature.getBytes(StandardCharsets.UTF_8),
            signature.getBytes(StandardCharsets.UTF_8));
}

From source file:ai.susi.tools.JsonSignature.java

/**
 * Create and add a signature to a JSONObject
 * @param obj the JSONObject/*w  w  w .ja v a 2 s .  co  m*/
 * @param key the private key to use
 * @throws InvalidKeyException if the key is not valid (for example not RSA)
 * @throws SignatureException if something with the JSONObject is bogus
 */
public static void addSignature(JSONObject obj, PrivateKey key) throws InvalidKeyException, SignatureException {

    removeSignature(obj);

    Signature signature;
    try {
        signature = Signature.getInstance("SHA256withRSA");
    } catch (NoSuchAlgorithmException e) {
        return; //does not happen
    }

    signature.initSign(key);
    signature.update(obj.toString().getBytes(StandardCharsets.UTF_8));

    byte[] sigBytes = signature.sign();

    obj.put(signatureString, new String(Base64.getEncoder().encode(sigBytes)));
}

From source file:com.shareplaylearn.resources.AccessToken.java

/**
 * TODO: we're abandoning the idea of internal OAUTH flow (it 'twas a teensy shady), and
 * TODO: this will just be use for users that create their own accounts.
 * TODO: which means redis, JWT signing & validation utilities, user stores, bcrypt, etc.
 * @param req/* ww  w  .j  a va2s  . c  o m*/
 * @param res
 * @return
 */
public static String handlePostAccessToken(Request req, Response res) {
    String credentialHeader = req.headers("Authorization");
    if (credentialHeader == null) {
        res.body("No credentials provided.");
        res.status(400);
        return "Bad Request";
    }
    //TODO: fuzz this to see what happens with bad strings
    byte[] decodedCredentials = Base64.decodeBase64(credentialHeader.trim());
    if (decodedCredentials == null) {
        res.body("Invalid credentials provided.");
        res.status(400);
        return "Bad Request";
    }
    String credentialsString = new String(decodedCredentials, StandardCharsets.UTF_8);
    String[] credentials = credentialsString.split(":");
    if (credentials.length != 3) {
        res.body("Invalid credentials provided.");
        res.status(400);
        return "Bad Request";
    }
    String realm = credentials[0];
    String username = credentials[1];
    String password = credentials[2];

    res.status(500);
    res.body("Not Implemented.");
    return "Internal Server Error";
}

From source file:Main.java

/**
 * This method writes a file based on xml string
 * @param path/*from   w  w w.  j  a v a2  s.c o  m*/
 * @param content (xml)
 * @return true if successfully written
 * @throws IOException
 */
public static Boolean writeFile(String path, String content) throws IOException {
    //read file
    OutputStream out = null;
    InputStream filecontent = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
    try {
        //initiate output stream
        out = new FileOutputStream(new File(path));
        int read = 0;
        final byte[] bytes = new byte[1024];
        while ((read = filecontent.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
    } catch (Exception e) {
        return false;
    } finally {
        if (out != null)
            out.close();
        if (filecontent != null)
            filecontent.close();
    }
    return true;
}