Search in sources :

Example 11 with Charset

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));
}
Also used : Charset(java.nio.charset.Charset)

Example 12 with Charset

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) JettisonConfigured(org.glassfish.jersey.jettison.JettisonConfigured) JettisonConfig(org.glassfish.jersey.jettison.JettisonConfig) Charset(java.nio.charset.Charset)

Example 13 with Charset

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) Collection(java.util.Collection) Charset(java.nio.charset.Charset) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Example 14 with Charset

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);
}
Also used : Charset(java.nio.charset.Charset)

Example 15 with Charset

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);
}
Also used : 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