public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static String |
DEVICE
Like
Build.DEVICE , but in a place where it can be conveniently overridden for local
testing. |
static String |
MANUFACTURER
Like
Build.MANUFACTURER , but in a place where it can be conveniently overridden for
local testing. |
static 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. |
static int |
TYPE_DASH
Value returned by
inferContentType(String) for DASH manifests. |
static int |
TYPE_HLS
Value returned by
inferContentType(String) for HLS manifests. |
static int |
TYPE_OTHER
Value returned by
inferContentType(String) for files other than DASH, HLS or Smooth
Streaming manifests. |
static int |
TYPE_SS
Value returned by
inferContentType(String) for Smooth Streaming manifests. |
Modifier and Type | Method and Description |
---|---|
static boolean |
areEqual(Object o1,
Object o2)
Tests two objects for
Object.equals(Object) equality, handling the case where one or
both may be null. |
static <T> int |
binarySearchCeil(List<? extends Comparable<? super T>> list,
T key,
boolean inclusive,
boolean stayInBounds)
Returns the index of the smallest value in an list that is greater than (or optionally equal
to) a specified key.
|
static int |
binarySearchCeil(long[] a,
long key,
boolean inclusive,
boolean stayInBounds)
Returns the index of the smallest value in an array that is greater than (or optionally equal
to) a specified key.
|
static <T> int |
binarySearchFloor(List<? extends Comparable<? super T>> list,
T key,
boolean inclusive,
boolean stayInBounds)
Returns the index of the largest value in an list that is less than (or optionally equal to)
a specified key.
|
static int |
binarySearchFloor(long[] a,
long key,
boolean inclusive,
boolean stayInBounds)
Returns the index of the largest value in an array that is less than (or optionally equal to)
a specified key.
|
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(OutputStream outputStream)
Closes an
OutputStream , suppressing any IOException that may occur. |
static boolean |
contains(Object[] items,
Object item)
|
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 String |
escapeFileName(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[] |
executePost(String url,
byte[] data,
Map<String,String> requestProperties)
Executes a post request using
HttpURLConnection . |
static int[] |
firstIntegersArray(int length)
Creates an integer array containing the integers from 0 to
length - 1 . |
static int |
getBottomInt(long value)
Returns the bottom 32 bits of a long as an integer.
|
static byte[] |
getBytesFromHexString(String hexString)
Returns a byte array containing values parsed from the hex string provided.
|
static <T> String |
getCommaDelimitedSimpleClassNames(T[] objects)
Returns a string with comma delimited simple names of each object's class.
|
static String |
getHexStringFromBytes(byte[] data,
int beginIndex,
int endIndex)
Returns a hex string representation of the data provided.
|
static int |
getIntegerCodeForString(String string)
Returns the integer equal to the big-endian concatenation of the characters in
string
as bytes. |
static long |
getLong(int topInteger,
int bottomInteger)
Returns a long created by concatenating the bits of two integers.
|
static int |
getPcmEncoding(int bitDepth)
Converts a sample bit depth to a corresponding PCM encoding constant.
|
static Point |
getPhysicalDisplaySize(Context context)
Gets the physical size of the default display, in pixels.
|
static DataSpec |
getRemainderDataSpec(DataSpec dataSpec,
int bytesLoaded)
|
static int |
getTopInt(long value)
Returns the top 32 bits of a long as an integer.
|
static String |
getUserAgent(Context context,
String applicationName)
Returns a user agent string based on the given application name and the library version.
|
static int |
inferContentType(String fileName)
Makes a best guess to infer the type from a file name.
|
static boolean |
isAndroidTv(Context context)
Returns whether the device is an AndroidTV.
|
static boolean |
isLocalFileUri(Uri uri)
Returns true if the URI is a path to a local file or a reference to a local file.
|
static void |
maybeTerminateInputStream(HttpURLConnection connection,
long bytesRemaining)
On platform API levels 19 and 20, okhttp's implementation of
InputStream.close() can
block for a long time if the stream has a lot of data remaining. |
static ExecutorService |
newSingleThreadExecutor(String threadName)
Instantiates a new single threaded executor whose thread has the specified name.
|
static ScheduledExecutorService |
newSingleThreadScheduledExecutor(String threadName)
Instantiates a new single threaded scheduled executor whose thread has the specified name.
|
static long |
parseXsDateTime(String value)
Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since
the epoch.
|
static long |
parseXsDuration(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(List<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 int[] |
toArray(List<Integer> list)
Converts a list of integers to a primitive array.
|
static byte[] |
toByteArray(InputStream inputStream)
Converts the entirety of an
InputStream to a byte array. |
static String |
toLowerInvariant(String text)
Converts text to lower case using
Locale.US . |
static String |
unescapeFileName(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 String DEVICE
Build.DEVICE
, but in a place where it can be conveniently overridden for local
testing.public static final String MANUFACTURER
Build.MANUFACTURER
, but in a place where it can be conveniently overridden for
local testing.public static final String MODEL
Build.MODEL
, but in a place where it can be conveniently overridden for local
testing.public static final int TYPE_DASH
inferContentType(String)
for DASH manifests.public static final int TYPE_SS
inferContentType(String)
for Smooth Streaming manifests.public static final int TYPE_HLS
inferContentType(String)
for HLS manifests.public static final int TYPE_OTHER
inferContentType(String)
for files other than DASH, HLS or Smooth
Streaming manifests.public static boolean isAndroidTv(Context context)
context
- A context.public static byte[] toByteArray(InputStream inputStream) throws IOException
InputStream
to a byte array.inputStream
- the InputStream
to be read. The input stream is not closed by this
method.IOException
- if an error occurs reading from the stream.public static boolean isLocalFileUri(Uri uri)
uri
- The uri to test.public static boolean areEqual(Object o1, 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(Object[] items, 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 ExecutorService newSingleThreadExecutor(String threadName)
threadName
- The name of the thread.public static ScheduledExecutorService newSingleThreadScheduledExecutor(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(OutputStream outputStream)
OutputStream
, suppressing any IOException
that may occur.outputStream
- The OutputStream
to close.public static String toLowerInvariant(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 binarySearchFloor(long[] a, long key, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, and so the array must be sorted.
a
- The array to search.key
- The key being searched for.inclusive
- If the key 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 key.stayInBounds
- If true, then 0 will be returned in the case that the key is smaller than
the smallest value in the array. If false then -1 will be returned.public static int binarySearchCeil(long[] a, long key, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, and so the array must be sorted.
a
- The array to search.key
- The key being searched for.inclusive
- If the key is present in the array, whether to return the corresponding index.
If false then the returned index corresponds to the smallest value in the array that is
strictly greater than the key.stayInBounds
- If true, then (a.length - 1)
will be returned in the case that the
key is greater than the largest value in the array. If false then a.length
will be
returned.public static <T> int binarySearchFloor(List<? extends Comparable<? super T>> list, T key, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, and so the list must be sorted.
list
- The list to search.key
- The key being searched for.inclusive
- If the key 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 key.stayInBounds
- If true, then 0 will be returned in the case that the key is smaller than
the smallest value in the list. If false then -1 will be returned.public static <T> int binarySearchCeil(List<? extends Comparable<? super T>> list, T key, boolean inclusive, boolean stayInBounds)
The search is performed using a binary search algorithm, and so the list must be sorted.
list
- The list to search.key
- The key being searched for.inclusive
- If the key 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 key.stayInBounds
- If true, then (list.size() - 1)
will be returned in the case that
the key is greater than the largest value in the list. If false then list.size()
will be returned.public static int[] firstIntegersArray(int length)
length - 1
.length
- The length of the array.public static long parseXsDuration(String value)
value
- The attribute value to parse.public static long parseXsDateTime(String value) throws ParseException
value
- The attribute value to parse.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(List<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(List<Integer> list)
list
- A list of integers.public static void maybeTerminateInputStream(HttpURLConnection connection, long bytesRemaining)
InputStream.close()
can
block for a long time if the stream has a lot of data remaining. Call this method before
closing the input stream to make a best effort to cause the input stream to encounter an
unexpected end of input, working around this issue. On other platform API levels, the method
does nothing.connection
- The connection whose InputStream
should be terminated.bytesRemaining
- The number of bytes remaining to be read from the input stream if its
length is known. C.LENGTH_UNBOUNDED
otherwise.public static DataSpec getRemainderDataSpec(DataSpec dataSpec, int bytesLoaded)
public static int getIntegerCodeForString(String string)
string
as bytes. string
must contain four or fewer characters.public static int getTopInt(long value)
public static int getBottomInt(long value)
public static long getLong(int topInteger, int bottomInteger)
public static String getHexStringFromBytes(byte[] data, int beginIndex, int endIndex)
data
- The byte array containing the data to be turned into a hex string.beginIndex
- The begin index, inclusive.endIndex
- The end index, exclusive.public static byte[] getBytesFromHexString(String hexString)
hexString
- The hex string to convert to bytes.public static <T> String getCommaDelimitedSimpleClassNames(T[] objects)
objects
- The objects whose simple class names should be comma delimited and returned.public static String getUserAgent(Context context, String applicationName)
context
- A valid context of the calling application.applicationName
- String that will be prefix'ed to the generated user agent.public static byte[] executePost(String url, byte[] data, Map<String,String> requestProperties) throws IOException
HttpURLConnection
.url
- The request URL.data
- The request body, or null.requestProperties
- Request properties, or null.IOException
- If an error occurred making the request.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(String fileName)
fileName
- Name of the file. It can include the path of the file.TYPE_DASH
, TYPE_SS
, TYPE_HLS
or TYPE_OTHER
.public static String escapeFileName(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 String unescapeFileName(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.