Example usage for org.apache.commons.codec CharEncoding UTF_8

List of usage examples for org.apache.commons.codec CharEncoding UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.codec CharEncoding UTF_8.

Prototype

String UTF_8

To view the source code for org.apache.commons.codec CharEncoding UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:com.autonomy.aci.client.transport.gss.GssEncryptionCodecTest.java

@Test(expected = EncryptionCodecException.class)
public void testDecodeInternalDecoderException() throws UnsupportedEncodingException, EncryptionCodecException {
    final byte[] encoded = new URLCodec().encode("This is a $tring that n33ds % ", CharEncoding.UTF_8)
            .getBytes(CharEncoding.UTF_8);

    // Partially copy the encoded byte array, so it should fail on decoding...
    final byte[] bad = new byte[encoded.length - 2];
    System.arraycopy(encoded, 0, bad, 0, (encoded.length - 2));

    new GssEncryptionCodec(spy(GSSContext.class)).decodeInternal(bad);
    fail("Should've thrown a DecoderException...");
}

From source file:com.spectralogic.ds3client.utils.MetadataStringManipulation.java

/**
 * Decodes a percent encoded string//from   w  w  w  . j a  v  a2s.c o m
 */
public static String toDecodedString(final String str) {
    if (str == null) {
        return null;
    }
    try {
        return URLDecoder.decode(str, CharEncoding.UTF_8);
    } catch (final UnsupportedEncodingException e) {
        //Should not happen
        throw new RuntimeException("Could not decode string: " + str, e);
    }
}

From source file:com.thoughtworks.go.util.FileDigester.java

public static String md5DigestOfFolderContent(File directory) throws IOException {
    File[] files = directory.listFiles();
    Arrays.sort(files, NameFileComparator.NAME_COMPARATOR);
    StringBuilder md5 = new StringBuilder();
    for (File file : files) {
        if (file.isDirectory())
            md5.append(md5DigestOfFolderContent(file));
        else/*from  w  w w  .  j  a  v  a  2  s.  co  m*/
            md5.append(file.getName() + md5DigestOfFile(file));
    }
    return md5DigestOfStream(new ByteArrayInputStream(md5.toString().getBytes(CharEncoding.UTF_8)));
}

From source file:es.jamisoft.comun.codec.B64Codec.java

/**
 * Default constructor.
 */
public B64Codec() {
    this(CharEncoding.UTF_8);
}

From source file:io.marto.aem.utils.freemarker.FreemarkerTemplateFactory.java

/**
 * @param templatePath  the template to render (view)
 * @param model         the model//from ww w.  ja va 2 s.  co m
 * @param writer        the writer
 * @throws TemplateException  on template error
 * @throws IOException        on any other write error
 */
public void render(String templatePath, Object model, Writer writer) throws TemplateException, IOException {
    final Template template;
    try {
        template = config.getTemplate(templatePath, CharEncoding.UTF_8);
    } catch (IOException e) {
        throw new TemplateException(format("Failed to render template '%s'", templatePath), e, null);
    }
    template.process(model, writer);
}

From source file:com.arcbees.vcs.AbstractVcsApi.java

protected <T> T readEntity(Class<T> clazz, HttpEntity entity, Gson gson) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    entity.writeTo(outputStream);//from w  w w  .  ja v a  2s  . co  m
    String json = outputStream.toString(CharEncoding.UTF_8);

    return gson.fromJson(json, clazz);
}

From source file:com.googlesource.gerrit.plugins.rabbitmq.config.AMQProperties.java

public AMQP.BasicProperties getBasicProperties() {
    return new AMQP.BasicProperties.Builder().appId(EVENT_APPID).contentEncoding(CharEncoding.UTF_8)
            .contentType(CONTENT_TYPE_JSON).deliveryMode(message.deliveryMode).priority(message.priority)
            .headers(headers).timestamp(new Date(TimeUtil.nowMs())).build();
}

From source file:io.moquette.parser.netty.performance.PublishBomber.java

public void publishLoop(int messagesPerSecond, int numToSend) throws UnsupportedEncodingException {
    long pauseMicroseconds = (int) ((1.0 / messagesPerSecond) * 1000 * 1000);
    LOG.warn("PUB: Pause over the each message sent {} microsecs", pauseMicroseconds);

    LOG.info("PUB: publishing..");
    final long startTime = System.currentTimeMillis();

    //initialize the timer
    PlatformTimer timer = PlatformTimer.detect();
    for (int i = 0; i < numToSend; i++) {
        long nanos = System.nanoTime();
        byte[] rawContent = ("Hello world!!-" + nanos).getBytes(CharEncoding.UTF_8);
        ByteBuf payload = Unpooled.copiedBuffer(rawContent);

        MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_MOST_ONCE,
                false, 0);// www. j av a 2 s  . co m
        MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader("/topic", 0);
        MqttPublishMessage pubMessage = new MqttPublishMessage(fixedHeader, varHeader, payload);

        sendMessage(pubMessage);
        timer.sleep(pauseMicroseconds);
    }
    LOG.info("PUB: published in {} ms", System.currentTimeMillis() - startTime);
}

From source file:com.fujitsu.dc.test.utils.Http.java

/**
 * ????.//w w w .  j av  a 2s .c  om
 * @return TResponse
 */
public TResponse returns() {
    BufferedReader br = null;
    try {
        // ???
        InputStreamReader isr = new InputStreamReader(is, CharEncoding.UTF_8);
        br = new BufferedReader(isr);
        String firstLine = br.readLine();
        firstLine = this.processParams(firstLine);
        String[] l1 = firstLine.split(" ");
        this.method = l1[0];
        this.path = l1[1];
        String protoVersion = l1[2];

        // Open
        this.url = new URL(baseUrl + this.path);
        try {
            this.socket = createSocket(this.url);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        }
        this.sIn = this.socket.getInputStream();
        this.sOut = this.socket.getOutputStream();
        this.sReader = new BufferedReader(new InputStreamReader(this.sIn, CharEncoding.UTF_8));
        this.sWriter = new BufferedOutputStream(this.sOut);

        // ?
        StringBuilder sb = new StringBuilder();
        sb.append(this.method);
        sb.append(" ");
        sb.append(this.url.getPath());
        if (this.url.getQuery() != null) {
            sb.append("?");
            sb.append(this.url.getQuery());
        }
        sb.append(" ");
        sb.append(protoVersion);
        this.sWriter.write(sb.toString().getBytes(CharEncoding.UTF_8));
        this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        log.debug("Req Start -------");
        log.debug(sb.toString());

        // Header
        String line = null;
        String lastLine = null;
        int contentLengthLineNum = -1;
        List<String> lines = new ArrayList<String>();
        int i = 0;
        while ((line = br.readLine()) != null) {
            if (line.length() == 0) {
                break;
            }
            line = this.processParams(line);
            if (line.toLowerCase().startsWith(HttpHeaders.CONTENT_LENGTH.toLowerCase())) {
                // Content-Length???????????????????
                contentLengthLineNum = i;
            } else if (line.toLowerCase().startsWith(HttpHeaders.HOST.toLowerCase())) {
                line = line.replaceAll("\\?", this.url.getAuthority());
            }
            lines.add(line + CRLF);
            lastLine = line;
            i++;
        }

        // Version?
        lines.add("X-Dc-Version: " + DcCoreTestConfig.getCoreVersion() + CRLF);
        String body = null;
        // ????Break??????Body?????
        if (line != null) {
            log.debug("Req Body-------");
            i = 1;
            StringWriter sw = new StringWriter();
            int chr;
            while ((chr = br.read()) != -1) {
                sw.write((char) chr);
            }
            body = sw.toString();
            body = this.processParams(body);
            // Content-Length?
            if (contentLengthLineNum != -1) {
                String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?",
                        String.valueOf(body.getBytes().length));
                lines.set(contentLengthLineNum, contentLength);
            }
        } else {
            if (this.paraBody != null) {
                log.debug("Req Body-------");
                // ?Body??
                String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?",
                        String.valueOf(this.paraBody.length));
                lines.set(contentLengthLineNum, contentLength);
            } else {
                // ??????.
                if (lastLine.length() > 0) {
                    log.debug("one more CRLF");
                    this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
                }
            }
        }
        // Header??
        for (String l : lines) {
            this.sWriter.write(l.getBytes(CharEncoding.UTF_8));
            if (log.isDebugEnabled()) {
                l.replaceAll(CRLF, "");
                log.debug(l);
            }
        }
        // 
        this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        // Body??
        if (body != null) {
            this.sWriter.write(body.getBytes(CharEncoding.UTF_8));
            log.debug(body);
        }
        // ?Body??
        if (this.paraBody != null) {
            this.sWriter.write(this.paraBody);
            this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        }
        this.sWriter.flush();
        // ??
        TResponse ret = new TResponse(this.sReader);
        return ret;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (br != null) {
                br.close();
            }
            if (this.sWriter != null) {
                this.sWriter.close();
            }
            if (this.sReader != null) {
                this.sReader.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:io.personium.test.utils.Http.java

/**
 * ????./*from   www.  java  2 s .  com*/
 * @return TResponse
 */
public TResponse returns() {
    BufferedReader br = null;
    try {
        // ???
        InputStreamReader isr = new InputStreamReader(is, CharEncoding.UTF_8);
        br = new BufferedReader(isr);
        String firstLine = br.readLine();
        firstLine = this.processParams(firstLine);
        String[] l1 = firstLine.split(" ");
        this.method = l1[0];
        this.path = l1[1];
        String protoVersion = l1[2];

        // Open
        this.url = new URL(baseUrl + this.path);
        try {
            this.socket = createSocket(this.url);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        }
        this.sIn = this.socket.getInputStream();
        this.sOut = this.socket.getOutputStream();
        this.sReader = new BufferedReader(new InputStreamReader(this.sIn, CharEncoding.UTF_8));
        this.sWriter = new BufferedOutputStream(this.sOut);

        // ?
        StringBuilder sb = new StringBuilder();
        sb.append(this.method);
        sb.append(" ");
        sb.append(this.url.getPath());
        if (this.url.getQuery() != null) {
            sb.append("?");
            sb.append(this.url.getQuery());
        }
        sb.append(" ");
        sb.append(protoVersion);
        this.sWriter.write(sb.toString().getBytes(CharEncoding.UTF_8));
        this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        log.debug("Req Start -------");
        log.debug(sb.toString());

        // Header
        String line = null;
        String lastLine = null;
        int contentLengthLineNum = -1;
        List<String> lines = new ArrayList<String>();
        int i = 0;
        while ((line = br.readLine()) != null) {
            if (line.length() == 0) {
                break;
            }
            line = this.processParams(line);
            if (line.toLowerCase().startsWith(HttpHeaders.CONTENT_LENGTH.toLowerCase())) {
                // Content-Length???????????????????
                contentLengthLineNum = i;
            } else if (line.toLowerCase().startsWith(HttpHeaders.HOST.toLowerCase())) {
                line = line.replaceAll("\\?", this.url.getAuthority());
            }
            lines.add(line + CRLF);
            lastLine = line;
            i++;
        }

        // Version?
        lines.add("X-Personium-Version: " + PersoniumCoreTestConfig.getCoreVersion() + CRLF);
        String body = null;
        // ????Break??????Body?????
        if (line != null) {
            log.debug("Req Body-------");
            i = 1;
            StringWriter sw = new StringWriter();
            int chr;
            while ((chr = br.read()) != -1) {
                sw.write((char) chr);
            }
            body = sw.toString();
            body = this.processParams(body);
            // Content-Length?
            if (contentLengthLineNum != -1) {
                String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?",
                        String.valueOf(body.getBytes().length));
                lines.set(contentLengthLineNum, contentLength);
            }
        } else {
            if (this.paraBody != null) {
                log.debug("Req Body-------");
                // ?Body??
                String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?",
                        String.valueOf(this.paraBody.length));
                lines.set(contentLengthLineNum, contentLength);
            } else {
                // ??????.
                if (lastLine.length() > 0) {
                    log.debug("one more CRLF");
                    this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
                }
            }
        }
        // Header??
        for (String l : lines) {
            this.sWriter.write(l.getBytes(CharEncoding.UTF_8));
            if (log.isDebugEnabled()) {
                l.replaceAll(CRLF, "");
                log.debug(l);
            }
        }
        // 
        this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        // Body??
        if (body != null) {
            this.sWriter.write(body.getBytes(CharEncoding.UTF_8));
            log.debug(body);
        }
        // ?Body??
        if (this.paraBody != null) {
            this.sWriter.write(this.paraBody);
            this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8));
        }
        this.sWriter.flush();
        // ??
        TResponse ret = new TResponse(this.sReader);
        return ret;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (br != null) {
                br.close();
            }
            if (this.sWriter != null) {
                this.sWriter.close();
            }
            if (this.sReader != null) {
                this.sReader.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}