This module contains helper functions for working with binaries.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Binaries
to the header of your
DataWeave script.
Functions
fromBase64
fromBase64(String): Binary
Transforms a Base64 string into a binary value.
Parameters
Name | Description |
---|---|
|
The Base64 string to transform. |
Example
This example takes a Base64 encoded string and transforms it into a binary value. This example assumes that the payload
contains the Base64 string generated from an image in example toBase64.
Source
1
2
3
4
5
6
%dw 2.0
import fromBase64 from dw::core::Binaries
output application/octet-stream
---
fromBase64(payload)
Output
The output of this function is a binary value that represents the image generated in example toBase64.
fromHex
fromHex(String): Binary
Transforms a hexadecimal string into a binary.
Parameters
Name | Description |
---|---|
|
A hexadecimal string to transform. |
Example
This example transforms a hexadecimal string to "Mule".
To show you the type, it outputs data in the application/dw
format.
Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/dw
---
{ "hexToBinary": fromHex("4D756C65") }
Output
1
2
3
{
hexToBinary: "TXVsZQ==" as Binary {base: "64"}
}
readLinesWith
readLinesWith(Binary, String): Array<String>
Splits the specified binary content into lines and returns the results in an array.
Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.
Parameters
Name | Description |
---|---|
|
Binary data to read and split. |
|
String representing the encoding to read. |
Example
This example transforms binary content, which is separated into new
lines (\n
), in a comma-separated array.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Binaries
var content = read("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n", "application/octet-stream")
output application/json
---
{
lines : (content readLinesWith "UTF-8"),
showType: typeOf(content)
}
Output
1
2
3
4
{
"lines": [ "Line 1", "Line 2", "Line 3", "Line 4", "Line 5" ],
"showType": "Binary"
}
toBase64
toBase64(Binary): String
Transforms a binary value into a Base64 string.
Parameters
Name | Description |
---|---|
|
The binary value to transform. |
Example
This example transforms a binary value into a Base64 encoded string. In this case, the binary value represents an image.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import dw::Crypto
import toBase64 from dw::core::Binaries
var emailChecksum = Crypto::MD5("achaval@gmail.com" as Binary)
var image = readUrl(log("https://www.gravatar.com/avatar/$(emailChecksum)"), "application/octet-stream")
output application/json
---
toBase64(image)
Output
This example outputs a Base64 encoded string. The resulting string was shortened for readability purposes:
1
"/9j/4AAQSkZJRgABAQEAYABgAAD//..."
toHex
toHex(Binary): String
Transforms a binary value into a hexadecimal string.
Parameters
Name | Description |
---|---|
|
The |
Example
This example transforms a binary version of "Mule" (defined in the variable,
myBinary
) to hexadecimal.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Binaries
output application/json
var myBinary = "Mule" as Binary
var testType = typeOf(myBinary)
---
{
"binaryToHex" : toHex(myBinary)
}
Output
1
{ "binaryToHex": "4D756C65" }
writeLinesWith
writeLinesWith(Array<String>, String): Binary
Writes the specified lines and returns the binary content.
Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.
Parameters
Name | Description |
---|---|
|
Array of items to write. |
|
String representing the encoding to use when writing. |
Example
This example inserts a new line (\n
) after each iteration. Specifically,
it uses map
to iterate over the result of to(1, 10)
, [1,2,3,4,5]
, then
writes the specified content ("Line $"), which includes the unnamed variable
$
for each number in the array.
Note that without writeLinesWith "UTF-8"
, the expression
{ lines: to(1, 10) map "Line $" }
simply returns
an array of line numbers as the value of an object:
{ "lines": [ "line 1", "line 2", "line 3", "line 4", "line 5" ] }
.
Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ lines: to(1, 10) map "Line $" writeLinesWith "UTF-8" }
Output
1
2
3
{
"lines": "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n"
}