/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.exoplayer.util; import com.google.android.exoplayer.upstream.DataSource; import android.net.Uri; import java.io.IOException; import java.net.URL; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; /** * Miscellaneous utility functions. */ public final class Util { /** * Like {@link android.os.Build.VERSION#SDK_INT}, but in a place where it can be conveniently * overridden for local testing. */ public static final int SDK_INT = android.os.Build.VERSION.SDK_INT; private Util() {} /** * Returns true if the URL points to a file on the local device * * @param url The URL to test */ public static boolean isUrlLocalFile(URL url) { return url.getProtocol().equals("file"); } /** * Tests two objects for {@link Object#equals(Object)} equality, handling the case where one or * both may be null. * * @param o1 The first object. * @param o2 The second object. * @return {@code o1 == null ? o2 == null : o1.equals(o2)}. */ public static boolean areEqual(Object o1, Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Instantiates a new single threaded executor whose thread has the specified name. * * @param threadName The name of the thread. * @return The executor. */ public static ExecutorService newSingleThreadExecutor(final String threadName) { return Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, threadName); } }); } /** * Instantiates a new single threaded scheduled executor whose thread has the specified name. * * @param threadName The name of the thread. * @return The executor. */ public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String threadName) { return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, threadName); } }); } /** * Closes a {@link DataSource}, suppressing any {@link IOException} that may occur. * * @param dataSource The {@link DataSource} to close. */ public static void closeQuietly(DataSource dataSource) { try { dataSource.close(); } catch (IOException e) { // Ignore. } } /** * Converts text to lower case using {@link Locale#US}. * * @param text The text to convert. * @return The lower case text, or null if {@code text} is null. */ public static String toLowerInvariant(String text) { return text == null ? null : text.toLowerCase(Locale.US); } /** * Like {@link Uri#parse(String)}, but discards the part of the uri that follows the final * forward slash. * * @param uriString An RFC 2396-compliant, encoded uri. * @return The parsed base uri. */ public static Uri parseBaseUri(String uriString) { return Uri.parse(uriString.substring(0, uriString.lastIndexOf('/'))); } /** * Returns the index of the largest value in an array that is less than (or optionally equal to) * a specified key. * <p> * The search is performed using a binary search algorithm, and so the array must be sorted. * * @param a The array to search. * @param key The key being searched for. * @param 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. * @param 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 binarySearchFloor(long[] a, long key, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(a, key); index = index < 0 ? -(index + 2) : (inclusive ? index : (index - 1)); return stayInBounds ? Math.max(0, index) : index; } /** * Returns the index of the smallest value in an array that is greater than (or optionally equal * to) a specified key. * <p> * The search is performed using a binary search algorithm, and so the array must be sorted. * * @param a The array to search. * @param key The key being searched for. * @param 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. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * key is greater than the largest value in the array. If false then {@code a.length} will be * returned. */ public static int binarySearchCeil(long[] a, long key, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(a, key); index = index < 0 ? ~index : (inclusive ? index : (index + 1)); return stayInBounds ? Math.min(a.length - 1, index) : index; } /** * Returns the index of the largest value in an list that is less than (or optionally equal to) * a specified key. * <p> * The search is performed using a binary search algorithm, and so the list must be sorted. * * @param list The list to search. * @param key The key being searched for. * @param 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. * @param 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 binarySearchFloor(List<? extends Comparable<? super T>> list, T key, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, key); index = index < 0 ? -(index + 2) : (inclusive ? index : (index - 1)); return stayInBounds ? Math.max(0, index) : index; } /** * Returns the index of the smallest value in an list that is greater than (or optionally equal * to) a specified key. * <p> * The search is performed using a binary search algorithm, and so the list must be sorted. * * @param list The list to search. * @param key The key being searched for. * @param 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. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the key is greater than the largest value in the list. If false then {@code list.size()} * will be returned. */ public static<T> int binarySearchCeil(List<? extends Comparable<? super T>> list, T key, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, key); index = index < 0 ? ~index : (inclusive ? index : (index + 1)); return stayInBounds ? Math.min(list.size() - 1, index) : index; } }