StringEntityProvider implements EntityProvider<String> { @Override public void writeTo(String string, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { Writer writer = new OutputStreamWriter(entityStream, getCharsetOrUtf8(mediaType)); writer.write(string); writer.flush(); }  @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override String readFrom(Class<String> type,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders,
                           InputStream entityStream); @Override long getSize(String string, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(String string,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesStringToOutputStream() throws Exception { MediaType mediaType = new MediaType("text", "plain", ImmutableMap.of("charset", "windows-1251")); ByteArrayOutputStream out = new ByteArrayOutputStream(); stringEntityProvider.writeTo(TEST_CONTENT, String.class, null, null, mediaType, new MultivaluedHashMap<>(), out); assertArrayEquals(TEST_CONTENT.getBytes("windows-1251"), out.toByteArray()); }
@Test public void writesUtf8StringToOutputStream() throws Exception { MediaType mediaType = new MediaType("text", "plain"); ByteArrayOutputStream out = new ByteArrayOutputStream(); stringEntityProvider.writeTo(TEST_CONTENT, String.class, null, null, mediaType, new MultivaluedHashMap<>(), out); assertArrayEquals(TEST_CONTENT.getBytes("UTF-8"), out.toByteArray()); }