use of java.nio.charset.Charset in project jna by java-native-access.
the class ReturnTypesTest method testReturnStringArray.
public void testReturnStringArray() {
Charset charset = Charset.forName(Native.getDefaultStringEncoding());
final String VALUE = getName() + charset.decode(charset.encode(UNICODE));
String[] input = { VALUE, null };
String[] result = lib.returnPointerArgument(input);
assertEquals("Wrong array length", input.length - 1, result.length);
assertEquals("Wrong array element value", VALUE, result[0]);
assertNull("NULL should result in null return value", lib.returnPointerArgument((String[]) null));
}
use of java.nio.charset.Charset in project jersey by jersey.
the class JettisonListElementProvider method getXMLStreamReader.
@Override
protected final XMLStreamReader getXMLStreamReader(Class<?> elementType, MediaType mediaType, Unmarshaller u, InputStream entityStream) throws XMLStreamException {
JettisonConfig c = JettisonConfig.DEFAULT;
final Charset charset = getCharset(mediaType);
if (u instanceof JettisonConfigured) {
c = ((JettisonConfigured) u).getJSONConfiguration();
}
return Stax2JettisonFactory.createReader(new InputStreamReader(entityStream, charset), c);
}
use of java.nio.charset.Charset in project jersey by jersey.
the class AbstractCollectionJaxbProvider method writeTo.
@Override
public final void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
try {
final Collection c = (type.isArray()) ? Arrays.asList((Object[]) t) : (Collection) t;
final Class elementType = getElementClass(type, genericType);
final Charset charset = getCharset(mediaType);
final String charsetName = charset.name();
final Marshaller m = getMarshaller(elementType, mediaType);
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
if (charset != UTF8) {
m.setProperty(Marshaller.JAXB_ENCODING, charsetName);
}
setHeader(m, annotations);
writeCollection(elementType, c, mediaType, charset, m, entityStream);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of java.nio.charset.Charset in project jna by java-native-access.
the class CallbacksTest method testCallStringCallback.
public void testCallStringCallback() {
final boolean[] called = { false };
final String[] cbargs = { null, null };
TestLibrary.StringCallback cb = new TestLibrary.StringCallback() {
@Override
public String callback(String arg, String arg2) {
called[0] = true;
cbargs[0] = arg;
cbargs[1] = arg2;
return arg + arg2;
}
};
Charset charset = Charset.forName(Native.getDefaultStringEncoding());
final String VALUE = "value" + charset.decode(charset.encode(UNICODE));
final String VALUE2 = getName() + charset.decode(charset.encode(UNICODE));
String value = lib.callStringCallback(cb, VALUE, VALUE2);
assertTrue("Callback not called", called[0]);
assertEquals("Wrong String callback argument 0", VALUE, cbargs[0]);
assertEquals("Wrong String callback argument 1", VALUE2, cbargs[1]);
assertEquals("Wrong String return", VALUE + VALUE2, value);
}
use of java.nio.charset.Charset in project jna by java-native-access.
the class CallbacksTest method testCallStringArrayCallback.
public void testCallStringArrayCallback() {
final boolean[] called = { false };
final String[][] cbargs = { null };
TestLibrary.StringArrayCallback cb = new TestLibrary.StringArrayCallback() {
@Override
public String[] callback(String[] arg) {
called[0] = true;
cbargs[0] = arg;
return arg;
}
};
Charset charset = Charset.forName(Native.getDefaultStringEncoding());
final String VALUE = "value" + charset.decode(charset.encode(UNICODE));
final String[] VALUE_ARRAY = { VALUE, null };
Pointer value = lib.callStringArrayCallback(cb, VALUE_ARRAY);
assertTrue("Callback not called", called[0]);
assertEquals("String[] array should not be modified", VALUE, VALUE_ARRAY[0]);
assertEquals("Terminating null should be removed from incoming arg", VALUE_ARRAY.length - 1, cbargs[0].length);
assertEquals("String[] argument index 0 mismatch", VALUE_ARRAY[0], cbargs[0][0]);
String[] result = value.getStringArray(0);
assertEquals("Wrong String[] return", VALUE_ARRAY[0], result[0]);
assertEquals("Terminating null should be removed from return value", VALUE_ARRAY.length - 1, result.length);
}
Aggregations