code: 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("destination MediaType 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);
     }
docs: Converts a java object to a sequence of bytes applying standard java serialization.
  
  @param source source the java object to convert.
  @param sourceMediaType the MediaType matching application/x-application-object describing the source.
  @return byte[] representation of the java object.
  @throws EncodingException if the sourceMediaType is not a application/x-java-object or if the conversion is
  not supported.