Search in sources :

Example 16 with Charset

use of java.nio.charset.Charset in project jna by java-native-access.

the class CallbacksTest method testStringCallbackMemoryReclamation.

public void testStringCallbackMemoryReclamation() throws InterruptedException {
    TestLibrary.StringCallback cb = new TestLibrary.StringCallback() {

        @Override
        public String callback(String arg, String arg2) {
            return arg + arg2;
        }
    };
    // A little internal groping
    Map<?, ?> m = CallbackReference.allocations;
    m.clear();
    Charset charset = Charset.forName(Native.getDefaultStringEncoding());
    String arg = getName() + "1" + charset.decode(charset.encode(UNICODE));
    String arg2 = getName() + "2" + charset.decode(charset.encode(UNICODE));
    String value = lib.callStringCallback(cb, arg, arg2);
    WeakReference<Object> ref = new WeakReference<Object>(value);
    arg = null;
    value = null;
    System.gc();
    for (int i = 0; i < 100 && (ref.get() != null || m.size() > 0); ++i) {
        try {
            // Give the GC a chance to run
            Thread.sleep(10);
            System.gc();
        } finally {
        }
    }
    assertNull("NativeString reference not GC'd", ref.get());
    assertEquals("NativeString reference still held: " + m.values(), 0, m.size());
}
Also used : WeakReference(java.lang.ref.WeakReference) Charset(java.nio.charset.Charset)

Example 17 with Charset

use of java.nio.charset.Charset in project hadoop by apache.

the class WebAppUtils method getURLEncodedQueryString.

private static String getURLEncodedQueryString(HttpServletRequest request, String parameterToRemove) {
    String queryString = request.getQueryString();
    if (queryString != null && !queryString.isEmpty()) {
        String reqEncoding = request.getCharacterEncoding();
        if (reqEncoding == null || reqEncoding.isEmpty()) {
            reqEncoding = "ISO-8859-1";
        }
        Charset encoding = Charset.forName(reqEncoding);
        List<NameValuePair> params = URLEncodedUtils.parse(queryString, encoding);
        if (parameterToRemove != null && !parameterToRemove.isEmpty()) {
            Iterator<NameValuePair> paramIterator = params.iterator();
            while (paramIterator.hasNext()) {
                NameValuePair current = paramIterator.next();
                if (current.getName().equals(parameterToRemove)) {
                    paramIterator.remove();
                }
            }
        }
        return URLEncodedUtils.format(params, encoding);
    }
    return null;
}
Also used : NameValuePair(org.apache.http.NameValuePair) Charset(java.nio.charset.Charset)

Example 18 with Charset

use of java.nio.charset.Charset in project groovy by apache.

the class JavaStubGenerator method generateClass.

public void generateClass(ClassNode classNode) throws FileNotFoundException {
    // Only attempt to render our self if our super-class is resolved, else wait for it
    if (requireSuperResolved && !classNode.getSuperClass().isResolved()) {
        return;
    }
    // owner should take care for us
    if (classNode instanceof InnerClassNode)
        return;
    // don't generate stubs for private classes, as they are only visible in the same file
    if ((classNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0)
        return;
    String fileName = classNode.getName().replace('.', '/');
    mkdirs(outputPath, fileName);
    toCompile.add(fileName);
    File file = new File(outputPath, fileName + ".java");
    FileOutputStream fos = new FileOutputStream(file);
    Charset charset = Charset.forName(encoding);
    PrintWriter out = new PrintWriter(new OutputStreamWriter(fos, charset));
    try {
        String packageName = classNode.getPackageName();
        if (packageName != null) {
            out.println("package " + packageName + ";\n");
        }
        printImports(out, classNode);
        printClassContents(out, classNode);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        // ignore
        }
        try {
            fos.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 19 with Charset

use of java.nio.charset.Charset in project groovy by apache.

the class NioGroovyMethods method append.

/**
     * Append the text at the end of the Path, using a specified encoding.  If
     * the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
     * <code>writeBom</code> is <code>true</code>, and the file doesn't already
     * exist, the requisite byte order mark is written to the file before the
     * text is appended.
     *
     * @param self     a Path
     * @param text     the text to append at the end of the Path
     * @param charset  the charset used
     * @param writeBom whether to write the BOM
     * @throws java.io.IOException if an IOException occurs.
     * @since 2.5.0
     */
public static void append(Path self, Object text, String charset, boolean writeBom) throws IOException {
    Writer writer = null;
    try {
        Charset resolvedCharset = Charset.forName(charset);
        boolean shouldWriteBom = writeBom && !self.toFile().exists();
        OutputStream out = Files.newOutputStream(self, CREATE, APPEND);
        if (shouldWriteBom) {
            IOGroovyMethods.writeUTF16BomIfRequired(out, resolvedCharset);
        }
        writer = new OutputStreamWriter(out, resolvedCharset);
        InvokerHelper.write(writer, text);
        writer.flush();
        Writer temp = writer;
        writer = null;
        temp.close();
    } finally {
        closeWithWarning(writer);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 20 with Charset

use of java.nio.charset.Charset in project camel by apache.

the class MinaComponent method configureDefaultCodecFactory.

protected void configureDefaultCodecFactory(String type, IoServiceConfig config, MinaConfiguration configuration) {
    if (configuration.isTextline()) {
        Charset charset = getEncodingParameter(type, configuration);
        LineDelimiter delimiter = getLineDelimiterParameter(configuration.getTextlineDelimiter());
        TextLineCodecFactory codecFactory = new TextLineCodecFactory(charset, delimiter);
        if (configuration.getEncoderMaxLineLength() > 0) {
            codecFactory.setEncoderMaxLineLength(configuration.getEncoderMaxLineLength());
        }
        if (configuration.getDecoderMaxLineLength() > 0) {
            codecFactory.setDecoderMaxLineLength(configuration.getDecoderMaxLineLength());
        }
        addCodecFactory(config, codecFactory);
        if (LOG.isDebugEnabled()) {
            LOG.debug("{}: Using TextLineCodecFactory: {} using encoding: {} line delimiter: {}({})", new Object[] { type, codecFactory, charset, configuration.getTextlineDelimiter(), delimiter });
            LOG.debug("Encoder maximum line length: {}. Decoder maximum line length: {}", codecFactory.getEncoderMaxLineLength(), codecFactory.getDecoderMaxLineLength());
        }
    } else {
        ObjectSerializationCodecFactory codecFactory = new ObjectSerializationCodecFactory();
        addCodecFactory(config, codecFactory);
        LOG.debug("{}: Using ObjectSerializationCodecFactory: {}", type, codecFactory);
    }
}
Also used : ObjectSerializationCodecFactory(org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory) LineDelimiter(org.apache.mina.filter.codec.textline.LineDelimiter) Charset(java.nio.charset.Charset)

Aggregations

Charset (java.nio.charset.Charset)1400 IOException (java.io.IOException)259 Test (org.junit.Test)186 InputStream (java.io.InputStream)114 ByteBuffer (java.nio.ByteBuffer)110 File (java.io.File)104 ArrayList (java.util.ArrayList)102 InputStreamReader (java.io.InputStreamReader)99 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)64 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)55 ByteArrayInputStream (java.io.ByteArrayInputStream)54 List (java.util.List)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 FileInputStream (java.io.FileInputStream)48 OutputStream (java.io.OutputStream)47