Example usage for java.nio.charset Charset name

List of usage examples for java.nio.charset Charset name

Introduction

In this page you can find the example usage for java.nio.charset Charset name.

Prototype

String name

To view the source code for java.nio.charset Charset name.

Click Source Link

Usage

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static String stream2Bytes(InputStream is, Charset charset) {
    String result = null;/*from  ww w. j  a va 2 s  .c o  m*/
    try {
        int total = is.available();
        byte[] bs = new byte[total];
        is.read(bs);
        result = new String(bs, charset.name());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static InputStream bytes2Stream(String text, Charset charset) throws UnsupportedEncodingException {
    ByteArrayInputStream inputStream = null;
    if (StringUtils.isBlank(text)) {
        return null;
    }//from   w ww.  j av a  2s.com
    byte[] bs = text.getBytes(charset.name());
    inputStream = new ByteArrayInputStream(bs);
    return inputStream;
}

From source file:Main.java

/**
 * Creates a {@link Marshaller} based on the given {@link JAXBContext}
 * and configures it to use the given {@link Charset}, and allows to
 * output the XML code to be generated formatted and as an XML fragment.
 * /*w ww  .jav a 2s  .  co m*/
 * @param ctx the {@link JAXBContext} to create a {@link Marshaller} for
 * @param charset the {@link Charset} the XML code should be formatted
 * @param formatted {@code true} if the XML code should be formatted,
 *       {@code false} otherwise
 * @param fragment {@code false} if the XML code should start with
 *       {@code <?xml }, or {@code true} if just fragment XML code should
 *       get generated
 * 
 * @return a preconfigured {@link Marshaller}
 * 
 * @throws IOException in case no {@link Marshaller} could get created
 */
public static Marshaller createMarshaller(JAXBContext ctx, Charset charset, boolean formatted, boolean fragment)
        throws IOException {
    if (charset == null) {
        return createMarshaller(ctx, Charset.defaultCharset(), formatted, fragment);
    }
    Objects.requireNonNull(ctx);
    try {
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
        return marshaller;
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}

From source file:io.github.jeddict.jcode.util.FileUtil.java

public static void expandTemplate(Reader reader, Writer writer, Map<String, Object> values,
        Charset targetEncoding) throws IOException {
    ScriptEngine eng = getScriptEngine();
    Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    bind.putAll(values);// w w  w. j av  a 2  s.  co  m
    bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name());

    try {
        eng.getContext().setWriter(writer);
        eng.eval(reader);
    } catch (ScriptException ex) {
        throw new IOException(ex);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:sit.web.client.HTTPTrustHelper.java

/**
 * from/*w  w w .  j  a v  a 2s  . c o  m*/
 * http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https
 *
 * @param charset
 * @param port
 * @return
 */
public static HttpClient getNewHttpClient(Charset charset, int port) {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, charset.name());

        SchemeRegistry registry = new SchemeRegistry();
        //registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, port));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:org.netbeans.jcode.core.util.FileUtil.java

private static void expandTemplate(InputStream template, Map<String, Object> values, Charset targetEncoding,
        Writer w) throws IOException {
    //        Charset sourceEnc = FileEncodingQuery.getEncoding(template);
    ScriptEngine eng = getScriptEngine();
    Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    bind.putAll(values);/*from www.  j a  va2 s  .  co m*/
    bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name());

    Reader is = null;
    try {
        eng.getContext().setWriter(w);
        is = new InputStreamReader(template);
        eng.eval(is);
    } catch (ScriptException ex) {
        throw new IOException(ex);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:io.coala.capability.online.FluentHCOnlineCapability.java

public static HttpEntity toFormEntity(final List<NameValuePair> paramList) {
    final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, Consts.ISO_8859_1);
    final Charset charset = contentType != null ? contentType.getCharset() : null;
    final String s = URLEncodedUtils.format(paramList, charset != null ? charset.name() : null);
    byte[] raw;//w  ww  .  ja  v a 2s  .  com
    try {
        raw = charset != null ? s.getBytes(charset.name()) : s.getBytes();
    } catch (UnsupportedEncodingException ex) {
        raw = s.getBytes();
    }
    return new ApacheInternalByteArrayEntity(raw, contentType);
}

From source file:Main.java

/**
 * Creates a {@link TransformerHandler}.
 * //from www . j a v a  2  s .c o m
 * @param commentHeader the comment header
 * @param rootTag the root tag
 * @param streamResult stream result
 */
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
        StreamResult streamResult, Charset charset)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException {
    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
    try {
        tf.setAttribute("indent-number", new Integer(2));
    } catch (Exception e) {
        // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
    }
    TransformerHandler transformerHandler = tf.newTransformerHandler();
    Transformer serializer = transformerHandler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, charset.name());
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformerHandler.setResult(streamResult);
    transformerHandler.startDocument();
    String newline = System.getProperty("line.separator");
    if (newline == null) {
        newline = "\n";
    }
    commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    if (rootTag.length() > 0) {
        transformerHandler.startElement("", "", rootTag, null);
    }
    return transformerHandler;
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TestTCPServerSource.java

protected static TCPServerSourceConfig createConfigBean(Charset charset) {
    TCPServerSourceConfig config = new TCPServerSourceConfig();
    config.batchSize = 10;//from www . ja va  2  s. c o m
    config.tlsConfigBean.tlsEnabled = false;
    config.numThreads = 1;
    config.syslogCharset = charset.name();
    config.tcpMode = TCPMode.SYSLOG;
    config.syslogFramingMode = SyslogFramingMode.NON_TRANSPARENT_FRAMING;
    config.nonTransparentFramingSeparatorCharStr = "\n";
    config.maxMessageSize = 4096;
    config.ports = randomSinglePort();
    config.maxWaitTime = 1000;
    return config;
}

From source file:net.officefloor.plugin.socket.server.http.HttpTestUtil.java

/**
 * Creates a {@link net.officefloor.plugin.socket.server.http.HttpRequest}
 * for testing.//from  www .j  a v a 2  s  . c o m
 * 
 * @param method
 *            HTTP method (GET, POST).
 * @param requestUri
 *            Request URI.
 * @param entity
 *            Contents of the
 *            {@link net.officefloor.plugin.socket.server.http.HttpRequest}
 *            entity.
 * @param headerNameValues
 *            {@link HttpHeader} name values.
 * @return {@link net.officefloor.plugin.socket.server.http.HttpRequest}.
 * @throws Exception
 *             If fails to create the
 *             {@link net.officefloor.plugin.socket.server.http.HttpRequest}
 *             .
 */
public static net.officefloor.plugin.socket.server.http.HttpRequest createHttpRequest(String method,
        String requestUri, String entity, String... headerNameValues) throws Exception {

    // Obtain the entity data
    final Charset charset = AbstractServerSocketManagedObjectSource.getCharset(null);
    byte[] entityData = (entity == null ? new byte[0] : entity.getBytes(charset));

    // Create the headers
    List<HttpHeader> headers = new LinkedList<HttpHeader>();
    if (entity != null) {
        // Include content type and content length if entity
        headers.add(new HttpHeaderImpl("content-type", "text/plain; charset=" + charset.name()));
        headers.add(new HttpHeaderImpl("content-length", String.valueOf(entityData.length)));
    }
    for (int i = 0; i < headerNameValues.length; i += 2) {
        String name = headerNameValues[i];
        String value = headerNameValues[i + 1];
        headers.add(new HttpHeaderImpl(name, value));
    }

    // Create the entity input stream
    ServerInputStreamImpl inputStream = new ServerInputStreamImpl(new Object());
    inputStream.inputData(entityData, 0, (entityData.length - 1), false);
    HttpEntity httpEntity = new HttpEntityImpl(inputStream);

    // Return the HTTP request
    return new HttpRequestImpl(method, requestUri, "HTTP/1.1", headers, httpEntity);
}