Class StandardConversions
- java.lang.Object
-
- org.infinispan.commons.dataconversion.StandardConversions
-
public final class StandardConversions extends Object
Utilities to convert between text/plain, octet-stream, java-objects and url-encoded contents.- Since:
- 9.2
-
-
Constructor Summary
Constructors Constructor Description StandardConversions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
bytesToHex(byte[] bytes)
static byte[]
convertCharset(Object content, Charset fromCharset, Charset toCharset)
Convert text content.static byte[]
convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller)
Converts a java object to a sequence of bytes applying standard java serialization.static byte[]
convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType)
Converts a java object to a text/plain representation.static Object
convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller)
Converts an octet stream to a Java objectstatic byte[]
convertOctetStreamToText(byte[] source, MediaType destination)
Converts generic byte[] to text.static Object
convertOctetStreamToUrlEncoded(Object content, MediaType contentType)
static String
convertTextToObject(Object source, MediaType sourceType)
Converts text content to the Java representation (String).static byte[]
convertTextToOctetStream(Object source, MediaType sourceType)
Converts text content to binary.static Object
convertTextToText(Object source, MediaType sourceType, MediaType destinationType)
Convert text content to a different encoding.static String
convertTextToUrlEncoded(Object source, MediaType sourceType)
Convert text format to a URL safe format.static Object
convertUrlEncodedToObject(Object content)
Handle x-www-form-urlencoded as single values for now.static Object
convertUrlEncodedToOctetStream(Object content)
static Object
convertUrlEncodedToText(Object content, MediaType destinationType)
static Object
decodeObjectContent(Object content, MediaType contentMediaType)
Decode UTF-8 as a java object.static byte[]
decodeOctetStream(Object input, MediaType octetStream)
Decode a octet-stream content that is not a byte[].static byte[]
hexToBytes(String hex)
static Object
urlDecode(Object content)
static String
urlEncode(Object content, MediaType mediaType)
-
-
-
Method Detail
-
convertTextToText
public static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType)
Convert text content to a different encoding.- Parameters:
source
- The source content.sourceType
- MediaType for the source content.destinationType
- the MediaType of the converted content.- Returns:
- content conforming to the destination MediaType.
-
convertTextToOctetStream
public static byte[] convertTextToOctetStream(Object source, MediaType sourceType)
Converts text content to binary.- Parameters:
source
- The source content.sourceType
- MediaType for the source content.- Returns:
- content converted as octet-stream represented as byte[].
- Throws:
EncodingException
- if the source cannot be interpreted as plain text.
-
convertTextToObject
public static String convertTextToObject(Object source, MediaType sourceType)
Converts text content to the Java representation (String).- Parameters:
source
- The source contentsourceType
- the MediaType of the source content.- Returns:
- String representation of the text content.
- Throws:
EncodingException
- if the source cannot be interpreted as plain text.
-
convertTextToUrlEncoded
public static String convertTextToUrlEncoded(Object source, MediaType sourceType)
Convert text format to a URL safe format.- Parameters:
source
- the source text/plain content.sourceType
- the MediaType of the source content.- Returns:
- a String with the content URLEncoded.
- Throws:
EncodingException
- if the source format cannot be interpreted as plain/text.
-
convertOctetStreamToText
public static byte[] convertOctetStreamToText(byte[] source, MediaType destination)
Converts generic byte[] to text.- Parameters:
source
- byte[] to convert.destination
- MediaType of the desired text conversion.- Returns:
- byte[] content interpreted as text, in the encoding specified by the destination MediaType.
-
convertOctetStreamToJava
public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller)
Converts an octet stream to a Java object- Parameters:
source
- The source to convertdestination
- The type of the converted object.- Returns:
- an instance of a java object compatible with the supplied destination type.
-
convertJavaToOctetStream
public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException
Converts a java object to a sequence of bytes applying standard java serialization.- Parameters:
source
- source the java object to convert.sourceMediaType
- the MediaType matching application/x-application-object describing the source.- Returns:
- 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.IOException
InterruptedException
-
convertJavaToText
public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType)
Converts a java object to a text/plain representation.- Parameters:
source
- Object to convert.sourceMediaType
- The MediaType for the source object.destinationMediaType
- The required text/plain specification.- Returns:
- byte[] with the text/plain representation of the object with the requested charset.
-
decodeObjectContent
public static Object decodeObjectContent(Object content, MediaType contentMediaType)
Decode UTF-8 as a java object. For this conversion, the "type" parameter is used in the suppliedMediaType
. Currently supported types are primitives and String, plus a special "ByteArray" to describe a sequence of bytes.- Parameters:
content
- The content to decode.contentMediaType
- theMediaType
describing the content.- Returns:
- instance of Object according to the supplied MediaType "type" parameter, or if no type is present, the object itself.
- Throws:
EncodingException
- if the provided type is not supported.
-
convertCharset
public static byte[] convertCharset(Object content, Charset fromCharset, Charset toCharset)
Convert text content.- Parameters:
content
- Object to convert.fromCharset
- Charset of the provided content.toCharset
- Charset to convert to.- Returns:
- byte[] with the content in the desired charset.
-
decodeOctetStream
public static byte[] decodeOctetStream(Object input, MediaType octetStream)
Decode a octet-stream content that is not a byte[]. For this, it uses a special param called "encoding" in the "application/octet-stream" MediaType. The "encoding" param supports only the "hex" value that represents an octet-stream as a hexadecimal representation, for example "0xdeadbeef". In the absence of the "encoding" param, it will assume base64 encoding.- Parameters:
input
- Object representing the binary content.octetStream
- The MediaType describing the input.- Returns:
- a byte[] with the decoded content.
-
bytesToHex
public static String bytesToHex(byte[] bytes)
-
hexToBytes
public static byte[] hexToBytes(String hex)
-
convertUrlEncodedToObject
public static Object convertUrlEncodedToObject(Object content)
Handle x-www-form-urlencoded as single values for now. Ideally it should generate a Map<String, String>
-
convertUrlEncodedToText
public static Object convertUrlEncodedToText(Object content, MediaType destinationType)
-
-