@Test public void testJavaToOctetStreamConversion() throws IOException, InterruptedException { Marshaller marshaller = new ProtoStreamMarshaller(); String string = "I've seen things you people wouldn't believe."; Double number = 12.1d; Instant complex = Instant.now(); byte[] binary = new byte[]{1, 2, 3}; MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); byte[] result = StandardConversions.convertJavaToOctetStream(string, stringType, marshaller); assertArrayEquals(string.getBytes(UTF_8), result); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); result = StandardConversions.convertJavaToOctetStream(number, doubleType, marshaller); assertArrayEquals(marshaller.objectToByteBuffer(number), result); MediaType customType = APPLICATION_OBJECT.withParameter("type", complex.getClass().getName()); result = StandardConversions.convertJavaToOctetStream(complex, customType, marshaller); assertArrayEquals(marshaller.objectToByteBuffer(complex), result); MediaType byteArrayType = APPLICATION_OBJECT.withParameter("type", "ByteArray"); result = StandardConversions.convertJavaToOctetStream(binary, byteArrayType, marshaller); assertArrayEquals(result, binary); }
public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("sourceMediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); }
StandardConversions { public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("sourceMediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); } }
StandardConversions { public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("sourceMediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); }  }
StandardConversions { public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("sourceMediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); }  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 byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("sourceMediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); }  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);  }