Example usage for org.apache.commons.net.util Charsets toCharset

List of usage examples for org.apache.commons.net.util Charsets toCharset

Introduction

In this page you can find the example usage for org.apache.commons.net.util Charsets toCharset.

Prototype

public static Charset toCharset(String charsetName) 

Source Link

Document

Returns a charset object for the given charset name.

Usage

From source file:com.chiorichan.factory.FileInterpreter.java

public FileInterpreter() {
    encoding = Charsets.toCharset(AppConfig.get().getString("server.defaultBinaryEncoding", "ISO-8859-1"));

    // All param keys are lower case. No such thing as a non-lowercase param keys because keys are forced to lowercase.
    annotations.put("title", null);
    annotations.put("reqperm", "-1");
    annotations.put("theme", null);
    annotations.put("view", null);

    annotations.put("html", null);
    annotations.put("file", null);

    // Shell Options (groovy,text,html)
    annotations.put("shell", null);
    annotations.put("encoding", encoding.name());
}

From source file:com.chiorichan.factory.FileInterpreter.java

public String getContentType() {
    if (cachedFile == null)
        return "text/html";

    String type = get("contenttype");

    if (type == null || type.isEmpty())
        type = ContentTypes.getContentType(cachedFile.getAbsoluteFile());

    if (type.startsWith("text"))
        setEncoding(Charsets.toCharset(AppConfig.get().getString("server.defaultTextEncoding", "UTF-8")));
    else/* w w w .  j  a  v  a2  s. c o m*/
        setEncoding(
                Charsets.toCharset(AppConfig.get().getString("server.defaultBinaryEncoding", "ISO-8859-1")));

    return type;
}

From source file:com.chiorichan.factory.FileInterpreter.java

public final void interpretParamsFromFile(File file) throws IOException {
    if (file == null)
        throw new FileNotFoundException("File path was null");

    FileInputStream is = null;//from  w  ww  .ja va  2s.  co  m
    try {
        cachedFile = file;

        annotations.put("file", file.getAbsolutePath());

        if (file.isDirectory())
            annotations.put("shell", "embedded");
        else {
            if (!annotations.containsKey("shell") || annotations.get("shell") == null) {
                String shell = determineShellFromName(file.getName());
                if (shell != null && !shell.isEmpty())
                    annotations.put("shell", shell);
            }

            is = new FileInputStream(file);

            ByteBuf buf = Unpooled.wrappedBuffer(IOUtils.toByteArray(is));
            boolean beginContent = false;
            int lastInx;
            int lineCnt = 0;

            data = Unpooled.buffer();

            do {
                lastInx = buf.readerIndex();
                String l = readLine(buf);
                if (l == null)
                    break;

                if (l.trim().startsWith("@"))
                    try {
                        lineCnt++;

                        /* Only solution I could think of for CSS files since they use @annotations too, so we share them. */
                        if (ContentTypes.getContentType(file).equalsIgnoreCase("text/css"))
                            data.writeBytes((l + "\n").getBytes());
                        /* Only solution I could think of for CSS files since they use @annotations too, so we share them. */

                        String key;
                        String val = "";

                        if (l.contains(" ")) {
                            key = l.trim().substring(1, l.trim().indexOf(" "));
                            val = l.trim().substring(l.trim().indexOf(" ") + 1);
                        } else
                            key = l;

                        if (val.endsWith(";"))
                            val = val.substring(0, val.length() - 1);

                        if (val.startsWith("'") && val.endsWith("'"))
                            val = val.substring(1, val.length() - 1);

                        annotations.put(key.toLowerCase(), val);
                        Log.get().finer("Setting param '" + key + "' to '" + val + "'");

                        if (key.equals("encoding"))
                            if (Charset.isSupported(val))
                                setEncoding(Charsets.toCharset(val));
                            else
                                Log.get()
                                        .severe("The file '" + file.getAbsolutePath() + "' requested encoding '"
                                                + val + "' but it's not supported by the JVM!");
                    } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {

                    }
                else if (l.trim().isEmpty())
                    lineCnt++;
                // Continue reading, this line is empty.
                else {
                    // We encountered the beginning of the file content.
                    beginContent = true;
                    buf.readerIndex(lastInx); // This rewinds the buffer to the last reader index
                }
            } while (!beginContent);

            data.writeBytes(Strings.repeat('\n', lineCnt).getBytes());

            data.writeBytes(buf);
        }
    } finally {
        if (is != null)
            is.close();
    }
}