Top Description Fields Constructors Methods
org.codehaus.groovy.runtime

public Class EncodingGroovyMethods

extends Object
Class Inheritance
Imports
groovy.lang.StringWriterIOException, .Writable, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation, java.io.IOException, .Writer, java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.security.MessageDigest, .NoSuchAlgorithmException

This class defines all the encoding/decoding groovy methods which enhance the normal JDK classes when inside the Groovy environment. Static methods are used with the first parameter the destination class.

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final String
private static final char[]
private static final char[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

the decoded bytes as an array
decodeBase64
(String
the string to be decoded
value
)

Decode the String from Base64 into a byte array.

private static byte[]
decodeBase64(String value, boolean urlSafe)

public static byte[]

Returns:

the decoded bytes as an array
decodeBase64Url
(String
the string to be decoded
value
)

Decodes a Base64 URL and Filename Safe encoded String into a byte array.

public static byte[]

Returns:

decoded byte array
decodeHex
(final String
string to be decoded
value
)

Decodes a hex string to a byte array.

public static String

Returns:

digested value
digest
(CharSequence self, String
the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc.
algorithm
)

digest the CharSequence instance

public static String

Returns:

digested value
digest
(byte[] self, String
the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc.
algorithm
)

digest the byte array

public static Writable

Returns:

object which will write the Base64 encoding of the byte array
encodeBase64
(Byte[]
Byte array to be encoded
data
,
final boolean
whether or not the Base64 encoded data should be MIME chunked
chunked
)

Produce a Writable object which writes the Base64 encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 encoding of the byte array
encodeBase64
(Byte[]
Byte array to be encoded
data
)

Produce a Writable object which writes the Base64 encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 encoding of the byte array
encodeBase64
(final byte[]
byte array to be encoded
data
,
final boolean
whether or not the Base64 encoded data should be MIME chunked
chunked
)

Produce a Writable object which writes the Base64 encoding of the byte array.

private static Writable
encodeBase64(final byte[] data, final boolean chunked, final boolean urlSafe, final boolean pad)

public static Writable

Returns:

object which will write the Base64 encoding of the byte array
encodeBase64
(final byte[]
byte array to be encoded
data
)

Produce a Writable object which writes the Base64 encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 URL and Filename Safe encoding of the byte array
encodeBase64Url
(Byte[]
Byte array to be encoded
data
)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 URL and Filename Safe encoding of the byte array
encodeBase64Url
(Byte[]
Byte array to be encoded
data
,
boolean
whether or not the encoded data should be padded
pad
)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 URL and Filename Safe encoding of the byte array
encodeBase64Url
(final byte[]
Byte array to be encoded
data
)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.

public static Writable

Returns:

object which will write the Base64 URL and Filename Safe encoding of the byte array
encodeBase64Url
(final byte[]
Byte array to be encoded
data
,
final boolean
whether or not the encoded data should be padded
pad
)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.

public static Writable

Returns:

object which will write the hex encoding of the byte array
encodeHex
(final Byte[]
byte array to be encoded
data
)

Produces a Writable that writes the hex encoding of the Byte[].

public static Writable

Returns:

object which will write the hex encoding of the byte array
encodeHex
(final byte[]
byte array to be encoded
data
)

Produces a Writable that writes the hex encoding of the byte[].

public static String

Returns:

md5 value
md5
(CharSequence self)

Calculate md5 of the CharSequence instance

public static String

Returns:

md5 value
md5
(byte[] self)

Calculate md5 of the byte array

public static String

Returns:

SHA-256 value
sha256
(CharSequence self)

Calculate SHA-256 of the CharSequence instance

public static String

Returns:

SHA-256 value
sha256
(byte[] self)

Calculate SHA-256 of the byte array

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHUNK_SEPARATORback to summary
private static final String CHUNK_SEPARATOR
MD5back to summary
private static final String MD5
SHA_256back to summary
private static final String SHA_256
T_TABLEback to summary
private static final char[] T_TABLE
T_TABLE_URLSAFEback to summary
private static final char[] T_TABLE_URLSAFE

Constructor Detail

EncodingGroovyMethodsback to summary
public EncodingGroovyMethods()

Method Detail

decodeBase64back to summary
public static byte[] decodeBase64(String value)

Decode the String from Base64 into a byte array.

Parameters
value:String

the string to be decoded

Returns:byte[]

the decoded bytes as an array

Since
1.0
decodeBase64back to summary
private static byte[] decodeBase64(String value, boolean urlSafe)
decodeBase64Urlback to summary
public static byte[] decodeBase64Url(String value)

Decodes a Base64 URL and Filename Safe encoded String into a byte array.

Parameters
value:String

the string to be decoded

Returns:byte[]

the decoded bytes as an array

Since
2.5.0
decodeHexback to summary
public static byte[] decodeHex(final String value)

Decodes a hex string to a byte array. The hex string can contain either upper case or lower case letters.

Parameters
value:String

string to be decoded

Returns:byte[]

decoded byte array

Exceptions
NumberFormatException:
If the string contains an odd number of characters or if the characters are not valid hexadecimal values.
digestback to summary
public static String digest(CharSequence self, String algorithm) throws NoSuchAlgorithmException

digest the CharSequence instance

Parameters
algorithm:String

the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc.

Returns:String

digested value

Exceptions
NoSuchAlgorithmException:
if the algorithm not found
Since
2.5.0
See Also
MessageDigest#getInstance(java.lang.String)
digestback to summary
public static String digest(byte[] self, String algorithm) throws NoSuchAlgorithmException

digest the byte array

Parameters
algorithm:String

the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc.

Returns:String

digested value

Exceptions
NoSuchAlgorithmException:
if the algorithm not found
Since
2.5.0
See Also
MessageDigest#getInstance(java.lang.String)
encodeBase64back to summary
public static Writable encodeBase64(Byte[] data, final boolean chunked)

Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see RFC 4648.

Parameters
data:Byte[]

Byte array to be encoded

chunked:boolean

whether or not the Base64 encoded data should be MIME chunked

Returns:Writable

object which will write the Base64 encoding of the byte array

Since
1.5.1
encodeBase64back to summary
public static Writable encodeBase64(Byte[] data)

Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see RFC 4648.

Parameters
data:Byte[]

Byte array to be encoded

Returns:Writable

object which will write the Base64 encoding of the byte array

Since
1.0
encodeBase64back to summary
public static Writable encodeBase64(final byte[] data, final boolean chunked)

Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see RFC 4648.

Parameters
data:byte[]

byte array to be encoded

chunked:boolean

whether or not the Base64 encoded data should be MIME chunked

Returns:Writable

object which will write the Base64 encoding of the byte array

Since
1.5.7
encodeBase64back to summary
private static Writable encodeBase64(final byte[] data, final boolean chunked, final boolean urlSafe, final boolean pad)
encodeBase64back to summary
public static Writable encodeBase64(final byte[] data)

Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see RFC 4648.

Parameters
data:byte[]

byte array to be encoded

Returns:Writable

object which will write the Base64 encoding of the byte array

Since
1.0
encodeBase64Urlback to summary
public static Writable encodeBase64Url(Byte[] data)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet.

The method omits padding and is equivalent to calling org.codehaus.groovy.runtime.EncodingGroovyMethods#encodeBase64Url(Byte[], boolean) with a value of false.

Parameters
data:Byte[]

Byte array to be encoded

Returns:Writable

object which will write the Base64 URL and Filename Safe encoding of the byte array

Since
2.5.0
See Also
org.codehaus.groovy.runtime.EncodingGroovyMethods#encodeBase64Url(Byte[], boolean)
encodeBase64Urlback to summary
public static Writable encodeBase64Url(Byte[] data, boolean pad)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet.

Parameters
data:Byte[]

Byte array to be encoded

pad:boolean

whether or not the encoded data should be padded

Returns:Writable

object which will write the Base64 URL and Filename Safe encoding of the byte array

Since
2.5.0
encodeBase64Urlback to summary
public static Writable encodeBase64Url(final byte[] data)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet.

The method omits padding and is equivalent to calling org.codehaus.groovy.runtime.EncodingGroovyMethods#encodeBase64Url(byte[], boolean) with a value of false.

Parameters
data:byte[]

Byte array to be encoded

Returns:Writable

object which will write the Base64 URL and Filename Safe encoding of the byte array

Since
2.5.0
See Also
org.codehaus.groovy.runtime.EncodingGroovyMethods#encodeBase64Url(byte[], boolean)
encodeBase64Urlback to summary
public static Writable encodeBase64Url(final byte[] data, final boolean pad)

Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet.

Parameters
data:byte[]

Byte array to be encoded

pad:boolean

whether or not the encoded data should be padded

Returns:Writable

object which will write the Base64 URL and Filename Safe encoding of the byte array

Since
2.5.0
encodeHexback to summary
public static Writable encodeHex(final Byte[] data)

Produces a Writable that writes the hex encoding of the Byte[]. Calling toString() on this Writable returns the hex encoding as a String. The hex encoding includes two characters for each byte and all letters are lower case.

Parameters
data:Byte[]

byte array to be encoded

Returns:Writable

object which will write the hex encoding of the byte array

See Also
Integer#toHexString(int)
encodeHexback to summary
public static Writable encodeHex(final byte[] data)

Produces a Writable that writes the hex encoding of the byte[]. Calling toString() on this Writable returns the hex encoding as a String. The hex encoding includes two characters for each byte and all letters are lower case.

Parameters
data:byte[]

byte array to be encoded

Returns:Writable

object which will write the hex encoding of the byte array

See Also
Integer#toHexString(int)
md5back to summary
public static String md5(CharSequence self) throws NoSuchAlgorithmException

Calculate md5 of the CharSequence instance

Returns:String

md5 value

Exceptions
NoSuchAlgorithmException:
if MD5 algorithm not found
Since
2.5.0
md5back to summary
public static String md5(byte[] self) throws NoSuchAlgorithmException

Calculate md5 of the byte array

Returns:String

md5 value

Exceptions
NoSuchAlgorithmException:
if MD5 algorithm not found
Since
2.5.0
sha256back to summary
public static String sha256(CharSequence self) throws NoSuchAlgorithmException

Calculate SHA-256 of the CharSequence instance

Returns:String

SHA-256 value

Exceptions
NoSuchAlgorithmException:
if SHA-256 algorithm not found
Since
2.5.3
sha256back to summary
public static String sha256(byte[] self) throws NoSuchAlgorithmException

Calculate SHA-256 of the byte array

Returns:String

SHA-256 value

Exceptions
NoSuchAlgorithmException:
if SHA-256 algorithm not found
Since
2.5.3