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());
}
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;
}
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
}
}
}
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);
}
}
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);
}
}
Aggregations