class BinaryDataProvider extends AbstractConfigurableProvider implements MessageBodyReader<T>, MessageBodyWriter<T> {

    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)) {
                if (DigestInputStream.class.isAssignableFrom(clazz)) {
                    is = new MessageDigestInputStream(is);
                }
                return clazz.cast(is);
            }
            if (Reader.class.isAssignableFrom(clazz)) {
                return clazz.cast(new InputStreamReader(is, getEncoding(type)));
            }
            if (byte[].class.isAssignableFrom(clazz)) {
                return clazz.cast(IOUtils.readBytesFromStream(is));
            }
            if (File.class.isAssignableFrom(clazz)) {
                LOG.warning("Reading data into File objects with the help of pre-packaged"
                    + " providers is not recommended - use InputStream or custom File reader");
                // create a temp file, delete on exit
                File f = FileUtils.createTempFile("File" + UUID.randomUUID().toString(),
                                                  "jaxrs",
                                                  null,
                                                  true);
                OutputStream os = Files.newOutputStream(f.toPath());
                IOUtils.copy(is, os, bufferSize);
                os.close();
                return clazz.cast(f);
            }
            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");
    }

}