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

concatWith

concatWith(source: Binary, with: Binary): Binary

Concatenates the content of two binaries.

Parameters
Name Type Description

source

Binary

The source binary content.

with

Binary

The binary to append.

Example

This example concats two binaries into one binary.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/dw
---
"CAFE" as Binary {base: "16"} concatWith "ABCD" as Binary {base: "16"}
Output
1
"yv6rzQ==" as Binary {base: "64"}

concatWith(source: Binary, with: Null): Binary

Helper function that enables concatWith to work with a null value.

concatWith(source: Null, with: Binary): Binary

Helper function that enables concatWith to work with a null value.

fromBase64

fromBase64(base64String: String): Binary

Transforms a Base64 string into a binary value.

Parameters
Name Description

base64String

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. The output of this function is a binary value that represents the image generated in example toBase64.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/octet-stream
---
fromBase64(payload)

fromHex

fromHex(hexString: String): Binary

Transforms a hexadecimal string into a binary.

Parameters
Name Description

hexString

A hexadecimal string to transform.

Example

This example transforms a hexadecimal string to "Mule". To make the resulting type clear, 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(content: Binary, charset: String): Array<String>

Splits the specified binary content into lines and returns the results in an array.

Parameters
Name Description

content

Binary data to read and split.

charset

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(content: Binary): String

Transforms a binary value into a Base64 string.

Parameters
Name Description

content

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(content: Binary): String

Transforms a binary value into a hexadecimal string.

Parameters
Name Description

content

The Binary value to transform.

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(content: Array<String>, charset: String): Binary

Writes the specified lines and returns the binary content.

Parameters
Name Description

content

Array of items to write.

charset

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"
}