public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException { if (InputStream.class.isAssignableFrom(o.getClass())) { copyInputToOutput((InputStream)o, os, annotations, headers); } else if (File.class.isAssignableFrom(o.getClass())) { copyInputToOutput(new BufferedInputStream( Files.newInputStream(((File)o).toPath())), os, annotations, headers); } else if (byte[].class.isAssignableFrom(o.getClass())) { copyInputToOutput(new ByteArrayInputStream((byte[])o), os, annotations, headers); } else if (Reader.class.isAssignableFrom(o.getClass())) { try { Writer writer = new OutputStreamWriter(os, getEncoding(type)); IOUtils.copy((Reader)o, writer, bufferSize); writer.flush(); } finally { ((Reader)o).close(); } } else if (StreamingOutput.class.isAssignableFrom(o.getClass())) { ((StreamingOutput)o).write(os); } else { throw new IOException("Unrecognized class"); } }
public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException { if (InputStream.class.isAssignableFrom(o.getClass())) { copyInputToOutput((InputStream)o, os, headers); } else if (File.class.isAssignableFrom(o.getClass())) { copyInputToOutput(new BufferedInputStream( new FileInputStream((File)o)), os, headers); } else if (byte[].class.isAssignableFrom(o.getClass())) { copyInputToOutput(new ByteArrayInputStream((byte[])o), os, headers); } else if (Reader.class.isAssignableFrom(o.getClass())) { try { Writer writer = new OutputStreamWriter(os, getEncoding(type)); IOUtils.copy((Reader)o, writer, BUFFER_SIZE); writer.flush(); } finally { ((Reader)o).close(); } } else if (StreamingOutput.class.isAssignableFrom(o.getClass())) { ((StreamingOutput)o).write(os); } else { throw new IOException("Unrecognized class"); } }
return clazz.cast(new InputStreamReader(is, getEncoding(type)));
public T readFrom(Class<T> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, String> headers, InputStream is) throws IOException { try { if (InputStream.class.isAssignableFrom(clazz)) { return clazz.cast(is); } if (Reader.class.isAssignableFrom(clazz)) { return clazz.cast(new InputStreamReader(is, getEncoding(type))); } if (byte[].class.isAssignableFrom(clazz)) { String enc = getCharset(type); if (enc == null) { return clazz.cast(IOUtils.readBytesFromStream(is)); } else { return clazz.cast(IOUtils.toString(is, enc).getBytes(enc)); } } if (StreamingOutput.class.isAssignableFrom(clazz)) { return clazz.cast(new ReadingStreamingOutput(is)); } } catch (ClassCastException e) { String msg = "Unsupported class: " + clazz.getName(); LOG.warning(msg); throw ExceptionUtils.toInternalServerErrorException(null, null); } throw new IOException("Unrecognized class"); }