concept byte in category mule

appears as: bytes, byte, bytes
Mule in Action, Second Edition

This is an excerpt from Manning's book Mule in Action, Second Edition.

Our tour of Mule will begin with a tutorial. You’ll build an application to allow third parties to register products for sale on www.theprancingdonkey.com, Prancing Donkey’s online store. This application will allow Prancing Donkey’s partners to post product data, formatted as JSON, to an HTTP URL. Once the data is accepted, it will be transformed from its original format, a stream of bytes, into a String and placed in a JMS queue from which subsequent processing can take place. This tutorial will demonstrate common tasks you’ll perform when building applications with Mule.

4.3.1. Dealing with bytes

Bytes are the finer grain of data unit that’s exchanged between systems. In these systems, bytes are usually handled as streams or as arrays, with the former suited for large volumes of data and the latter for smaller chunks of information.

In Mule core, the ByteArrayTo... transformers are able to handle both arrays and streams of bytes as their input. This means that if the endpoint you’re declaring a transformer on is capable of streaming, the transformer will be able to act on the flow of data without the need to first store it in an array.

Let’s consider first the versatile byte-array-to-object-transformer. If this transformer receives a byte payload that represents a serialized Java object, the result will be an object deserialized from these bytes. If the payload isn’t a serialized object, the transformer will build a String from the bytes. The following code demonstrates this transformer, configured to deserialize byte arrays or streams into instances of java.util.Map only:

The alter ego of this transformer is the object-to-byte-array-transformer. As you’d expect, it works the opposite way; it transforms Strings and streams into byte arrays and marshals serializable payloads into bytes using the standard Java serialization mechanism. Note that this transformer chokes on nonserializable payloads as it can’t possibly perform any transformation on them. The following shows the simple declaration of the transformer to copy all the bytes of an incoming input stream into an array:

<object-to-byte-array-transformer name="ObjectToByteArray" />