public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) {
if (source == null) return null;
if (!destination.match(MediaType.APPLICATION_OBJECT)) {
throw log.invalidMediaType(APPLICATION_OBJECT_TYPE, destination.toString());
}
String classType = destination.getClassType();
if (classType == null) return source;
if (classType.equals("ByteArray")) {
return source;
}
if (destination.hasStringType()) {
return new String(source, UTF_8);
}
try {
return marshaller.objectFromByteBuffer(source);
} catch (IOException | ClassNotFoundException e) {
throw log.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString());
}
}