Myna. JavaUtils

Collection of static functions for working with Java objects

Summary
Myna. JavaUtilsCollection of static functions for working with Java objects
Functions
base64ToByteArrayreturns a Java byte[] from a Base64 string
beanToObjectAttempts to convert a JavaBean in to JavaScript object
byteArrayToHexreturns HEX string representation of a Java byte[]
byteArrayToBase64returns Base64 string representation of a Java byte[]
createSyncFunctionreturns a thread-safe version of a javascript function
createByteArrayreturns a java Byte array of the supplied size
createClassArrayreturns a java array of the supplied class and size
createCharArrayreturns a java Character array of the supplied size
enumToArrayConverts a Java enumeration into a JavaScript Array.
hexToByteArrayreturns a Java byte[] representation of a HEX string
mapToObjectreturns a Java Map as a JavaScript Object
readerToStringreturns a JavaScript String containing the entire contents off the supplied Java Reader.
streamToStringreturns a JavaScript String containing the entire contents off the supplied Java InputStream.
streamToByteArrayreturns a Java byte[] containing the entire contents off the supplied Java InputStream.
streamCopyCopies a java.io.InputStream to a java.io.OutputStream
readerToByteArrayreturns a Java byte[] containing the entire contents off the supplied Java Reader.

Functions

base64ToByteArray

Myna.JavaUtils.base64ToByteArray = function(string)

returns a Java byte[] from a Base64 string

Paramters

bytesbyte array to convert

See Also

  • <base64ByteArray>

beanToObject

Myna.JavaUtils.beanToObject=function(bean)

Attempts to convert a JavaBean in to JavaScript object

Parameters

beanJava object to convert

Returns

JavaScript Object

Detail

This function examines bean and looks for functions that start with “get” followed by at least one other character.  These functions are executed and their values are returned as a Javascript object where the property names are the get functions minus the “get” and the following character lower cased.  Functions that start with “is” are treated similarly, except the “is” is kept in the proerty name

*EXAMPLE*

this class...

public class PersonBean implements java.io.Serializable {
private name="bob";
private deceased=true;
...
public String getName() {
return this.name;
}

public boolean isDeceased() {
return this.deceased;
}
}

would become...

{
name:"bob",
isDeceased:true
}

byteArrayToHex

Myna.JavaUtils.byteArrayToHex = function(bytes)

returns HEX string representation of a Java byte[]

Paramters

bytesbyte array to convert

See Also

byteArrayToBase64

Myna.JavaUtils.byteArrayToBase64 = function(bytes,
urlSafe)

returns Base64 string representation of a Java byte[]

Paramters

bytesbyte array to convert
urlSafeOptional, default false If true, then the characters + and / and = will be replaced with
  • and _ and . respectively.  base64ToByteArray will automatically detect this format, but it is non-standard so other libraries may not properly parse it.  See Also:
  • <base64ByteArray>

createSyncFunction

Myna.JavaUtils.createSyncFunction=function(functionObject)

returns a thread-safe version of a javascript function

Parameters

functionObjectJavaScript Function object to synchronize

Returns

a thread-safe version functionObject, similar to synchronized keyword for Java functions

See Also

var s ={
string:"",
append:Myna.JavaUtils.createSyncFunction(function(val){
// This kind of multi-step string manipulation usually results in
// thread collision, but "sync"ing the function ensures that all
// steps are completed before another thread can run the same
// function
var s = this
var old =s.string
Myna.sleep(10); //gives another thread the chance to whomp our string
s.string= old+val;
})
}

Array.dim(10).forEach(function(d,i){
new Myna.Thread(function(i,s){
s.append(i+",");
},[i,s])
})
Myna.Thread.joinAll();
Myna.printDump(s.string.split(/,/));

createByteArray

Myna.JavaUtils.createByteArray=function(size)

returns a java Byte array of the supplied size

Parameters

sizeinitial size of array

Detail

When working with binary data or streams it is often necessary to pass a Byte array to java functions as a buffer.  This function will create this buffer for you

createClassArray

Myna.JavaUtils.createClassArray=function(className,
size)

returns a java array of the supplied class and size

Paramters

classNamefull classname to instantiate, e.g. java.lang.String
sizeinitial size of array

Detail

creates an array of a particular class

createCharArray

Myna.JavaUtils.createCharArray=function(size)

returns a java Character array of the supplied size

Detail

When working with striong data or streams it is often necessary to pass a Character array to java functions as a buffer.  This function will create this buffer for you

enumToArray

Myna.JavaUtils.enumToArray=function(enumeration)

Converts a Java enumeration into a JavaScript Array.

Parameters

enumerationJava enumeration to iterate over

Returns

JS Array object

hexToByteArray

Myna.JavaUtils.hexToByteArray = function(string)

returns a Java byte[] representation of a HEX string

Paramters

stringbyte array to convert

See Also

mapToObject

Myna.JavaUtils.mapToObject=function(map)

returns a Java Map as a JavaScript Object

Parameters

mapJava Map (or subclass/implementation)

Returns

a Java Map as a JavaScript Object

Detail

This function loops through all of the entries in the map and creates a property on the returned object for each key and sets it equal to the entry’s value.

readerToString

Myna.JavaUtils.readerToString=function(reader)

returns a JavaScript String containing the entire contents off the supplied Java Reader.

Parameters

readerJava Reader (or subclass/implementation)

streamToString

Myna.JavaUtils.streamToString=function(stream)

returns a JavaScript String containing the entire contents off the supplied Java InputStream.

Parameters

streamJava InputStream (or subclass/implementation)

streamToByteArray

Myna.JavaUtils.streamToByteArray=function(stream)

returns a Java byte[] containing the entire contents off the supplied Java InputStream.

Parameters

streamJava InputStream (or subclass/implementation)

streamCopy

Myna.JavaUtils.streamCopy=function(input,
output,
closeStreams)

Copies a java.io.InputStream to a java.io.OutputStream

Parameters

inputJava InputStream (or subclass/implementation)
outputJava OutputStream (or subclass/implementation)
closeStreamsOptional, default false If true, both streams will be closed after the copy

readerToByteArray

Myna.JavaUtils.readerToByteArray=function(reader)

returns a Java byte[] containing the entire contents off the supplied Java Reader.

Parameters

readerJava InputStream (or subclass/implementation)
Myna.JavaUtils.base64ToByteArray = function(string)
returns a Java byte[] from a Base64 string
Myna.JavaUtils.beanToObject=function(bean)
Attempts to convert a JavaBean in to JavaScript object
Myna.JavaUtils.byteArrayToHex = function(bytes)
returns HEX string representation of a Java byte[]
Myna.JavaUtils.byteArrayToBase64 = function(bytes,
urlSafe)
returns Base64 string representation of a Java byte[]
Myna.JavaUtils.createSyncFunction=function(functionObject)
returns a thread-safe version of a javascript function
Myna.JavaUtils.createByteArray=function(size)
returns a java Byte array of the supplied size
Myna.JavaUtils.createClassArray=function(className,
size)
returns a java array of the supplied class and size
Myna.JavaUtils.createCharArray=function(size)
returns a java Character array of the supplied size
Myna.JavaUtils.enumToArray=function(enumeration)
Converts a Java enumeration into a JavaScript Array.
Myna.JavaUtils.hexToByteArray = function(string)
returns a Java byte[] representation of a HEX string
Myna.JavaUtils.mapToObject=function(map)
returns a Java Map as a JavaScript Object
Myna.JavaUtils.readerToString=function(reader)
returns a JavaScript String containing the entire contents off the supplied Java Reader.
Myna.JavaUtils.streamToString=function(stream)
returns a JavaScript String containing the entire contents off the supplied Java InputStream.
Myna.JavaUtils.streamToByteArray=function(stream)
returns a Java byte[] containing the entire contents off the supplied Java InputStream.
Myna.JavaUtils.streamCopy=function(input,
output,
closeStreams)
Copies a java.io.InputStream to a java.io.OutputStream
Myna.JavaUtils.readerToByteArray=function(reader)
returns a Java byte[] containing the entire contents off the supplied Java Reader.
Myna.sync=function(func)
returns a synchronized version of a function that can only executed by one thread at a time.