@Test public void testOctetStreamToJavaConversion() { String value = "It's not an easy thing to meet your maker."; byte[] textStream = value.getBytes(UTF_8); byte[] randomBytes = new byte[]{23, 23, 34, 1, -1, -123}; JavaSerializationMarshaller marshaller = new JavaSerializationMarshaller(); MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); Object result = StandardConversions.convertOctetStreamToJava(textStream, stringType, marshaller); assertEquals(value, result); MediaType byteArrayType = APPLICATION_OBJECT.withParameter("type", "ByteArray"); Object result2 = StandardConversions.convertOctetStreamToJava(textStream, byteArrayType, marshaller); assertArrayEquals(textStream, (byte[]) result2); Object result3 = StandardConversions.convertOctetStreamToJava(randomBytes, byteArrayType, marshaller); assertArrayEquals(randomBytes, (byte[]) result3); thrown.expect(EncodingException.class); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); StandardConversions.convertOctetStreamToJava(randomBytes, doubleType, marshaller); System.out.println(thrown); }
private Object convertToObject(Object content, MediaType contentType, MediaType destinationType) { if (contentType.match(APPLICATION_OCTET_STREAM)) { byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType); return StandardConversions.convertOctetStreamToJava(decoded, destinationType, javaMarshaller); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.decodeObjectContent(content, contentType); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToObject(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToObject(content); } if (contentType.equals(MediaType.APPLICATION_UNKNOWN)) { if (content instanceof byte[]) { return tryDeserialize((byte[]) content); } if (content instanceof WrappedByteArray) { return tryDeserialize(((WrappedByteArray) content).getBytes()); } return content; } throw log.unsupportedContent(content); }