@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}; Marshaller marshaller = new ProtoStreamMarshaller(); 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); }
public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw CONTAINER.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 | IllegalStateException | ClassNotFoundException e) { throw CONTAINER.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }
StandardConversions { public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw CONTAINER.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 | IllegalStateException | ClassNotFoundException e) { throw CONTAINER.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } } }
StandardConversions { public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw CONTAINER.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 | IllegalStateException | ClassNotFoundException e) { throw CONTAINER.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }  }
StandardConversions { public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw CONTAINER.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 | IllegalStateException | ClassNotFoundException e) { throw CONTAINER.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }  static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType); static byte[] convertTextToOctetStream(Object source, MediaType sourceType); static String convertTextToObject(Object source, MediaType sourceType); static String convertTextToUrlEncoded(Object source, MediaType sourceType); static byte[] convertOctetStreamToText(byte[] source, MediaType destination); static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller); static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller); static byte[] convertJavaToProtoStream(Object source, MediaType sourceMediaType, ImmutableSerializationContext ctx); static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType); static Object decodeObjectContent(Object content, MediaType contentMediaType); static byte[] convertCharset(Object content, Charset fromCharset, Charset toCharset); static byte[] decodeOctetStream(Object input, MediaType octetStream); static String bytesToHex(byte[] bytes); static byte[] hexToBytes(String hex); static Object convertUrlEncodedToObject(Object content); static Object convertUrlEncodedToText(Object content, MediaType destinationType); static Object convertUrlEncodedToOctetStream(Object content); static String urlEncode(Object content, MediaType mediaType); static Object urlDecode(Object content); static Object convertOctetStreamToUrlEncoded(Object content, MediaType contentType); }
StandardConversions { public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw CONTAINER.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 | IllegalStateException | ClassNotFoundException e) { throw CONTAINER.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }  static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType); static byte[] convertTextToOctetStream(Object source, MediaType sourceType); static String convertTextToObject(Object source, MediaType sourceType); static String convertTextToUrlEncoded(Object source, MediaType sourceType); static byte[] convertOctetStreamToText(byte[] source, MediaType destination); static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller); static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller); static byte[] convertJavaToProtoStream(Object source, MediaType sourceMediaType, ImmutableSerializationContext ctx); static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType); static Object decodeObjectContent(Object content, MediaType contentMediaType); static byte[] convertCharset(Object content, Charset fromCharset, Charset toCharset); static byte[] decodeOctetStream(Object input, MediaType octetStream); static String bytesToHex(byte[] bytes); static byte[] hexToBytes(String hex); static Object convertUrlEncodedToObject(Object content); static Object convertUrlEncodedToText(Object content, MediaType destinationType); static Object convertUrlEncodedToOctetStream(Object content); static String urlEncode(Object content, MediaType mediaType); static Object urlDecode(Object content); static Object convertOctetStreamToUrlEncoded(Object content, MediaType contentType);  }