Example usage for org.apache.commons.io Charsets UTF_8

List of usage examples for org.apache.commons.io Charsets UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets UTF_8.

Prototype

Charset UTF_8

To view the source code for org.apache.commons.io Charsets UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:com.opendoorlogistics.core.utils.strings.Strings.java

public static String readUTF8Resource(String name) {
    // Use own class loader to prevent problems when jar loaded by reflection
    InputStream is = Strings.class.getResourceAsStream(name);
    StringWriter writer = new StringWriter();
    try {//w ww  .  java  2s. c o m
        IOUtils.copy(is, writer, Charsets.UTF_8);
        is.close();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }

    return writer.toString();
}

From source file:com.buaa.cfs.security.ShellBasedIdMapping.java

static StaticMapping parseStaticMap(File staticMapFile) throws IOException {

    Map<Integer, Integer> uidMapping = new HashMap<Integer, Integer>();
    Map<Integer, Integer> gidMapping = new HashMap<Integer, Integer>();

    BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream(staticMapFile), Charsets.UTF_8));

    try {/*from   ww  w.j a  v a2s.c o m*/
        String line = null;
        while ((line = in.readLine()) != null) {
            // Skip entirely empty and comment lines.
            LOG.info("--- the idmaping read line is : " + line);
            if (EMPTY_LINE.matcher(line).matches() || COMMENT_LINE.matcher(line).matches()) {
                continue;
            }

            Matcher lineMatcher = MAPPING_LINE.matcher(line);
            if (!lineMatcher.matches()) {
                LOG.warn("Could not parse line '" + line + "'. Lines should be of "
                        + "the form '[uid|gid] [remote id] [local id]'. Blank lines and "
                        + "everything following a '#' on a line will be ignored.");
                continue;
            }

            // We know the line is fine to parse without error checking like this
            // since it matched the regex above.
            String firstComponent = lineMatcher.group(1);
            LOG.info("--- the idmaping read line group(1) is : " + firstComponent);
            int remoteId = parseId(lineMatcher.group(2));
            int localId = parseId(lineMatcher.group(3));
            if (firstComponent.equals("uid")) {
                uidMapping.put(localId, remoteId);
            } else {
                gidMapping.put(localId, remoteId);
            }
        }
    } finally {
        in.close();
    }

    return new StaticMapping(uidMapping, gidMapping);
}

From source file:net.minecraftforge.fml.common.FMLCommonHandler.java

/**
 * Loads a lang file, first searching for a marker to enable the 'extended' format {escape charaters}
 * If the marker is not found it simply returns and let the vanilla code load things.
 * The Marker is 'PARSE_ESCAPES' by itself on a line starting with '#' as such:
 * #PARSE_ESCAPES// w  w w.ja v a2 s  .  co  m
 *
 * @param table The Map to load each key/value pair into.
 * @param inputstream Input stream containing the lang file.
 * @return A new InputStream that vanilla uses to load normal Lang files, Null if this is a 'enhanced' file and loading is done.
 */
public InputStream loadLanguage(Map<String, String> table, InputStream inputstream) throws IOException {
    byte[] data = IOUtils.toByteArray(inputstream);

    boolean isEnhanced = false;
    for (String line : IOUtils.readLines(new ByteArrayInputStream(data), Charsets.UTF_8)) {
        if (!line.isEmpty() && line.charAt(0) == '#') {
            line = line.substring(1).trim();
            if (line.equals("PARSE_ESCAPES")) {
                isEnhanced = true;
                break;
            }
        }
    }

    if (!isEnhanced)
        return new ByteArrayInputStream(data);

    Properties props = new Properties();
    props.load(new InputStreamReader(new ByteArrayInputStream(data), Charsets.UTF_8));
    for (Entry e : props.entrySet()) {
        table.put((String) e.getKey(), (String) e.getValue());
    }
    props.clear();
    return null;
}

From source file:io.fabric8.kubernetes.pipeline.devops.ApplyStepExecution.java

private String readFile(String fileName) throws AbortException {
    try {//from   w  ww  . j  a  v a 2  s.  c o  m
        InputStream is = workspace.child(fileName).read();
        try {
            return IOUtils.toString(is, Charsets.UTF_8);
        } finally {
            is.close();
        }
    } catch (Exception e) {
        throw new AbortException("Unable to read file " + fileName + ". " + e);
    }
}

From source file:net.technicpack.rest.RestObject.java

public static <T extends RestObject> T postRestObject(Class<T> restObject, String url)
        throws RestfulAPIException {
    InputStream stream = null;/*from  www. j  a va2s .c  om*/
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(15000);

        stream = conn.getInputStream();
        String data = IOUtils.toString(stream, Charsets.UTF_8);
        T result = gson.fromJson(data, restObject);

        if (result == null) {
            throw new RestfulAPIException("Unable to access URL [" + url + "]");
        }

        if (result.hasError()) {
            throw new RestfulAPIException("Error in response: " + result.getError());
        }

        return result;
    } catch (SocketTimeoutException e) {
        throw new RestfulAPIException("Timed out accessing URL [" + url + "]", e);
    } catch (MalformedURLException e) {
        throw new RestfulAPIException("Invalid URL [" + url + "]", e);
    } catch (JsonParseException e) {
        throw new RestfulAPIException("Error parsing response JSON at URL [" + url + "]", e);
    } catch (IOException e) {
        throw new RestfulAPIException("Error accessing URL [" + url + "]", e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:net.technicpack.rest.RestObject.java

public static <T> List<T> getRestArray(Class<T> restObject, String url) throws RestfulAPIException {
    InputStream stream = null;/*from  ww  w .j av a2s .  co m*/
    try {
        URLConnection conn = new URL(url).openConnection();
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(15000);

        stream = conn.getInputStream();
        String data = IOUtils.toString(stream, Charsets.UTF_8);

        JsonElement response = gson.fromJson(data, JsonElement.class);

        if (response == null || !response.isJsonArray()) {
            if (response.isJsonObject() && response.getAsJsonObject().has("error"))
                throw new RestfulAPIException("Error in response: " + response.getAsJsonObject().get("error"));
            else
                throw new RestfulAPIException("Unable to access URL [" + url + "]");
        }

        JsonArray array = response.getAsJsonArray();
        List<T> result = new ArrayList<T>(array.size());

        for (JsonElement element : array) {
            if (element.isJsonObject())
                result.add(gson.fromJson(element.getAsJsonObject(), restObject));
            else
                result.add(gson.fromJson(element.getAsString(), restObject));
        }

        return result;
    } catch (SocketTimeoutException e) {
        throw new RestfulAPIException("Timed out accessing URL [" + url + "]", e);
    } catch (MalformedURLException e) {
        throw new RestfulAPIException("Invalid URL [" + url + "]", e);
    } catch (JsonParseException e) {
        throw new RestfulAPIException("Error parsing response JSON at URL [" + url + "]", e);
    } catch (IOException e) {
        throw new RestfulAPIException("Error accessing URL [" + url + "]", e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:net.technicpack.rest.RestObject.java

public static <T extends RestObject> T getRestObject(Class<T> restObject, String url)
        throws RestfulAPIException {
    InputStream stream = null;/*w  w w.j  a  v a  2 s  .c om*/
    try {
        URLConnection conn = new URL(url).openConnection();
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(15000);

        stream = conn.getInputStream();
        String data = IOUtils.toString(stream, Charsets.UTF_8);
        T result = gson.fromJson(data, restObject);

        if (result == null) {
            throw new RestfulAPIException("Unable to access URL [" + url + "]");
        }

        if (result.hasError()) {
            throw new RestfulAPIException("Error in response: " + result.getError());
        }

        return result;
    } catch (SocketTimeoutException e) {
        throw new RestfulAPIException("Timed out accessing URL [" + url + "]", e);
    } catch (MalformedURLException e) {
        throw new RestfulAPIException("Invalid URL [" + url + "]", e);
    } catch (JsonParseException e) {
        throw new RestfulAPIException("Error parsing response JSON at URL [" + url + "]", e);
    } catch (IOException e) {
        throw new RestfulAPIException("Error accessing URL [" + url + "]", e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:net.xby1993.common.util.io.HashUtil.java

/**
 * sha1, ?UTF8./*w w  w.  ja  va2 s .  c o m*/
 */
public static byte[] sha1(@NotNull String input) {
    return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), null, 1);
}

From source file:net.xby1993.common.util.io.HashUtil.java

/**
 * sha1salt.//from   w  w  w.  j  ava 2 s.  c om
 */
public static byte[] sha1(@NotNull String input, @Nullable byte[] salt) {
    return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), salt, 1);
}

From source file:net.xby1993.common.util.io.HashUtil.java

/**
 * sha1salt.//w w  w  .ja va 2  s  . c om
 * 
 * @see #generateSalt(int)
 */
public static byte[] sha1(@NotNull String input, @Nullable byte[] salt, int iterations) {
    return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), salt, iterations);
}