public class Util extends Object
Constructor and Description |
---|
Util() |
Modifier and Type | Method and Description |
---|---|
static void |
cleanup(Object o)
Closes the object (connection, stream etc.) without throwing any exception, even if the
object is null
|
static void |
copy(InputStream i,
OutputStream o)
Copy the input stream into the output stream, closes both streams when finishing or in
a case of an exception
|
static void |
copy(InputStream i,
OutputStream o,
int bufferSize)
Copy the input stream into the output stream, closes both streams when finishing or in
a case of an exception
|
static void |
copyNoClose(InputStream i,
OutputStream o,
int bufferSize)
Copy the input stream into the output stream, without closing the streams when done
|
static void |
copyNoClose(InputStream i,
OutputStream o,
int bufferSize,
IOProgressListener callback)
Copy the input stream into the output stream, without closing the streams when done
|
static String |
decode(String s,
String enc,
boolean plusToSpace)
Decodes a String URL encoded URL
|
static AsyncResource<Image> |
downloadImageToCache(String url)
Downloads an image to the cache asynchronously.
|
static void |
downloadImageToCache(String url,
SuccessCallback<Image> onSuccess,
FailureCallback<Image> onFail)
Downloads an image to the cache asynchronously.
|
static AsyncResource<Image> |
downloadImageToFileSystem(String url,
String fileName)
Downloads an image to the file system asynchronously.
|
static void |
downloadImageToFileSystem(String url,
String fileName,
SuccessCallback<Image> onSuccess)
Downloads an image to the file system asynchronously.
|
static void |
downloadImageToFileSystem(String url,
String fileName,
SuccessCallback<Image> onSuccess,
FailureCallback<Image> onFail)
Downloads an image to the file system asynchronously.
|
static AsyncResource<Image> |
downloadImageToStorage(String url,
String fileName)
Downloads an image to storage asynchronously.
|
static void |
downloadImageToStorage(String url,
String fileName,
SuccessCallback<Image> onSuccess)
Downloads an image to storage asynchronously.
|
static void |
downloadImageToStorage(String url,
String fileName,
SuccessCallback<Image> onSuccess,
FailureCallback<Image> onFail)
Downloads an image to storage asynchronously.
|
static void |
downloadUrlSafely(String url,
String fileName,
OnComplete<Integer> percentageCallback,
OnComplete<String> filesavedCallback)
Safely download the given URL to the Storage or to the FileSystemStorage:
this method is resistant to network errors and capable of resume the
download as soon as network conditions allow and in a completely
transparent way for the user; note that in the global network error
handling, there must be an automatic
|
static boolean |
downloadUrlToFile(String url,
String fileName,
boolean showProgress)
Blocking method that will download the given URL to the file system storage and return when the
operation completes
|
static void |
downloadUrlToFileSystemInBackground(String url,
String fileName)
Non-blocking method that will download the given URL to file system storage in the background and return immediately
|
static void |
downloadUrlToFileSystemInBackground(String url,
String fileName,
ActionListener onCompletion)
Non-blocking method that will download the given URL to file system storage in the background and return immediately
|
static boolean |
downloadUrlToStorage(String url,
String fileName,
boolean showProgress)
Blocking method that will download the given URL to storage and return when the
operation completes
|
static void |
downloadUrlToStorageInBackground(String url,
String fileName)
Non-blocking method that will download the given URL to storage in the background and return
immediately.
|
static void |
downloadUrlToStorageInBackground(String url,
String fileName,
ActionListener onCompletion)
Non-blocking method that will download the given URL to storage in the background and return immediately
|
static String |
encodeBody(byte[] buf)
Deprecated.
use encodeUrl(char[]) instead
|
static String |
encodeBody(char[] buf)
Encode a string for HTML post requests matching the style used in application/x-www-form-urlencoded
|
static String |
encodeBody(String str)
Encode a string for HTML post requests matching the style used in application/x-www-form-urlencoded
|
static String |
encodeUrl(byte[] buf)
Deprecated.
use encodeUrl(char[]) instead
|
static String |
encodeUrl(char[] buf)
Encode a string for HTML requests
|
static String |
encodeUrl(String str)
Encode a string for HTML requests
|
static String |
encodeUrl(String str,
String doNotEncodeChars)
Encodes the provided string as a URL (with %20 for spaces).
|
static long |
getFileSizeWithoutDownload(String url)
Returns -1 if the content length is unknown, a value greater than 0 if
the Content-Length is known.
|
static long |
getFileSizeWithoutDownload(String url,
boolean checkPartialDownloadSupport)
Returns -2 if the server doesn't accept partial downloads (and if
checkPartialDownloadSupport is true), -1 if the content length is unknow,
a value greater than 0 if the Content-Length is known.
|
static String |
getIgnorCharsWhileEncoding()
These chars will not be encoded by the encoding method in this class
as requested in RFE 427 http://java.net/jira/browse/LWUIT-427
|
static String |
getURLBasePath(String url)
Returns the URL's base path, which is the same as the path only without an ending file e.g.:
http://domain.com/f/f.html would return as: /f/
|
static String |
getURLHost(String url)
Returns the URL's host portion
|
static String |
getURLPath(String url)
Returns the URL's path
|
static String |
getURLProtocol(String url)
Returns the protocol of an absolute URL e.g.
|
static String |
getUUID()
Creates a new UUID, that is a 128-bit number used to identify information
in computer systems.
|
static String |
getUUID(long time,
long clockSeqAndNode)
Creates a custom UUID, from the given two
long values. |
static String |
guessMimeType(byte[] data)
Tries to determine the mime type of a byte array based on its first
bytes.Direct inspection of the bytes to determine the content type is
often more accurate than believing the content type claimed by the
http server or by the file extension. |
static String |
guessMimeType(InputStream in)
Tries to determine the mime type of an InputStream based on its first
bytes.Direct inspection of the bytes to determine the content type is
often more accurate than believing the content type claimed by the
http server or by the file extension. |
static String |
guessMimeType(String sourceFile)
Tries to determine the mime type of a file based on its first
bytes.Direct inspection of the bytes to determine the content type is
often more accurate than believing the content type claimed by the
http server or by the file extension. |
static int |
indexOf(Object[] arr,
Object value)
Finds the object at the given offset while using the == operator and not the equals method call, it doesn't
rely on the ordering of the elements like the Arrays method.
|
static void |
insertObjectAtOffset(Object[] sourceArray,
Object[] destinationArray,
int offset,
Object o)
Inserts the object at the destination array offset
|
static boolean |
instanceofByteArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofDoubleArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofFloatArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofIntArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofLongArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofObjArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static boolean |
instanceofShortArray(Object o)
Deprecated.
this method serves as a temporary workaround for an XMLVM bug and will be removed
once the bug is fixed
|
static void |
mergeArrays(Object[] arr1,
Object[] arr2,
Object[] destinationArray)
Merges arrays into one larger array
|
static int |
readAll(InputStream i,
byte[] b)
Reads until the array is full or until the stream ends
|
static void |
readFully(InputStream i,
byte[] b)
The read fully method from data input stream is very useful for all types of
streams...
|
static void |
readFully(InputStream i,
byte[] b,
int off,
int len)
The read fully method from data input stream is very useful for all types of
streams...
|
static byte[] |
readInputStream(InputStream i)
Converts a small input stream to a byte array
|
static Object |
readObject(DataInputStream input)
Reads an object from the stream, notice that this is the inverse of the
writeObject(java.lang.Object, java.io.DataOutputStream) . |
static String |
readToString(File file)
Reads the contents of a file to a string.
|
static String |
readToString(File file,
String charset)
Reads the contents of a file to a string.
|
static String |
readToString(InputStream i)
Reads an input stream to a string
|
static String |
readToString(InputStream i,
String encoding)
Reads an input stream to a string
|
static String |
readToString(Reader reader)
Reads a reader to a string
|
static String |
readUTF(DataInputStream d)
Reads a UTF string that may be null previously written by writeUTF
|
static void |
register(Externalizable e)
Registers this externalizable so readObject will be able to load such objects.
|
static void |
register(String id,
Class c)
Registers this externalizable so readObject will be able to load such objects.
|
static String |
relativeToAbsolute(String baseURL,
String relativeURL)
Converts a relative url e.g.: /myfile.html to an absolute url
|
static void |
removeObjectAtOffset(Object[] sourceArray,
Object[] destinationArray,
int offset)
Removes the object at the source array offset and copies all other objects to the destination array
|
static void |
removeObjectAtOffset(Object[] sourceArray,
Object[] destinationArray,
Object o)
Removes the object at the source array offset and copies all other objects to the destination array
|
static void |
setDateFormatter(SimpleDateFormat formatter)
Sets a custom formatter to use when toDateValue is invoked
|
static void |
setIgnorCharsWhileEncoding(String s)
These chars will not be encoded by the encoding method in this class
as requested in RFE 427 http://java.net/jira/browse/LWUIT-427
|
static void |
setImplementation(CodenameOneImplementation impl)
Invoked internally from Display, this method is for internal use only
|
static void |
sleep(int t)
Shorthand method for Thread sleep that doesn't throw the stupid interrupted checked exception
|
static String[] |
split(String original,
String separator)
Provides a utility method breaks a given String to array of String according
to the given separator
|
static boolean |
toBooleanValue(Object val)
Returns true or false based on a "soft" object
|
static char[] |
toCharArray(String s)
toCharArray should return a new array always, however some devices might
suffer a bug that allows mutating a String (serious security hole in the JVM)
hence this method simulates the proper behavior
|
static Date |
toDateValue(Object o)
Tries to convert an arbitrary object to a date
|
static double |
toDoubleValue(Object number)
Returns the number object as a double
|
static float |
toFloatValue(Object number)
Returns the number object as a float
|
static int |
toIntValue(Object number)
Returns the number object as an int
|
static long |
toLongValue(Object number)
Returns the number object as a long
|
static void |
wait(Object o)
Shorthand method wait method that doesn't throw the stupid interrupted checked exception, it also
includes the synchronized block to further reduce code clutter
|
static void |
wait(Object o,
int t)
Shorthand method wait method that doesn't throw the stupid interrupted checked exception, it also
includes the synchronized block to further reduce code clutter
|
static void |
writeObject(Object o,
DataOutputStream out)
Writes an object to the given output stream, notice that it should be externalizable or one of
the supported types.
|
static void |
writeStringToFile(File file,
String contents)
Writes a string to a file using UTF-8 encoding.
|
static void |
writeStringToFile(File file,
String contents,
String charset)
Writes a string to a file.
|
static void |
writeUTF(String s,
DataOutputStream d)
Writes a string with a null flag, this allows a String which may be null
|
static String |
xorDecode(String s)
Encodes a string in a way that makes it harder to read it "as is" this makes it possible for Strings to be
"encoded" within the app and thus harder to discover by a casual search.
|
static String |
xorEncode(String s)
The inverse method of xorDecode, this is normally unnecessary and is here mostly for completeness
|
public static void setIgnorCharsWhileEncoding(String s)
s
- set of characters to skip when encodingpublic static String getIgnorCharsWhileEncoding()
public static void copy(InputStream i, OutputStream o) throws IOException
i
- sourceo
- destinationIOException
public static void copyNoClose(InputStream i, OutputStream o, int bufferSize) throws IOException
i
- sourceo
- destinationbufferSize
- the size of the buffer, which should be a power of 2 large enoughIOException
public static void copyNoClose(InputStream i, OutputStream o, int bufferSize, IOProgressListener callback) throws IOException
i
- sourceo
- destinationbufferSize
- the size of the buffer, which should be a power of 2 large enoughcallback
- called after each copy stepIOException
public static void copy(InputStream i, OutputStream o, int bufferSize) throws IOException
i
- sourceo
- destinationbufferSize
- the size of the buffer, which should be a power of 2 large enoughIOException
public static void cleanup(Object o)
o
- Connection, Stream or other closeable objectpublic static String readToString(InputStream i) throws IOException
i
- the input streamIOException
- thrown by the streampublic static String readToString(File file, String charset) throws IOException
file
- The file to read.charset
- The Charset to use to write the file.IOException
- If the file does not exist, or cannot be read for some reason.public static String readToString(File file) throws IOException
file
- The file to read.IOException
- If the file does not exist, or cannot be read for some reason.public static void writeStringToFile(File file, String contents) throws IOException
file
- The file to write to.contents
- The contents to write to the file.IOException
- If it cannot write to the file for some reason.public static void writeStringToFile(File file, String contents, String charset) throws IOException
file
- The file to write to.contents
- The contents to write to the file.charset
- The charset to use. If null, it defaults to UTF-8IOException
- If it cannot write to the file for some reason.public static String readToString(InputStream i, String encoding) throws IOException
i
- the input streamencoding
- the encoding of the streamIOException
- thrown by the streampublic static String readToString(Reader reader) throws IOException
i
- the input streamencoding
- the encoding of the streamIOException
- thrown by the streampublic static byte[] readInputStream(InputStream i) throws IOException
i
- the stream to convertIOException
public static void register(Externalizable e)
Registers this externalizable so readObject will be able to load such objects.
The sample below demonstrates the usage and registration of the Externalizable
interface:
e
- the externalizable instancepublic static void register(String id, Class c)
Registers this externalizable so readObject will be able to load such objects.
The sample below demonstrates the usage and registration of the Externalizable
interface:
id
- id of the externalizablec
- the class for the externalizablepublic static void writeObject(Object o, DataOutputStream out) throws IOException
Writes an object to the given output stream, notice that it should be externalizable or one of the supported types.
The sample below demonstrates the usage and registration of the Externalizable
interface:
o
- the object to write which can be nullout
- the destination output streamIOException
- thrown by the streampublic static boolean instanceofObjArray(Object o)
o
- object to testpublic static boolean instanceofByteArray(Object o)
o
- object to testpublic static boolean instanceofShortArray(Object o)
o
- object to testpublic static boolean instanceofLongArray(Object o)
o
- object to testpublic static boolean instanceofIntArray(Object o)
o
- object to testpublic static boolean instanceofFloatArray(Object o)
o
- object to testpublic static boolean instanceofDoubleArray(Object o)
o
- object to testpublic static Object readObject(DataInputStream input) throws IOException
Reads an object from the stream, notice that this is the inverse of the
writeObject(java.lang.Object, java.io.DataOutputStream)
.
The sample below demonstrates the usage and registration of the Externalizable
interface:
input
- the source input streamIOException
- thrown by the streampublic static String encodeUrl(String str)
str
- none encoded stringpublic static String encodeUrl(String str, String doNotEncodeChars)
str
- The URL to encodedoNotEncodeChars
- A string whose characters will not be encoded.public static char[] toCharArray(String s)
s
- a stringpublic static String decode(String s, String enc, boolean plusToSpace)
s
- the stringenc
- the encoding (defaults to UTF-8 if null)plusToSpace
- true if plus signs be converted to spacespublic static String encodeBody(String str)
str
- none encoded stringpublic static String encodeUrl(byte[] buf)
buf
- none encoded stringpublic static String encodeUrl(char[] buf)
buf
- none encoded stringpublic static String encodeBody(char[] buf)
buf
- none encoded stringpublic static String encodeBody(byte[] buf)
buf
- none encoded stringpublic static String relativeToAbsolute(String baseURL, String relativeURL)
baseURL
- a source URL whose properties should be used to construct the actual URLrelativeURL
- relative addresspublic static String getURLProtocol(String url)
url
- absolute URLpublic static String getURLHost(String url)
url
- absolute URLpublic static String getURLPath(String url)
url
- absolute URLpublic static String getURLBasePath(String url)
url
- absolute URLpublic static void writeUTF(String s, DataOutputStream d) throws IOException
s
- the string to writed
- the destination output streamIOException
public static String readUTF(DataInputStream d) throws IOException
d
- the streamIOException
public static void readFully(InputStream i, byte[] b) throws IOException
b
- the buffer into which the data is read.IOException
- the stream has been closed and the contained
input stream does not support reading after close, or
another I/O error occurs.public static final void readFully(InputStream i, byte[] b, int off, int len) throws IOException
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the number of bytes to read.IOException
- the stream has been closed and the contained
input stream does not support reading after close, or
another I/O error occurs.public static int readAll(InputStream i, byte[] b) throws IOException
b
- the buffer into which the data is read.IOException
- the stream has been closed and the contained
input stream does not support reading after close, or
another I/O error occurs.public static String[] split(String original, String separator)
original
- the String to breakseparator
- the pattern to look in the original Stringpublic static void setImplementation(CodenameOneImplementation impl)
impl
- implementation instancepublic static void mergeArrays(Object[] arr1, Object[] arr2, Object[] destinationArray)
public static void removeObjectAtOffset(Object[] sourceArray, Object[] destinationArray, Object o)
sourceArray
- the source arraydestinationArray
- the resulting array which should be of the length sourceArray.length - 1o
- the object to remove from the arraypublic static void removeObjectAtOffset(Object[] sourceArray, Object[] destinationArray, int offset)
sourceArray
- the source arraydestinationArray
- the resulting array which should be of the length sourceArray.length - 1offset
- the offset of the arraypublic static void insertObjectAtOffset(Object[] sourceArray, Object[] destinationArray, int offset, Object o)
sourceArray
- the source arraydestinationArray
- the resulting array which should be of the length sourceArray.length + 1offset
- the offset of the arrayo
- the objectpublic static int indexOf(Object[] arr, Object value)
arr
- the arrayvalue
- the value to searchpublic static boolean downloadUrlToStorage(String url, String fileName, boolean showProgress)
url
- the URLfileName
- the storage file nameshowProgress
- whether to block the UI until download completes/failspublic static boolean downloadUrlToFile(String url, String fileName, boolean showProgress)
url
- the URLfileName
- the file nameshowProgress
- whether to block the UI until download completes/failspublic static void downloadUrlToStorageInBackground(String url, String fileName)
Non-blocking method that will download the given URL to storage in the background and return
immediately. This method can be used to fetch data dynamically and asynchronously e.g. in this code it is used
to fetch book covers for the ImageViewer
:
url
- the URLfileName
- the storage file namepublic static void downloadUrlToFileSystemInBackground(String url, String fileName)
url
- the URLfileName
- the file namepublic static void downloadUrlToStorageInBackground(String url, String fileName, ActionListener onCompletion)
url
- the URLfileName
- the storage file nameonCompletion
- invoked when download completespublic static void downloadUrlToFileSystemInBackground(String url, String fileName, ActionListener onCompletion)
url
- the URLfileName
- the file nameonCompletion
- invoked when download completespublic static void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail)
url
- The URL to download the image from.fileName
- The the path to the file where the image should be downloaded. If this file already exists, it will simply load this file and skip the
network request altogether.onSuccess
- Callback called on success.onFail
- Callback called if we fail to load the image.ConnectionRequest.downloadImageToFileSystem(java.lang.String, com.codename1.util.SuccessCallback, com.codename1.util.FailureCallback)
public static AsyncResource<Image> downloadImageToFileSystem(String url, String fileName)
url
- The URL to download the image from.fileName
- The the path to the file where the image should be downloaded. If this file already exists, it will simply load this file and skip the
network request altogether.ConnectionRequest.downloadImageToFileSystem(java.lang.String, com.codename1.util.SuccessCallback, com.codename1.util.FailureCallback)
public static void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess)
url
- The URL to download the image from.fileName
- The the path to the file where the image should be downloaded. If this file already exists, it will simply load this file and skip the
network request altogether.onSuccess
- Callback called on success.ConnectionRequest.downloadImageToFileSystem(java.lang.String, com.codename1.util.SuccessCallback)
public static void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail)
url
- The URL to download the image from.fileName
- The the storage file to save the image to. If this file already exists, it will simply load this file and skip the
network request altogether.onSuccess
- Callback called on success.onFail
- Callback called if we fail to load the image.ConnectionRequest.downloadImageToStorage(java.lang.String, com.codename1.util.SuccessCallback, com.codename1.util.FailureCallback)
public static AsyncResource<Image> downloadImageToStorage(String url, String fileName)
url
- The URL to download the image from.fileName
- The the storage file to save the image to. If this file already exists, it will simply load this file and skip the
network request altogether.ConnectionRequest.downloadImageToStorage(java.lang.String, com.codename1.util.SuccessCallback, com.codename1.util.FailureCallback)
public static void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail)
url
- The URL to download.onSuccess
- Callback to run on successful completion.onFail
- Callback to run if download fails.public static AsyncResource<Image> downloadImageToCache(String url)
url
- The URL of the image to download.public static void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess)
url
- The URL to download the image from.fileName
- The the storage file to save the image to. If this file already exists, it will simply load this file and skip the
network request altogether.onSuccess
- Callback called on success.ConnectionRequest.downloadImageToStorage(java.lang.String, com.codename1.util.SuccessCallback)
public static void sleep(int t)
t
- the timepublic static void wait(Object o, int t)
o
- the object to wait ont
- the timepublic static void wait(Object o)
o
- the object to wait onpublic static boolean toBooleanValue(Object val)
val
- a boolean value as a Boolean object, String or numberpublic static int toIntValue(Object number)
number
- this can be a String or any number typepublic static long toLongValue(Object number)
number
- this can be a String or any number typepublic static float toFloatValue(Object number)
number
- this can be a String or any number typepublic static double toDoubleValue(Object number)
number
- this can be a String or any number typepublic static void setDateFormatter(SimpleDateFormat formatter)
formatter
- the formatter to usepublic static Date toDateValue(Object o)
o
- an object that can be a string, number or datepublic static String xorDecode(String s)
s
- the string to decodepublic static String xorEncode(String s)
s
- a regular stringpublic static String guessMimeType(String sourceFile) throws IOException
http
server or by the file extension.sourceFile,
- it automatically choose Storage API or
FileSystemStorage APIIOException
public static String guessMimeType(InputStream in) throws IOException
http
server or by the file extension.in
- IOException
public static String guessMimeType(byte[] data)
http
server or by the file extension.data
- public static long getFileSizeWithoutDownload(String url)
url
- public static long getFileSizeWithoutDownload(String url, boolean checkPartialDownloadSupport)
url
- checkPartialDownloadSupport
- if true returns -2 if the server
doesn't accept partial downloads.public static void downloadUrlSafely(String url, String fileName, OnComplete<Integer> percentageCallback, OnComplete<String> filesavedCallback) throws IOException
Safely download the given URL to the Storage or to the FileSystemStorage: this method is resistant to network errors and capable of resume the download as soon as network conditions allow and in a completely transparent way for the user; note that in the global network error handling, there must be an automatic
.retry(), as in the code example below.
This method is useful if the server correctly returns Content-Length and if it supports partial downloads: if not, it works like a normal download.
Pros: always allows you to complete downloads, even if very heavy (e.g. 100MB), even if the connection is unstable (network errors) and even if the app goes temporarily in the background (on some platforms the download will continue in the background, on others it will be temporarily suspended).
Cons: since this method is based on splitting the download into small parts (512kbytes is the default), this approach causes many GET requests that slightly slow down the download and cause more traffic than normally needed.
Usage example:
url
- fileName
- must be a valid Storage file name or FileSystemStorage
file pathpercentageCallback
- invoked (in EDT) during the download to notify
the progress (from 0 to 100); it can be null if you are not interested in
monitoring the progressfilesavedCallback
- invoked (in EDT) only when the download is
finished; if null, no action is takenIOException
public static String getUUID()
Creates a new UUID, that is a 128-bit number used to identify information in computer systems. UUIDs aim to be unique for practical purposes.
This implementation uses the system clock and some device info as seeds for random data, that are enough for practical usage. More specifically, two instances of Random, instantiated with different seeds, are used. The first seed corresponds to the timestamp in which the first object of the static class UUID is created, the second seed is a number (long type) that identifies the current installation of the app and is assumed to be as different as possible from other installations of the app. A unique identifier (long type) associated with the current app installation can be specified by the developer via the Preference "CustomDeviceId__$" (as in the following example) BEFORE the generation of the first UIID, or - if it is not specified - it is obtained from an internal Codename One implementation; in the worst case, if an identifier has not been specified by the developer and Codename One is unable to distinguish the current installation of the app from other installations, an internal algorithm will be used that will generate a number based on some hardware and software characteristics of the device: the number thus generated will be the same on identical models of the same device and with the same version of the operating system, but will vary between different models. Even in the worst case scenario, the probability that two app installations with identical device identifiers will generate the first UIID in the same timestamp is very low.
As a tip, consider that any alphanumeric text string (corresponding for example to a username) can be converted into a long type number, considering this string as a number based on 36, provided it does not exceed 12 characters. This suggestion is applied in the following example.
Code example:
public static String getUUID(long time, long clockSeqAndNode)
long
values.time
- the upper 64 bitsclockSeqAndNode
- the lower 64 bits