public static String convertTextToObject(Object source, MediaType sourceType) {
      if (source == null) return null;
      if (source instanceof String) return source.toString();
      if (source instanceof byte[]) {
         byte[] bytesSource = (byte[]) source;
         return new String(bytesSource, sourceType.getCharset());
      }
      throw log.invalidTextContent(source);
   }

   /**
    * Convert text format to a URL safe format.
    *
    * @param source the source text/plain content.
    * @param sourceType the MediaType of the source content.
    * @return a String with the content URLEncoded.
    * @throws EncodingException if the source format cannot be interpreted as plain/text.
    */
   public static String convertTextToUrlEncoded(Object source, MediaType sourceType) {
      return urlEncode(source, sourceType);
   }

   /**
    * Converts generic byte[] to text.
    *
    * @param source byte[] to convert.
    * @param destination MediaType of the desired text conversion.
    * @return byte[] content interpreted as text, in the encoding specified by the destination MediaType.
    */
   public static byte[] convertOctetStreamToText(byte[] source, MediaType destination) {
      if (source == null) return null;
      return convertCharset(source, UTF_8, destination.getCharset());
   }

   /**
    * Converts an octet stream to a Java object
    *
    * @param source The source to convert
    * @param destination The type of the converted object.
    * @return an instance of a java object compatible with the supplied destination type.
    */
   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());
      }
   }
public static String convertTextToObject ( Object source , MediaType sourceType ) { if ( source == null ) return null ; if ( source instanceof String ) return source . toString ( ) ; if ( source instanceof byte [ ] ) { byte [ ] bytesSource = ( byte [ ] ) source ; return new String ( bytesSource , sourceType . getCharset ( ) ) ; } throw log . invalidTextContent ( source ) ; } public static String convertTextToUrlEncoded ( Object source , MediaType sourceType ) { return urlEncode ( source , sourceType ) ; } public static byte [ ] convertOctetStreamToText ( byte [ ] source , MediaType destination ) { if ( source == null ) return null ; return convertCharset ( source , UTF_8 , destination . getCharset ( ) ) ; } 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 ( ) ) ; } }
Converts text content to the Java representation (String).

@param source The source content
@param sourceType the MediaType of the source content.
@return String representation of the text content.
@throws EncodingException if the source cannot be interpreted as plain text.