public final class Util
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEVICE
Like
Build.DEVICE , but in a place where it can be conveniently overridden for local
testing. |
static java.lang.String |
MANUFACTURER
Like
Build.MANUFACTURER , but in a place where it can be conveniently overridden for
local testing. |
static java.lang.String |
MODEL
Like
Build.MODEL , but in a place where it can be conveniently overridden for local
testing. |
static int |
SDK_INT
Like
Build.VERSION.SDK_INT , but in a place where it can be conveniently
overridden for local testing. |
Modifier and Type | Method and Description |
---|---|
static boolean |
areEqual(java.lang.Object o1,
java.lang.Object o2)
Tests two objects for
Object.equals(Object) equality, handling the case where one or
both may be null. |
static <T> int |
binarySearchCeil(java.util.List<? extends java.lang.Comparable<? super T>> list,
T value,
boolean inclusive,
boolean stayInBounds)
Returns the index of the smallest value in an list that is greater than (or optionally equal
to) a specified value.
|
static int |
binarySearchCeil(long[] a,
long value,
boolean inclusive,
boolean stayInBounds)
Returns the index of the smallest value in an array that is greater than (or optionally equal
to) a specified value.
|
static int |
binarySearchFloor(int[] a,
int value,
boolean inclusive,
boolean stayInBounds)
Returns the index of the largest value in an array that is less than (or optionally equal to)
a specified value.
|
static <T> int |
binarySearchFloor(java.util.List<? extends java.lang.Comparable<? super T>> list,
T value,
boolean inclusive,
boolean stayInBounds)
Returns the index of the largest value in an list that is less than (or optionally equal to)
a specified value.
|
static int |
binarySearchFloor(long[] a,
long value,
boolean inclusive,
boolean stayInBounds)
Returns the index of the largest value in an array that is less than (or optionally equal to)
a specified value.
|
static int |
ceilDivide(int numerator,
int denominator)
Divides a
numerator by a denominator , returning the ceiled result. |
static long |
ceilDivide(long numerator,
long denominator)
Divides a
numerator by a denominator , returning the ceiled result. |
static void |
closeQuietly(DataSource dataSource)
Closes a
DataSource , suppressing any IOException that may occur. |
static void |
closeQuietly(java.io.OutputStream outputStream)
Closes an
OutputStream , suppressing any IOException that may occur. |
static int |
constrainValue(int value,
int min,
int max)
Constrains a value to the specified bounds.
|
static boolean |
contains(java.lang.Object[] items,
java.lang.Object item)
Tests whether an
items array contains an object equal to item , according to
Object.equals(Object) . |
static int |
crc(byte[] bytes,
int start,
int end,
int initialValue)
Returns the result of updating a CRC with the specified bytes in a "most significant bit first"
order.
|
static java.lang.String |
escapeFileName(java.lang.String fileName)
Escapes a string so that it's safe for use as a file or directory name on at least FAT32
filesystems.
|
static byte[] |
getBytesFromHexString(java.lang.String hexString)
Returns a byte array containing values parsed from the hex string provided.
|
static java.lang.String |
getCommaDelimitedSimpleClassNames(java.lang.Object[] objects)
Returns a string with comma delimited simple names of each object's class.
|
static int |
getDefaultBufferSize(int trackType)
|
static java.lang.String |
getHexString(byte[] bytes)
Returns a hex string representation of the given byte array.
|
static int |
getIntegerCodeForString(java.lang.String string)
Returns the integer equal to the big-endian concatenation of the characters in
string
as bytes. |
static int |
getPcmEncoding(int bitDepth)
Converts a sample bit depth to a corresponding PCM encoding constant.
|
static android.graphics.Point |
getPhysicalDisplaySize(android.content.Context context)
Gets the physical size of the default display, in pixels.
|
static android.graphics.Point |
getPhysicalDisplaySize(android.content.Context context,
android.view.Display display)
Gets the physical size of the specified display, in pixels.
|
static DataSpec |
getRemainderDataSpec(DataSpec dataSpec,
int bytesLoaded)
|
static java.lang.String |
getUserAgent(android.content.Context context,
java.lang.String applicationName)
Returns a user agent string based on the given application name and the library version.
|
static byte[] |
getUtf8Bytes(java.lang.String value)
Returns a new byte array containing the code points of a
String encoded using UTF-8. |
static int |
inferContentType(java.lang.String fileName)
Makes a best guess to infer the type from a file name.
|
static boolean |
isLocalFileUri(android.net.Uri uri)
Returns true if the URI is a path to a local file or a reference to a local file.
|
static boolean |
maybeRequestReadExternalStoragePermission(android.app.Activity activity,
android.net.Uri... uris)
Checks whether it's necessary to request the
Manifest.permission.READ_EXTERNAL_STORAGE
permission read the specified Uri s, requesting the permission if necessary. |
static java.util.concurrent.ExecutorService |
newSingleThreadExecutor(java.lang.String threadName)
Instantiates a new single threaded executor whose thread has the specified name.
|
static java.lang.String |
normalizeLanguageCode(java.lang.String language)
Returns a normalized RFC 5646 language code.
|
static long |
parseXsDateTime(java.lang.String value)
Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since
the epoch.
|
static long |
parseXsDuration(java.lang.String value)
Parses an xs:duration attribute value, returning the parsed duration in milliseconds.
|
static long |
scaleLargeTimestamp(long timestamp,
long multiplier,
long divisor)
Scales a large timestamp.
|
static long[] |
scaleLargeTimestamps(java.util.List<java.lang.Long> timestamps,
long multiplier,
long divisor)
Applies
scaleLargeTimestamp(long, long, long) to a list of unscaled timestamps. |
static void |
scaleLargeTimestampsInPlace(long[] timestamps,
long multiplier,
long divisor)
Applies
scaleLargeTimestamp(long, long, long) to an array of unscaled timestamps. |
static java.lang.String |
sha1(java.lang.String input)
Returns the SHA-1 digest of
input as a hex string. |
static int[] |
toArray(java.util.List<java.lang.Integer> list)
Converts a list of integers to a primitive array.
|
static byte[] |
toByteArray(java.io.InputStream inputStream)
Converts the entirety of an
InputStream to a byte array. |
static java.lang.String |
toLowerInvariant(java.lang.String text)
Converts text to lower case using
Locale.US . |
static java.lang.String |
unescapeFileName(java.lang.String fileName)
Unescapes an escaped file or directory name back to its original value.
|
public static final int SDK_INT
Build.VERSION.SDK_INT
, but in a place where it can be conveniently
overridden for local testing.public static final java.lang.String DEVICE
Build.DEVICE
, but in a place where it can be conveniently overridden for local
testing.public static final java.lang.String MANUFACTURER
Build.MANUFACTURER
, but in a place where it can be conveniently overridden for
local testing.public static final java.lang.String MODEL
Build.MODEL
, but in a place where it can be conveniently overridden for local
testing.public static byte[] toByteArray(java.io.InputStream inputStream) throws java.io.IOException
InputStream
to a byte array.inputStream
- the InputStream
to be read. The input stream is not closed by this
method.java.io.IOException
- if an error occurs reading from the stream.public static boolean maybeRequestReadExternalStoragePermission(android.app.Activity activity, android.net.Uri... uris)
Manifest.permission.READ_EXTERNAL_STORAGE
permission read the specified Uri
s, requesting the permission if necessary.activity
- The host activity for checking and requesting the permission.uris
- Uri
s that may require Manifest.permission.READ_EXTERNAL_STORAGE
to read.public static boolean isLocalFileUri(android.net.Uri uri)
uri
- The uri to test.public static boolean areEqual(java.lang.Object o1, java.lang.Object o2)
Object.equals(Object)
equality, handling the case where one or
both may be null.o1
- The first object.o2
- The second object.o1 == null ? o2 == null : o1.equals(o2)
.public static boolean contains(java.lang.Object[] items, java.lang.Object item)
items
array contains an object equal to item
, according to
Object.equals(Object)
.
If item
is null then true is returned if and only if items
contains null.
items
- The array of items to search.item
- The item to search for.public static java.util.concurrent.ExecutorService newSingleThreadExecutor(java.lang.String threadName)
threadName
- The name of the thread.public static void closeQuietly(DataSource dataSource)
DataSource
, suppressing any IOException
that may occur.dataSource
- The DataSource
to close.public static void closeQuietly(java.io.OutputStream outputStream)
OutputStream
, suppressing any IOException
that may occur.outputStream
- The OutputStream
to close.public static java.lang.String normalizeLanguageCode(java.lang.String language)
language
- A possibly non-normalized RFC 5646 language code.public static byte[] getUtf8Bytes(java.lang.String value)
String
encoded using UTF-8.value
- The String
whose bytes should be obtained.public static java.lang.String toLowerInvariant(java.lang.String text)
Locale.US
.text
- The text to convert.text
is null.public static int ceilDivide(int numerator, int denominator)
numerator
by a denominator
, returning the ceiled result.numerator
- The numerator to divide.denominator
- The denominator to divide by.public static long ceilDivide(long numerator, long denominator)
numerator
by a denominator
, returning the ceiled result.numerator
- The numerator to divide.denominator
- The denominator to divide by.public static int constrainValue(int value, int min, int max)
value
- The value to constrain.min
- The lower bound.max
- The upper bound.Math.max(min, Math.min(value, max))
.public static int binarySearchFloor(int[] a, int value, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, so the array must be sorted.
a
- The array to search.value
- The value being searched for.inclusive
- If the value is present in the array, whether to return the corresponding
index. If false then the returned index corresponds to the largest value in the array that
is strictly less than the value.stayInBounds
- If true, then 0 will be returned in the case that the value is smaller than
the smallest value in the array. If false then -1 will be returned.public static int binarySearchFloor(long[] a, long value, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, so the array must be sorted.
a
- The array to search.value
- The value being searched for.inclusive
- If the value is present in the array, whether to return the corresponding
index. If false then the returned index corresponds to the largest value in the array that
is strictly less than the value.stayInBounds
- If true, then 0 will be returned in the case that the value is smaller than
the smallest value in the array. If false then -1 will be returned.public static int binarySearchCeil(long[] a, long value, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, so the array must be sorted.
a
- The array to search.value
- The value being searched for.inclusive
- If the value is present in the array, whether to return the corresponding
index. If false then the returned index corresponds to the largest value in the array that
is strictly less than the value.stayInBounds
- If true, then (a.length - 1)
will be returned in the case that the
value is greater than the largest value in the array. If false then a.length
will
be returned.public static <T> int binarySearchFloor(java.util.List<? extends java.lang.Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, so the list must be sorted.
T
- The type of values being searched.list
- The list to search.value
- The value being searched for.inclusive
- If the value is present in the list, whether to return the corresponding
index. If false then the returned index corresponds to the largest value in the list that
is strictly less than the value.stayInBounds
- If true, then 0 will be returned in the case that the value is smaller than
the smallest value in the list. If false then -1 will be returned.public static <T> int binarySearchCeil(java.util.List<? extends java.lang.Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, so the list must be sorted.
T
- The type of values being searched.list
- The list to search.value
- The value being searched for.inclusive
- If the value is present in the list, whether to return the corresponding
index. If false then the returned index corresponds to the smallest value in the list that
is strictly greater than the value.stayInBounds
- If true, then (list.size() - 1)
will be returned in the case that
the value is greater than the largest value in the list. If false then list.size()
will be returned.public static long parseXsDuration(java.lang.String value)
value
- The attribute value to decode.public static long parseXsDateTime(java.lang.String value) throws java.text.ParseException
value
- The attribute value to decode.java.text.ParseException
public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor)
Logically, scaling consists of a multiplication followed by a division. The actual operations performed are designed to minimize the probability of overflow.
timestamp
- The timestamp to scale.multiplier
- The multiplier.divisor
- The divisor.public static long[] scaleLargeTimestamps(java.util.List<java.lang.Long> timestamps, long multiplier, long divisor)
scaleLargeTimestamp(long, long, long)
to a list of unscaled timestamps.timestamps
- The timestamps to scale.multiplier
- The multiplier.divisor
- The divisor.public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor)
scaleLargeTimestamp(long, long, long)
to an array of unscaled timestamps.timestamps
- The timestamps to scale.multiplier
- The multiplier.divisor
- The divisor.public static int[] toArray(java.util.List<java.lang.Integer> list)
list
- A list of integers.public static DataSpec getRemainderDataSpec(DataSpec dataSpec, int bytesLoaded)
public static int getIntegerCodeForString(java.lang.String string)
string
as bytes. The string must be no more than four characters long.string
- A string no more than four characters long.public static byte[] getBytesFromHexString(java.lang.String hexString)
hexString
- The hex string to convert to bytes.public static java.lang.String getHexString(byte[] bytes)
bytes
- The byte array.public static java.lang.String getCommaDelimitedSimpleClassNames(java.lang.Object[] objects)
objects
- The objects whose simple class names should be comma delimited and returned.public static java.lang.String getUserAgent(android.content.Context context, java.lang.String applicationName)
context
- A valid context of the calling application.applicationName
- String that will be prefix'ed to the generated user agent.public static int getPcmEncoding(int bitDepth)
bitDepth
- The bit depth. Supported values are 8, 16, 24 and 32.C.ENCODING_PCM_8BIT
,
C.ENCODING_PCM_16BIT
, C.ENCODING_PCM_24BIT
and
C.ENCODING_PCM_32BIT
. If the bit depth is unsupported then
C.ENCODING_INVALID
is returned.public static int inferContentType(java.lang.String fileName)
fileName
- Name of the file. It can include the path of the file.public static int getDefaultBufferSize(int trackType)
trackType
- The track type.public static java.lang.String escapeFileName(java.lang.String fileName)
For simplicity, this only handles common characters known to be illegal on FAT32:
<, >, :, ", /, \, |, ?, and *. % is also escaped since it is used as the escape
character. Escaping is performed in a consistent way so that no collisions occur and
unescapeFileName(String)
can be used to retrieve the original file name.
fileName
- File name to be escaped.public static java.lang.String unescapeFileName(java.lang.String fileName)
See escapeFileName(String)
for more information.
fileName
- File name to be unescaped.public static int crc(byte[] bytes, int start, int end, int initialValue)
bytes
- Array containing the bytes to update the crc value with.start
- The index to the first byte in the byte range to update the crc with.end
- The index after the last byte in the byte range to update the crc with.initialValue
- The initial value for the crc calculation.public static java.lang.String sha1(java.lang.String input)
input
as a hex string.input
- The string whose SHA-1 digest is required.public static android.graphics.Point getPhysicalDisplaySize(android.content.Context context)
context
- Any context.public static android.graphics.Point getPhysicalDisplaySize(android.content.Context context, android.view.Display display)
context
- Any context.display
- The display whose size is to be returned.