X
stringlengths
236
264k
y
stringlengths
5
74
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String [MASK] = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = [MASK] + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = [MASK] + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
prefix
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl. [MASK] ; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @ [MASK] public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @ [MASK] public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @ [MASK] public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @ [MASK] public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @ [MASK] public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @ [MASK] public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @ [MASK] public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @ [MASK] public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
GenerateUncached
/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2024 DBeaver Corp and others * * 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 org. [MASK] .dbeaver.ext.postgresql.model; import org. [MASK] .code.NotNull; import org. [MASK] .code.Nullable; import org. [MASK] .dbeaver.DBException; import org. [MASK] .dbeaver.model.DBPEvaluationContext; import org. [MASK] .dbeaver.model.exec.DBCFeatureNotSupportedException; import org. [MASK] .dbeaver.model.meta.Property; import org. [MASK] .dbeaver.model.runtime.DBRProgressMonitor; import org. [MASK] .dbeaver.model.struct.DBSEntityAssociation; import org. [MASK] .dbeaver.model.struct.DBSEntityConstraint; import org. [MASK] .dbeaver.model.struct.DBSEntityConstraintType; import java.util.List; /** * PostgreTableInheritance */ public class PostgreTableInheritance extends PostgreTableConstraintBase<PostgreTableConstraintColumn> implements DBSEntityAssociation { private final PostgreTableBase superTable; private int sequenceNum; public PostgreTableInheritance( @NotNull PostgreTableBase table, @NotNull PostgreTableBase superTable, int sequenceNum, boolean persisted) { super(table, table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "->" + superTable.getFullyQualifiedName(DBPEvaluationContext.DDL), DBSEntityConstraintType.INHERITANCE); this.setPersisted(persisted); this.superTable = superTable; this.sequenceNum = sequenceNum; } @Override public boolean isInherited() { // Inheritance itself can't be inherited return false; } @Nullable @Override public DBSEntityConstraint getReferencedConstraint() { return this; } @Nullable @Override @Property(viewable = true) public PostgreTableBase getAssociatedEntity() { return this.superTable; } @Property(viewable = true) public int getSequenceNum() { return sequenceNum; } @Nullable @Override public List<PostgreTableConstraintColumn> getAttributeReferences(DBRProgressMonitor monitor) throws DBException { return null; } @Override public void setAttributeReferences(List<PostgreTableConstraintColumn> postgreTableConstraintColumns) throws DBException { throw new DBCFeatureNotSupportedException(); } @Override void cacheAttributes(DBRProgressMonitor monitor, List<? extends PostgreTableConstraintColumn> children, boolean secondPass) { } }
jkiss
package com.baeldung.connectionstatus; import org. [MASK] .jupiter.api.Test; import java.sql.Connection; import static org. [MASK] .jupiter.api.Assertions.assertFalse; import static org. [MASK] .jupiter.api.Assertions.assertNotNull; import static org. [MASK] .jupiter.api.Assertions.assertTrue; class ConnectionValidationUnitTest { @Test void givenConnectionObject_whenCreated_thenIsNotClosed() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertNotNull(connection); assertFalse(connection.isClosed()); } @Test void givenConnectionObject_whenCreated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(connection.isValid(0)); } @Test void givenConnectionObject_whenValidated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(ConnectionValidation.isConnectionValid(connection)); } }
junit
package com.baeldung.recordswithjpa.repository; import com.baeldung.recordswithjpa.entity.Book; import com.baeldung.recordswithjpa.records.BookRecord; import org. [MASK] .data.jpa.repository.Query; import org. [MASK] .data.repository.CrudRepository; import org. [MASK] .data.repository.query.Param; import java.util.List; public interface BookRepository extends CrudRepository<Book, Long> { List<BookRecord> findBookByAuthor(String author); @Query("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " + "FROM Book b WHERE b.id = :id") BookRecord findBookById(@Param("id") Long id); }
springframework
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject. [MASK] ; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInInt(value)) { try { return [MASK] .asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInByte(value)) { try { return [MASK] .asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInShort(value)) { try { return [MASK] .asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInLong(value)) { try { return [MASK] .asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInFloat(value)) { try { return [MASK] .asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && [MASK] .fitsInDouble(value)) { try { return [MASK] .asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
EspressoInterop
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point [MASK] ) { Display.Mode mode = display.getMode(); [MASK] .x = mode.getPhysicalWidth(); [MASK] .y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point [MASK] ) { display.getRealSize( [MASK] ); } private static void getDisplaySizeV16(Display display, Point [MASK] ) { display.getSize( [MASK] ); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
outSize
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util.Collections; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main(String[] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet options = parser.parse(args); * PropertySource&lt;?&gt; ps = new JOptCommandLinePropertySource(options); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet options) { super(options); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource(String name, OptionSet options) { super(name, options); } @Override protected boolean containsOption(String name) { return this.source.has(name); } @Override public String[] getPropertyNames() { List<String> names = new ArrayList<>(); for (OptionSpec<?> spec : this.source.specs()) { // Last option is expected to be the most descriptive. String lastOption = CollectionUtils.lastElement(spec.options()); if (lastOption != null) { names.add(lastOption); } } return StringUtils.toStringArray(names); } @Override public @Nullable List<String> getOptionValues(String name) { List<?> argValues = this.source.valuesOf(name); List<String> [MASK] = new ArrayList<>(); for (Object argValue : argValues) { [MASK] .add(argValue.toString()); } if ( [MASK] .isEmpty()) { return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList( [MASK] ); } @Override protected List<String> getNonOptionArgs() { List<?> argValues = this.source.nonOptionArguments(); List<String> [MASK] = new ArrayList<>(); for (Object argValue : argValues) { [MASK] .add(argValue.toString()); } return ( [MASK] .isEmpty() ? Collections.emptyList() : Collections.unmodifiableList( [MASK] )); } }
stringArgValues
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ package org.elasticsearch.repositories; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; import java.io.IOException; import java.util.Objects; /** * The details of a successful shard-level snapshot that are used to build the overall snapshot during finalization. */ public class ShardSnapshotResult implements Writeable { private final ShardGeneration [MASK] ; private final ByteSizeValue size; private final int segmentCount; /** * @param [MASK] the shard [MASK] UUID, which uniquely identifies the specific snapshot of the shard * @param size the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when * restored * @param segmentCount the number of segments in this shard snapshot */ public ShardSnapshotResult(ShardGeneration [MASK] , ByteSizeValue size, int segmentCount) { this. [MASK] = Objects.requireNonNull( [MASK] ); this.size = Objects.requireNonNull(size); assert segmentCount >= 0; this.segmentCount = segmentCount; } public ShardSnapshotResult(StreamInput in) throws IOException { [MASK] = new ShardGeneration(in); size = ByteSizeValue.readFrom(in); segmentCount = in.readVInt(); } /** * @return the shard [MASK] UUID, which uniquely identifies the specific snapshot of the shard */ public ShardGeneration getGeneration() { return [MASK] ; } /** * @return the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when restored */ public ByteSizeValue getSize() { return size; } /** * @return the number of segments in this shard snapshot */ public int getSegmentCount() { return segmentCount; } @Override public void writeTo(StreamOutput out) throws IOException { [MASK] .writeTo(out); size.writeTo(out); out.writeVInt(segmentCount); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardSnapshotResult that = (ShardSnapshotResult) o; return segmentCount == that.segmentCount && [MASK] .equals(that. [MASK] ) && size.equals(that.size); } @Override public int hashCode() { return Objects.hash( [MASK] , size, segmentCount); } @Override public String toString() { return "ShardSnapshotResult{" + " [MASK] ='" + [MASK] + '\'' + ", size=" + size + ", segmentCount=" + segmentCount + '}'; } }
generation
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> [MASK] (r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); [MASK] (Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); [MASK] (Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
assertTrue
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler [MASK] = new ArgumentHandler(argv); log = new Log(out, [MASK] ); Binder binder = new Binder( [MASK] , log); Debugee debugee = binder.bindToDebugee(debugeeName + ( [MASK] .verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
argHandler
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives. [MASK] (); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
transferToInterpreterAndInvalidate
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String [MASK] = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve( [MASK] ), target.resolve( [MASK] )); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
THIS_CLASS
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api. [MASK] ; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { [MASK] .transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
CompilerDirectives
package com.baeldung.recordswithjpa. [MASK] ; import com.baeldung.recordswithjpa.entity.Book; import com.baeldung.recordswithjpa.records.BookRecord; import org.springframework.data.jpa. [MASK] .Query; import org.springframework.data. [MASK] .CrudRepository; import org.springframework.data. [MASK] .query.Param; import java.util.List; public interface BookRepository extends CrudRepository<Book, Long> { List<BookRecord> findBookByAuthor(String author); @Query("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " + "FROM Book b WHERE b.id = :id") BookRecord findBookById(@Param("id") Long id); }
repository
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param [MASK] The [MASK] to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler [MASK] , Runnable runnable) { Looper looper = [MASK] .getLooper(); if (!looper.getThread().isAlive()) { return false; } if ( [MASK] .getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return [MASK] .post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param [MASK] The [MASK] to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler [MASK] , Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( [MASK] , () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
handler
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip. [MASK] ; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); create [MASK] (zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); create [MASK] (zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void create [MASK] (Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try ( [MASK] zf = new [MASK] (zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
ZipFile
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long [MASK] ; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { [MASK] = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - [MASK] ; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
runningTestTime
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int [MASK] = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = [MASK] - 1; entries < [MASK] + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = [MASK] - 1; entries < [MASK] + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= [MASK] || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
ZIP64_ENTRIES
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param [MASK] Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path [MASK] , Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", [MASK] ); try (FileSystem zipfs = FileSystems.newFileSystem( [MASK] , env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param [MASK] Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path [MASK] , Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", [MASK] ); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem( [MASK] , env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param [MASK] ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File [MASK] ) throws Exception { try (RandomAccessFile raf = new RandomAccessFile( [MASK] , "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
zipFile
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long [MASK] ; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { [MASK] = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - [MASK] ; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
startTestRunTime
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util.Collections; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link [MASK] } * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main(String[] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet options = parser.parse(args); * PropertySource&lt;?&gt; ps = new [MASK] (options); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class [MASK] extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code [MASK] } having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public [MASK] (OptionSet options) { super(options); } /** * Create a new {@code [MASK] } having the given name * and backed by the given {@code OptionSet}. */ public [MASK] (String name, OptionSet options) { super(name, options); } @Override protected boolean containsOption(String name) { return this.source.has(name); } @Override public String[] getPropertyNames() { List<String> names = new ArrayList<>(); for (OptionSpec<?> spec : this.source.specs()) { // Last option is expected to be the most descriptive. String lastOption = CollectionUtils.lastElement(spec.options()); if (lastOption != null) { names.add(lastOption); } } return StringUtils.toStringArray(names); } @Override public @Nullable List<String> getOptionValues(String name) { List<?> argValues = this.source.valuesOf(name); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } if (stringArgValues.isEmpty()) { return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List<String> getNonOptionArgs() { List<?> argValues = this.source.nonOptionArguments(); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } return (stringArgValues.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(stringArgValues)); } }
JOptCommandLinePropertySource
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop. [MASK] ; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch ( [MASK] e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
UnsupportedMessageException
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util.Collections; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util. [MASK] Utils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code [MASK] []} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main( [MASK] [] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet options = parser.parse(args); * PropertySource&lt;?&gt; ps = new JOptCommandLinePropertySource(options); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet options) { super(options); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource( [MASK] name, OptionSet options) { super(name, options); } @Override protected boolean containsOption( [MASK] name) { return this.source.has(name); } @Override public [MASK] [] getPropertyNames() { List< [MASK] > names = new ArrayList<>(); for (OptionSpec<?> spec : this.source.specs()) { // Last option is expected to be the most descriptive. [MASK] lastOption = CollectionUtils.lastElement(spec.options()); if (lastOption != null) { names.add(lastOption); } } return [MASK] Utils.to [MASK] Array(names); } @Override public @Nullable List< [MASK] > getOptionValues( [MASK] name) { List<?> argValues = this.source.valuesOf(name); List< [MASK] > stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.to [MASK] ()); } if (stringArgValues.isEmpty()) { return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List< [MASK] > getNonOptionArgs() { List<?> argValues = this.source.nonOptionArguments(); List< [MASK] > stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.to [MASK] ()); } return (stringArgValues.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(stringArgValues)); } }
String
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code [MASK] } whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String [MASK] , @C.TrackType int trackType) { String[] codecArray = splitCodecs( [MASK] ); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code [MASK] } without the [MASK] whose track type doesn't match {@code * trackType}. * * @param [MASK] A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code [MASK] } without the [MASK] whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code [MASK] } is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String [MASK] , @C.TrackType int trackType) { String[] codecArray = splitCodecs( [MASK] ); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a [MASK] sequence string, as defined in RFC 6381, into individual codec strings. * * @param [MASK] A codec sequence string, as defined in RFC 6381. * @return The split [MASK] , or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String [MASK] ) { if (TextUtils.isEmpty( [MASK] )) { return new String[0]; } return split( [MASK] .trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video [MASK] should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
codecs
package com.baeldung. [MASK] status; import org.junit.jupiter.api.Test; import java.sql.Connection; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; class ConnectionValidationUnitTest { @Test void givenConnectionObject_whenCreated_thenIsNotClosed() throws Exception { Connection [MASK] = ConnectionValidation.getConnection(); assertNotNull( [MASK] ); assertFalse( [MASK] .isClosed()); } @Test void givenConnectionObject_whenCreated_thenIsValid() throws Exception { Connection [MASK] = ConnectionValidation.getConnection(); assertTrue( [MASK] .isValid(0)); } @Test void givenConnectionObject_whenValidated_thenIsValid() throws Exception { Connection [MASK] = ConnectionValidation.getConnection(); assertTrue(ConnectionValidation.isConnectionValid( [MASK] )); } }
connection
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles. [MASK] ; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached [MASK] exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
BranchProfile
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional in [MASK] ion or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String. [MASK] ; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", [MASK] Map(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", [MASK] Map(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 [MASK] extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a [MASK] ted String of the key:value entries for * a Map * * @param env Map to [MASK] * @return Formatted string of the Map entries */ private static String [MASK] Map(Map<String, String> env) { return env.entrySet().stream() .map(e -> [MASK] ("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( [MASK] ("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, [MASK] ("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
format
package com.baeldung.connectionstatus; import org.junit.jupiter.api.Test; import java.sql.Connection; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; class ConnectionValidationUnitTest { @Test void givenConnectionObject_whenCreated_thenIsNotClosed() throws Exception { Connection connection = ConnectionValidation. [MASK] (); assertNotNull(connection); assertFalse(connection.isClosed()); } @Test void givenConnectionObject_whenCreated_thenIsValid() throws Exception { Connection connection = ConnectionValidation. [MASK] (); assertTrue(connection.isValid(0)); } @Test void givenConnectionObject_whenValidated_thenIsValid() throws Exception { Connection connection = ConnectionValidation. [MASK] (); assertTrue(ConnectionValidation.isConnectionValid(connection)); } }
getConnection
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count [MASK] (entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); [MASK] (method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); [MASK] (entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); [MASK] (requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { [MASK] (ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
assertEquals
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be [MASK] 'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String [MASK] = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", [MASK] , hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", [MASK] , minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String [MASK] ) throws IOException { File tempFile = createTempFile(context, [MASK] ); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String [MASK] ) throws IOException { return File.createTempFile( [MASK] , null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
prefix
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test( [MASK] = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test( [MASK] = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test( [MASK] = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
dataProvider
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ package org. [MASK] .repositories; import org. [MASK] .common.io.stream.StreamInput; import org. [MASK] .common.io.stream.StreamOutput; import org. [MASK] .common.io.stream.Writeable; import org. [MASK] .common.unit.ByteSizeValue; import java.io.IOException; import java.util.Objects; /** * The details of a successful shard-level snapshot that are used to build the overall snapshot during finalization. */ public class ShardSnapshotResult implements Writeable { private final ShardGeneration generation; private final ByteSizeValue size; private final int segmentCount; /** * @param generation the shard generation UUID, which uniquely identifies the specific snapshot of the shard * @param size the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when * restored * @param segmentCount the number of segments in this shard snapshot */ public ShardSnapshotResult(ShardGeneration generation, ByteSizeValue size, int segmentCount) { this.generation = Objects.requireNonNull(generation); this.size = Objects.requireNonNull(size); assert segmentCount >= 0; this.segmentCount = segmentCount; } public ShardSnapshotResult(StreamInput in) throws IOException { generation = new ShardGeneration(in); size = ByteSizeValue.readFrom(in); segmentCount = in.readVInt(); } /** * @return the shard generation UUID, which uniquely identifies the specific snapshot of the shard */ public ShardGeneration getGeneration() { return generation; } /** * @return the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when restored */ public ByteSizeValue getSize() { return size; } /** * @return the number of segments in this shard snapshot */ public int getSegmentCount() { return segmentCount; } @Override public void writeTo(StreamOutput out) throws IOException { generation.writeTo(out); size.writeTo(out); out.writeVInt(segmentCount); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardSnapshotResult that = (ShardSnapshotResult) o; return segmentCount == that.segmentCount && generation.equals(that.generation) && size.equals(that.size); } @Override public int hashCode() { return Objects.hash(generation, size, segmentCount); } @Override public String toString() { return "ShardSnapshotResult{" + "generation='" + generation + '\'' + ", size=" + size + ", segmentCount=" + segmentCount + '}'; } }
elasticsearch
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util.Collections; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main(String[] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet options = parser.parse(args); * PropertySource&lt;?&gt; ps = new JOptCommandLinePropertySource(options); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet options) { super(options); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource(String name, OptionSet options) { super(name, options); } @Override protected boolean containsOption(String name) { return this. [MASK] .has(name); } @Override public String[] getPropertyNames() { List<String> names = new ArrayList<>(); for (OptionSpec<?> spec : this. [MASK] .specs()) { // Last option is expected to be the most descriptive. String lastOption = CollectionUtils.lastElement(spec.options()); if (lastOption != null) { names.add(lastOption); } } return StringUtils.toStringArray(names); } @Override public @Nullable List<String> getOptionValues(String name) { List<?> argValues = this. [MASK] .valuesOf(name); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } if (stringArgValues.isEmpty()) { return (this. [MASK] .has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List<String> getNonOptionArgs() { List<?> argValues = this. [MASK] .nonOptionArguments(); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } return (stringArgValues.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(stringArgValues)); } }
source
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 org.apache. [MASK] .runtime.jobmaster; import org.apache. [MASK] .api.common.JobID; import org.apache. [MASK] .api.common.JobStatus; import org.apache. [MASK] .runtime.messages.Acknowledge; import org.apache. [MASK] .runtime.messages.webmonitor.JobDetails; import org.apache. [MASK] .runtime.scheduler.ExecutionGraphInfo; import org.apache. [MASK] .util.AutoCloseableAsync; import java.time.Duration; import java.util.concurrent.CompletableFuture; /** Interface for a runner which executes a {@link JobMaster}. */ public interface JobManagerRunner extends AutoCloseableAsync { /** * Start the execution of the {@link JobMaster}. * * @throws Exception if the JobMaster cannot be started */ void start() throws Exception; /** * Get the {@link JobMasterGateway} of the {@link JobMaster}. The future is only completed if * the JobMaster becomes leader. * * @return Future with the JobMasterGateway once the underlying JobMaster becomes leader */ CompletableFuture<JobMasterGateway> getJobMasterGateway(); /** * Get the result future of this runner. The future is completed once the executed job reaches a * globally terminal state or if the initialization of the {@link JobMaster} fails. If the * result future is completed exceptionally via {@link JobNotFinishedException}, then this * signals that the job has not been completed successfully. All other exceptional completions * denote an unexpected exception which leads to a process restart. * * @return Future which is completed with the job result */ CompletableFuture<JobManagerRunnerResult> getResultFuture(); /** * Get the job id of the executed job. * * @return job id of the executed job */ JobID getJobID(); /** * Cancels the currently executed job. * * @param timeout of this operation * @return Future acknowledge of the operation */ CompletableFuture<Acknowledge> cancel(Duration timeout); /** * Requests the current job status. * * @param timeout for the rpc call * @return Future containing the current job status */ CompletableFuture<JobStatus> requestJobStatus(Duration timeout); /** * Request the details of the executed job. * * @param timeout for the rpc call * @return Future details of the executed job */ CompletableFuture<JobDetails> requestJobDetails(Duration timeout); /** * Requests the {@link ExecutionGraphInfo} of the executed job. * * @param timeout for the rpc call * @return Future which is completed with the {@link ExecutionGraphInfo} of the executed job */ CompletableFuture<ExecutionGraphInfo> requestJob(Duration timeout); /** * Flag indicating if the JobManagerRunner has been initialized. * * @return true if the JobManagerRunner has been initialized. */ boolean isInitialized(); }
flink
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media [MASK] }, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media [MASK] }s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media [MASK] based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media [MASK] }. * @return Whether it may be possible to load the URIs of the given media [MASK] . */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code [MASK] } array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code [MASK] } contains null. * * @param [MASK] The array of [MASK] to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] [MASK] , @Nullable Object item) { for (Object arrayItem : [MASK] ) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy [MASK] from. * @param array The array to copy [MASK] to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param [MASK] The list of which to move elements. * @param fromIndex The index at which the [MASK] to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> [MASK] , int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst( [MASK] .remove(fromIndex + i)); } [MASK] .addAll(min(newFromIndex, [MASK] .size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
items
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private [MASK] [][] zipfsMap() { return new [MASK] [][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private [MASK] [][] zip64Map() { return new [MASK] [][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
Object
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System. [MASK] ("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System. [MASK] ("java.vendor"); String jdkVersion = System. [MASK] ("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System. [MASK] ("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
getProperty
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String [MASK] = uri.getScheme(); return TextUtils.isEmpty( [MASK] ) || "file".equals( [MASK] ); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String [MASK] = uri.getScheme(); if ( [MASK] != null && Ascii.equalsIgnoreCase("rtsp", [MASK] )) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
scheme
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.File [MASK] ; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get( [MASK] .getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = [MASK] .nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = [MASK] .nanoTime(); long duration = endTestRunTime - startTestRunTime; [MASK] .out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = [MASK] .nanoTime(); [MASK] .out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = [MASK] .nanoTime(); long duration = endRunningTestTime - runningTestTime; [MASK] .out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { [MASK] .out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { [MASK] .out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File [MASK] using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { [MASK] .out.printf("Creating file = %s%n", zipFile); try (File [MASK] zipfs = File [MASK] s.newFile [MASK] (zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File [MASK] using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { [MASK] .out.printf("Creating file = %s%n", zipFile); String jdkVendor = [MASK] .getProperty("java.vendor"); String jdkVersion = [MASK] .getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + [MASK] .lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + [MASK] .lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (File [MASK] zipfs = File [MASK] s.newFile [MASK] (zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with File [MASK] API try (File [MASK] fs = File [MASK] s.newFile [MASK] (zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; [MASK] .out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFile [MASK] ().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = [MASK] .getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { [MASK] .out.print("Main"); } } }
System
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util. [MASK] ; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue( [MASK] .equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue( [MASK] .equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
Arrays
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary( [MASK] = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
limit
package com.baeldung.connectionstatus; import org.junit. [MASK] .api.Test; import java.sql.Connection; import static org.junit. [MASK] .api.Assertions.assertFalse; import static org.junit. [MASK] .api.Assertions.assertNotNull; import static org.junit. [MASK] .api.Assertions.assertTrue; class ConnectionValidationUnitTest { @Test void givenConnectionObject_whenCreated_thenIsNotClosed() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertNotNull(connection); assertFalse(connection.isClosed()); } @Test void givenConnectionObject_whenCreated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(connection.isValid(0)); } @Test void givenConnectionObject_whenValidated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(ConnectionValidation.isConnectionValid(connection)); } }
jupiter
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = [MASK] (HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = [MASK] (HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = [MASK] (HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path [MASK] (Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
generatePath
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code [MASK] }, returning the ceiled result. * * @param numerator The numerator to divide. * @param [MASK] The [MASK] to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int [MASK] ) { return (numerator + [MASK] - 1) / [MASK] ; } /** * Divides a {@code numerator} by a {@code [MASK] }, returning the ceiled result. * * @param numerator The numerator to divide. * @param [MASK] The [MASK] to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long [MASK] ) { return (numerator + [MASK] - 1) / [MASK] ; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
denominator
package com.baeldung. [MASK] .repository; import com.baeldung. [MASK] .entity.Book; import com.baeldung. [MASK] .records.BookRecord; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import java.util.List; public interface BookRepository extends CrudRepository<Book, Long> { List<BookRecord> findBookByAuthor(String author); @Query("SELECT new com.baeldung. [MASK] .records.BookRecord(b.id, b.title, b.author, b.isbn) " + "FROM Book b WHERE b.id = :id") BookRecord findBookById(@Param("id") Long id); }
recordswithjpa
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long [MASK] TestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { [MASK] TestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - [MASK] TestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param [MASK] Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int [MASK] ) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = [MASK] ; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = [MASK] ; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n [MASK] ing offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb. [MASK] (); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * [MASK] ed by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
start
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util.Collections; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main(String[] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet [MASK] = parser.parse(args); * PropertySource&lt;?&gt; ps = new JOptCommandLinePropertySource( [MASK] ); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet [MASK] ) { super( [MASK] ); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource(String name, OptionSet [MASK] ) { super(name, [MASK] ); } @Override protected boolean containsOption(String name) { return this.source.has(name); } @Override public String[] getPropertyNames() { List<String> names = new ArrayList<>(); for (OptionSpec<?> spec : this.source.specs()) { // Last option is expected to be the most descriptive. String lastOption = CollectionUtils.lastElement(spec. [MASK] ()); if (lastOption != null) { names.add(lastOption); } } return StringUtils.toStringArray(names); } @Override public @Nullable List<String> getOptionValues(String name) { List<?> argValues = this.source.valuesOf(name); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } if (stringArgValues.isEmpty()) { return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List<String> getNonOptionArgs() { List<?> argValues = this.source.nonOptionArguments(); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } return (stringArgValues.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(stringArgValues)); } }
options
/* * Copyright 2002-2023 the original author or authors. * * 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 * * https://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 org.springframework.core.env; import java.util.ArrayList; import java.util. [MASK] ; import java.util.List; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.jspecify.annotations.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * * <h2>Typical usage</h2> * * Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: * * <pre class="code"> * public static void main(String[] args) { * OptionParser parser = new OptionParser(); * parser.accepts("option1"); * parser.accepts("option2").withRequiredArg(); * OptionSet options = parser.parse(args); * PropertySource&lt;?&gt; ps = new JOptCommandLinePropertySource(options); * // ... * }</pre> * * <p>If an option has several representations, the most descriptive is expected * to be set last, and is used as the property name of the associated * {@link EnumerablePropertySource#getPropertyNames()}. * * <p>See {@link CommandLinePropertySource} for complete general usage examples. * * <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet * @deprecated since 6.1 with no plans for a replacement */ @Deprecated(since = "6.1") public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet options) { super(options); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource(String name, OptionSet options) { super(name, options); } @Override protected boolean containsOption(String name) { return this.source.has(name); } @Override public String[] getPropertyNames() { List<String> names = new ArrayList<>(); for (OptionSpec<?> spec : this.source.specs()) { // Last option is expected to be the most descriptive. String lastOption = CollectionUtils.lastElement(spec.options()); if (lastOption != null) { names.add(lastOption); } } return StringUtils.toStringArray(names); } @Override public @Nullable List<String> getOptionValues(String name) { List<?> argValues = this.source.valuesOf(name); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } if (stringArgValues.isEmpty()) { return (this.source.has(name) ? [MASK] .emptyList() : null); } return [MASK] .unmodifiableList(stringArgValues); } @Override protected List<String> getNonOptionArgs() { List<?> argValues = this.source.nonOptionArguments(); List<String> stringArgValues = new ArrayList<>(); for (Object argValue : argValues) { stringArgValues.add(argValue.toString()); } return (stringArgValues.isEmpty() ? [MASK] .emptyList() : [MASK] .unmodifiableList(stringArgValues)); } }
Collections
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ package org.elasticsearch.repositories; import org.elasticsearch. [MASK] .io.stream.StreamInput; import org.elasticsearch. [MASK] .io.stream.StreamOutput; import org.elasticsearch. [MASK] .io.stream.Writeable; import org.elasticsearch. [MASK] .unit.ByteSizeValue; import java.io.IOException; import java.util.Objects; /** * The details of a successful shard-level snapshot that are used to build the overall snapshot during finalization. */ public class ShardSnapshotResult implements Writeable { private final ShardGeneration generation; private final ByteSizeValue size; private final int segmentCount; /** * @param generation the shard generation UUID, which uniquely identifies the specific snapshot of the shard * @param size the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when * restored * @param segmentCount the number of segments in this shard snapshot */ public ShardSnapshotResult(ShardGeneration generation, ByteSizeValue size, int segmentCount) { this.generation = Objects.requireNonNull(generation); this.size = Objects.requireNonNull(size); assert segmentCount >= 0; this.segmentCount = segmentCount; } public ShardSnapshotResult(StreamInput in) throws IOException { generation = new ShardGeneration(in); size = ByteSizeValue.readFrom(in); segmentCount = in.readVInt(); } /** * @return the shard generation UUID, which uniquely identifies the specific snapshot of the shard */ public ShardGeneration getGeneration() { return generation; } /** * @return the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when restored */ public ByteSizeValue getSize() { return size; } /** * @return the number of segments in this shard snapshot */ public int getSegmentCount() { return segmentCount; } @Override public void writeTo(StreamOutput out) throws IOException { generation.writeTo(out); size.writeTo(out); out.writeVInt(segmentCount); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardSnapshotResult that = (ShardSnapshotResult) o; return segmentCount == that.segmentCount && generation.equals(that.generation) && size.equals(that.size); } @Override public int hashCode() { return Objects.hash(generation, size, segmentCount); } @Override public String toString() { return "ShardSnapshotResult{" + "generation='" + generation + '\'' + ", size=" + size + ", segmentCount=" + segmentCount + '}'; } }
common
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); [MASK] (zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); [MASK] (zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void [MASK] (Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
createZipFile
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset. [MASK] ; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes( [MASK] .UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), [MASK] .UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
StandardCharsets
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir. [MASK] ("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES. [MASK] (MAIN_CLASS), target. [MASK] (MAIN_CLASS)); Files.copy(TEST_CLASSES. [MASK] (THIS_CLASS), target. [MASK] (THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir. [MASK] (name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
resolve
package com.baeldung.connectionstatus; import org.junit.jupiter.api.Test; import java.sql.Connection; import static org.junit.jupiter.api. [MASK] .assertFalse; import static org.junit.jupiter.api. [MASK] .assertNotNull; import static org.junit.jupiter.api. [MASK] .assertTrue; class ConnectionValidationUnitTest { @Test void givenConnectionObject_whenCreated_thenIsNotClosed() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertNotNull(connection); assertFalse(connection.isClosed()); } @Test void givenConnectionObject_whenCreated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(connection.isValid(0)); } @Test void givenConnectionObject_whenValidated_thenIsValid() throws Exception { Connection connection = ConnectionValidation.getConnection(); assertTrue(ConnectionValidation.isConnectionValid(connection)); } }
Assertions
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk. [MASK] * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = " [MASK] Map", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = " [MASK] Map", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem [MASK] = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString( [MASK] .getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem [MASK] = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = [MASK] .getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory( [MASK] .getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = [MASK] .getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString( [MASK] .getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = " [MASK] Map") private Object[][] [MASK] Map() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
zipfs
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files. [MASK] (zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files. [MASK] (zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files. [MASK] (zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files. [MASK] (zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files. [MASK] (jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files. [MASK] (jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
deleteIfExists
/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2024 DBeaver Corp and others * * 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 org.jkiss.dbeaver.ext.postgresql.model; import org.jkiss.code. [MASK] ; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.model.DBPEvaluationContext; import org.jkiss.dbeaver.model.exec.DBCFeatureNotSupportedException; import org.jkiss.dbeaver.model.meta.Property; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.struct.DBSEntityAssociation; import org.jkiss.dbeaver.model.struct.DBSEntityConstraint; import org.jkiss.dbeaver.model.struct.DBSEntityConstraintType; import java.util.List; /** * PostgreTableInheritance */ public class PostgreTableInheritance extends PostgreTableConstraintBase<PostgreTableConstraintColumn> implements DBSEntityAssociation { private final PostgreTableBase superTable; private int sequenceNum; public PostgreTableInheritance( @ [MASK] PostgreTableBase table, @ [MASK] PostgreTableBase superTable, int sequenceNum, boolean persisted) { super(table, table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "->" + superTable.getFullyQualifiedName(DBPEvaluationContext.DDL), DBSEntityConstraintType.INHERITANCE); this.setPersisted(persisted); this.superTable = superTable; this.sequenceNum = sequenceNum; } @Override public boolean isInherited() { // Inheritance itself can't be inherited return false; } @Nullable @Override public DBSEntityConstraint getReferencedConstraint() { return this; } @Nullable @Override @Property(viewable = true) public PostgreTableBase getAssociatedEntity() { return this.superTable; } @Property(viewable = true) public int getSequenceNum() { return sequenceNum; } @Nullable @Override public List<PostgreTableConstraintColumn> getAttributeReferences(DBRProgressMonitor monitor) throws DBException { return null; } @Override public void setAttributeReferences(List<PostgreTableConstraintColumn> postgreTableConstraintColumns) throws DBException { throw new DBCFeatureNotSupportedException(); } @Override void cacheAttributes(DBRProgressMonitor monitor, List<? extends PostgreTableConstraintColumn> children, boolean secondPass) { } }
NotNull
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName(). [MASK] ()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName(). [MASK] ().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java"). [MASK] (); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = [MASK] (p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String [MASK] (InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
toString
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip. [MASK] ; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), [MASK] .DEFLATED}, {Map.of("create", "true", "noCompression", "true"), [MASK] .STORED}, {Map.of("create", "true", "noCompression", "false"), [MASK] .DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), [MASK] .DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), [MASK] .STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), [MASK] .DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), [MASK] .STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { [MASK] ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
ZipEntry
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double. [MASK] (years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double. [MASK] (months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double. [MASK] (days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double. [MASK] (hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double. [MASK] (minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double. [MASK] (seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double. [MASK] (value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
parseDouble
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int [MASK] = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if ( [MASK] == 95) { log.display("debuger> expected Debugee's exit " + " [MASK] - " + [MASK] ); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + " [MASK] (not 95) - " + [MASK] ); return 2; } } } }
status
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest( [MASK] = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest( [MASK] = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod( [MASK] = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod( [MASK] = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", [MASK] = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", [MASK] = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", [MASK] = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
enabled
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType [MASK] = debugee.classByName(classToCheckName); if ( [MASK] == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + [MASK] .allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = [MASK] .fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = [MASK] .getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
refType
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static [MASK] FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static [MASK] prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static [MASK] className = "getvaluesii004"; private final static [MASK] debugerName = prefix + className; private final static [MASK] debugeeName = debugerName + "a"; private final static [MASK] classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main( [MASK] argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run( [MASK] argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); [MASK] line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; [MASK] name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
String
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME. [MASK] + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME. [MASK] ; i++) { Field field; String name = FIELD_NAME[i][0]; Integer [MASK] OfArray = Integer.valueOf(FIELD_NAME[i][1]); int [MASK] = [MASK] OfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with [MASK] // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef. [MASK] () // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > [MASK] ) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with [MASK] " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with [MASK] " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with [MASK] " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
length
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException. [MASK] (new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException. [MASK] (new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
create
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www. [MASK] .com if you need additional information or have any * questions. */ package com. [MASK] .truffle.espresso.nodes.interop; import com. [MASK] .truffle.api.CompilerDirectives; import com. [MASK] .truffle.api.dsl.Cached; import com. [MASK] .truffle.api.dsl.Fallback; import com. [MASK] .truffle.api.dsl.GenerateUncached; import com. [MASK] .truffle.api.dsl.Specialization; import com. [MASK] .truffle.api.interop.InteropLibrary; import com. [MASK] .truffle.api.interop.UnsupportedMessageException; import com. [MASK] .truffle.api.interop.UnsupportedTypeException; import com. [MASK] .truffle.api.library.CachedLibrary; import com. [MASK] .truffle.api.nodes.NodeInfo; import com. [MASK] .truffle.api.profiles.BranchProfile; import com. [MASK] .truffle.espresso.meta.EspressoError; import com. [MASK] .truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com. [MASK] .truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
oracle
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ package org.elasticsearch.repositories; import org.elasticsearch.common.io. [MASK] .StreamInput; import org.elasticsearch.common.io. [MASK] .StreamOutput; import org.elasticsearch.common.io. [MASK] .Writeable; import org.elasticsearch.common.unit.ByteSizeValue; import java.io.IOException; import java.util.Objects; /** * The details of a successful shard-level snapshot that are used to build the overall snapshot during finalization. */ public class ShardSnapshotResult implements Writeable { private final ShardGeneration generation; private final ByteSizeValue size; private final int segmentCount; /** * @param generation the shard generation UUID, which uniquely identifies the specific snapshot of the shard * @param size the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when * restored * @param segmentCount the number of segments in this shard snapshot */ public ShardSnapshotResult(ShardGeneration generation, ByteSizeValue size, int segmentCount) { this.generation = Objects.requireNonNull(generation); this.size = Objects.requireNonNull(size); assert segmentCount >= 0; this.segmentCount = segmentCount; } public ShardSnapshotResult(StreamInput in) throws IOException { generation = new ShardGeneration(in); size = ByteSizeValue.readFrom(in); segmentCount = in.readVInt(); } /** * @return the shard generation UUID, which uniquely identifies the specific snapshot of the shard */ public ShardGeneration getGeneration() { return generation; } /** * @return the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when restored */ public ByteSizeValue getSize() { return size; } /** * @return the number of segments in this shard snapshot */ public int getSegmentCount() { return segmentCount; } @Override public void writeTo(StreamOutput out) throws IOException { generation.writeTo(out); size.writeTo(out); out.writeVInt(segmentCount); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardSnapshotResult that = (ShardSnapshotResult) o; return segmentCount == that.segmentCount && generation.equals(that.generation) && size.equals(that.size); } @Override public int hashCode() { return Objects.hash(generation, size, segmentCount); } @Override public String toString() { return "ShardSnapshotResult{" + "generation='" + generation + '\'' + ", size=" + size + ", segmentCount=" + segmentCount + '}'; } }
stream
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi. [MASK] .getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi. [MASK] .getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; [MASK] arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to [MASK] . All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = ( [MASK] )value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to [MASK] ."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
ArrayReference
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double [MASK] (Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
doForeign
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String [MASK] [][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + [MASK] .length + "\n"); // Check all array fields from debugee for (int i = 0; i < [MASK] .length; i++) { Field field; String name = [MASK] [i][0]; Integer lengthOfArray = Integer.valueOf( [MASK] [i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
FIELD_NAME
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes. [MASK] ; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @ [MASK] (shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @ [MASK] (shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @ [MASK] (shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @ [MASK] (shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @ [MASK] (shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @ [MASK] (shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @ [MASK] (shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @ [MASK] (shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @ [MASK] (shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
NodeInfo
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String [MASK] = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName( [MASK] ); if (refType == null) { log.complain("debuger FAILURE> Class " + [MASK] + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
classToCheckName
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util. [MASK] .Matcher; import java.util. [MASK] .Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split( [MASK] , -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param [MASK] A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String [MASK] ) { return value.split( [MASK] , /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code [MASK] }. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param [MASK] A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String [MASK] ) { return value.split( [MASK] , /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
regex
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile [MASK] ) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } [MASK] .enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
exceptionProfile
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are [MASK] (default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are [MASK] (default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are [MASK] (default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry. [MASK] }, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry. [MASK] } }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry. [MASK] }, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry. [MASK] }, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or [MASK] * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
DEFLATED
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws [MASK] If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws [MASK] { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws [MASK] If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws [MASK] { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch ( [MASK] | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws [MASK] If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws [MASK] { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
IOException
/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2024 DBeaver Corp and others * * 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 org.jkiss.dbeaver.ext.postgresql.model; import org.jkiss.code.NotNull; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.model.DBPEvaluationContext; import org.jkiss.dbeaver.model.exec.DBCFeatureNotSupportedException; import org.jkiss.dbeaver.model.meta.Property; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.struct.DBSEntityAssociation; import org.jkiss.dbeaver.model.struct.DBSEntityConstraint; import org.jkiss.dbeaver.model.struct.DBSEntityConstraintType; import java.util.List; /** * PostgreTableInheritance */ public class PostgreTableInheritance extends PostgreTableConstraintBase<PostgreTableConstraintColumn> implements DBSEntityAssociation { private final PostgreTableBase superTable; private int [MASK] ; public PostgreTableInheritance( @NotNull PostgreTableBase table, @NotNull PostgreTableBase superTable, int [MASK] , boolean persisted) { super(table, table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "->" + superTable.getFullyQualifiedName(DBPEvaluationContext.DDL), DBSEntityConstraintType.INHERITANCE); this.setPersisted(persisted); this.superTable = superTable; this. [MASK] = [MASK] ; } @Override public boolean isInherited() { // Inheritance itself can't be inherited return false; } @Nullable @Override public DBSEntityConstraint getReferencedConstraint() { return this; } @Nullable @Override @Property(viewable = true) public PostgreTableBase getAssociatedEntity() { return this.superTable; } @Property(viewable = true) public int getSequenceNum() { return [MASK] ; } @Nullable @Override public List<PostgreTableConstraintColumn> getAttributeReferences(DBRProgressMonitor monitor) throws DBException { return null; } @Override public void setAttributeReferences(List<PostgreTableConstraintColumn> postgreTableConstraintColumns) throws DBException { throw new DBCFeatureNotSupportedException(); } @Override void cacheAttributes(DBRProgressMonitor monitor, List<? extends PostgreTableConstraintColumn> children, boolean secondPass) { } }
sequenceNum
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch ( [MASK] e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch ( [MASK] e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch ( [MASK] e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
ObjectCollectedException
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle. [MASK] .nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle. [MASK] .meta.EspressoError; import com.oracle.truffle. [MASK] .runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle. [MASK] .runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
espresso
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io. [MASK] ; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws [MASK] if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws [MASK] { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link [MASK] } that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch ( [MASK] e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws [MASK] { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws [MASK] { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch ( [MASK] e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw [MASK] since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
IOException
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? [MASK] .parse [MASK] (years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? [MASK] .parse [MASK] (months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? [MASK] .parse [MASK] (days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? [MASK] .parse [MASK] (hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? [MASK] .parse [MASK] (minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? [MASK] .parse [MASK] (seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) ( [MASK] .parse [MASK] (value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
Double
/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2024 DBeaver Corp and others * * 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 org.jkiss. [MASK] .ext.postgresql.model; import org.jkiss.code.NotNull; import org.jkiss.code.Nullable; import org.jkiss. [MASK] .DBException; import org.jkiss. [MASK] .model.DBPEvaluationContext; import org.jkiss. [MASK] .model.exec.DBCFeatureNotSupportedException; import org.jkiss. [MASK] .model.meta.Property; import org.jkiss. [MASK] .model.runtime.DBRProgressMonitor; import org.jkiss. [MASK] .model.struct.DBSEntityAssociation; import org.jkiss. [MASK] .model.struct.DBSEntityConstraint; import org.jkiss. [MASK] .model.struct.DBSEntityConstraintType; import java.util.List; /** * PostgreTableInheritance */ public class PostgreTableInheritance extends PostgreTableConstraintBase<PostgreTableConstraintColumn> implements DBSEntityAssociation { private final PostgreTableBase superTable; private int sequenceNum; public PostgreTableInheritance( @NotNull PostgreTableBase table, @NotNull PostgreTableBase superTable, int sequenceNum, boolean persisted) { super(table, table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "->" + superTable.getFullyQualifiedName(DBPEvaluationContext.DDL), DBSEntityConstraintType.INHERITANCE); this.setPersisted(persisted); this.superTable = superTable; this.sequenceNum = sequenceNum; } @Override public boolean isInherited() { // Inheritance itself can't be inherited return false; } @Nullable @Override public DBSEntityConstraint getReferencedConstraint() { return this; } @Nullable @Override @Property(viewable = true) public PostgreTableBase getAssociatedEntity() { return this.superTable; } @Property(viewable = true) public int getSequenceNum() { return sequenceNum; } @Nullable @Override public List<PostgreTableConstraintColumn> getAttributeReferences(DBRProgressMonitor monitor) throws DBException { return null; } @Override public void setAttributeReferences(List<PostgreTableConstraintColumn> postgreTableConstraintColumns) throws DBException { throw new DBCFeatureNotSupportedException(); } @Override void cacheAttributes(DBRProgressMonitor monitor, List<? extends PostgreTableConstraintColumn> children, boolean secondPass) { } }
dbeaver
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean [MASK] (Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if ( [MASK] (activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean [MASK] ( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if ( [MASK] (activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if ( [MASK] (activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean [MASK] (Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
maybeRequestReadExternalStoragePermission
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop. [MASK] ; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") [MASK] interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
InteropLibrary
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex. [MASK] ; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch (Throwable e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable Throwable cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch (Throwable exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { [MASK] matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { [MASK] matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } [MASK] ism [MASK] = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ism [MASK] .matches()) { @Nullable String extensions = ism [MASK] .group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } [MASK] ism [MASK] = ISM_PATH_PATTERN.matcher(path); if (ism [MASK] .matches() && ism [MASK] .group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); [MASK] matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow(Throwable t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
Matcher
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == [MASK] ().java_lang_Boolean) { return (boolean) [MASK] ().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == [MASK] ().java_lang_Character) { return (char) [MASK] ().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
getMeta
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double [MASK] (StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
doEspresso
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean [MASK] (Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int [MASK] (Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte [MASK] (Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short [MASK] (Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char [MASK] (Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long [MASK] (Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float [MASK] (Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double [MASK] (Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
doHost
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] [MASK] = is.readAllBytes(); assertTrue(Arrays.equals( [MASK] , ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] [MASK] = Files.readAllBytes(file); assertTrue(Arrays.equals( [MASK] , ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
bytes
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle. [MASK] .espresso.nodes.interop; import com.oracle. [MASK] .api.CompilerDirectives; import com.oracle. [MASK] .api.dsl.Cached; import com.oracle. [MASK] .api.dsl.Fallback; import com.oracle. [MASK] .api.dsl.GenerateUncached; import com.oracle. [MASK] .api.dsl.Specialization; import com.oracle. [MASK] .api.interop.InteropLibrary; import com.oracle. [MASK] .api.interop.UnsupportedMessageException; import com.oracle. [MASK] .api.interop.UnsupportedTypeException; import com.oracle. [MASK] .api.library.CachedLibrary; import com.oracle. [MASK] .api.nodes.NodeInfo; import com.oracle. [MASK] .api.profiles.BranchProfile; import com.oracle. [MASK] .espresso.meta.EspressoError; import com.oracle. [MASK] .espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle. [MASK] .espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
truffle
package com. [MASK] .recordswithjpa.repository; import com. [MASK] .recordswithjpa.entity.Book; import com. [MASK] .recordswithjpa.records.BookRecord; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import java.util.List; public interface BookRepository extends CrudRepository<Book, Long> { List<BookRecord> findBookByAuthor(String author); @Query("SELECT new com. [MASK] .recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " + "FROM Book b WHERE b.id = :id") BookRecord findBookById(@Param("id") Long id); }
baeldung
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta. [MASK] ; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "boolean"); } static boolean isHostBoolean(Object value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "int"); } static boolean isHostInteger(Object value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "byte"); } static boolean isHostByte(Object value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "short"); } static boolean isHostShort(Object value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "char"); } static boolean isHostCharacter(Object value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "long"); } static boolean isHostLong(Object value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "float"); } static boolean isHostFloat(Object value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(StaticObject value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new Object[]{value}, [MASK] .cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStaticObject(value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign(Object value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw [MASK] .shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback StaticObject doUnsupportedType(Object value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new Object[]{value}, "double"); } static boolean isHostDouble(Object value) { return value instanceof Double; } } }
EspressoError
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ package org.elasticsearch.repositories; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit. [MASK] ; import java.io.IOException; import java.util.Objects; /** * The details of a successful shard-level snapshot that are used to build the overall snapshot during finalization. */ public class ShardSnapshotResult implements Writeable { private final ShardGeneration generation; private final [MASK] size; private final int segmentCount; /** * @param generation the shard generation UUID, which uniquely identifies the specific snapshot of the shard * @param size the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when * restored * @param segmentCount the number of segments in this shard snapshot */ public ShardSnapshotResult(ShardGeneration generation, [MASK] size, int segmentCount) { this.generation = Objects.requireNonNull(generation); this.size = Objects.requireNonNull(size); assert segmentCount >= 0; this.segmentCount = segmentCount; } public ShardSnapshotResult(StreamInput in) throws IOException { generation = new ShardGeneration(in); size = [MASK] .readFrom(in); segmentCount = in.readVInt(); } /** * @return the shard generation UUID, which uniquely identifies the specific snapshot of the shard */ public ShardGeneration getGeneration() { return generation; } /** * @return the total size of all the blobs that make up the shard snapshot, or equivalently, the size of the shard when restored */ public [MASK] getSize() { return size; } /** * @return the number of segments in this shard snapshot */ public int getSegmentCount() { return segmentCount; } @Override public void writeTo(StreamOutput out) throws IOException { generation.writeTo(out); size.writeTo(out); out.writeVInt(segmentCount); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardSnapshotResult that = (ShardSnapshotResult) o; return segmentCount == that.segmentCount && generation.equals(that.generation) && size.equals(that.size); } @Override public int hashCode() { return Objects.hash(generation, size, segmentCount); } @Override public String toString() { return "ShardSnapshotResult{" + "generation='" + generation + '\'' + ", size=" + size + ", segmentCount=" + segmentCount + '}'; } }
ByteSizeValue
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] ZIP_FILE_ENTRY = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path [MASK] = generatePath(HERE, "test", ".zip"); Files.deleteIfExists( [MASK] ); createZipFile( [MASK] , env, entries); verify( [MASK] , compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists( [MASK] ); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path [MASK] = generatePath(HERE, "test", ".zip"); Files.deleteIfExists( [MASK] ); createZipFile( [MASK] , env, 1); verify( [MASK] , compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists( [MASK] ); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param [MASK] ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path [MASK] , int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile( [MASK] .toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem( [MASK] )) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, ZIP_FILE_ENTRY)); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64( [MASK] .toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
zipfile
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk.share.*; import nsk.share.jpda.*; import nsk.share.jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log. [MASK] ("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log. [MASK] ("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log. [MASK] ("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log. [MASK] ("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log. [MASK] ("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log. [MASK] ("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log. [MASK] ("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log. [MASK] ("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log. [MASK] ("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
display
/* * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.jdi.ArrayReference.getValues_ii; import nsk. [MASK] .*; import nsk. [MASK] .jpda.*; import nsk. [MASK] .jdi.*; import com.sun.jdi.*; import java.io.*; import java.util.*; public class getvaluesii004 { final static int MIN_LENGTH = -50; final static int MAX_LENGTH = 51; final static String FIELD_NAME[][] = { {"z1", "5"}, {"b1", "5"}, {"c1", "6"}, {"d1", "1"}, {"f1", "1"}, {"i1", "10"}, {"l1", "2"}, {"r1", "5"}, {"lF1", "1"}, {"lP1", "1"}, {"lU1", "2"}, {"lR1", "3"}, {"lT1", "4"}, {"lV1", "5"} }; private static Log log; private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii."; private final static String className = "getvaluesii004"; private final static String debugerName = prefix + className; private final static String debugeeName = debugerName + "a"; private final static String classToCheckName = prefix + "getvaluesii004aClassToCheck"; public static void main(String argv[]) { int result = run(argv,System.out); if (result != 0) { throw new RuntimeException("TEST FAILED with result " + result); } } public static int run(String argv[], PrintStream out) { ArgumentHandler argHandler = new ArgumentHandler(argv); log = new Log(out, argHandler); Binder binder = new Binder(argHandler, log); Debugee debugee = binder.bindToDebugee(debugeeName + (argHandler.verbose() ? " -verbose" : "")); IOPipe pipe = debugee.createIOPipe(); boolean testFailed = false; // Connect with debugee and resume it debugee.redirectStderr(out); debugee.resume(); String line = pipe.readln(); if (line == null) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null"); return 2; } if (!line.equals("ready")) { log.complain("debuger FAILURE> UNEXPECTED debugee's signal - " + line); return 2; } else { log.display("debuger> debugee's \"ready\" signal recieved."); } ReferenceType refType = debugee.classByName(classToCheckName); if (refType == null) { log.complain("debuger FAILURE> Class " + classToCheckName + " not found."); return 2; } log.display("debuger> Total fields in debugee read: " + refType.allFields().size() + " total fields in debuger: " + FIELD_NAME.length + "\n"); // Check all array fields from debugee for (int i = 0; i < FIELD_NAME.length; i++) { Field field; String name = FIELD_NAME[i][0]; Integer lengthOfArray = Integer.valueOf(FIELD_NAME[i][1]); int length = lengthOfArray.intValue(); Value value; ArrayReference arrayRef; // Get field from debuggee by name try { field = refType.fieldByName(name); } catch (ClassNotPreparedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 1> Can't get field by name " + name); log.complain("debuger FAILURE 1> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field " + field + " read."); // Get field's value try { value = refType.getValue(field); } catch (IllegalArgumentException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } catch (ObjectCollectedException e) { log.complain("debuger FAILURE 2> Cannot get value for field " + name); log.complain("debuger FAILURE 2> Exception: " + e); testFailed = true; continue; } log.display("debuger> " + i + " field value is " + value); // Cast to ArrayReference. All fields in debugee are // arrays, so ClassCastException should not be thrown try { arrayRef = (ArrayReference)value; } catch (ClassCastException e) { log.complain("debuger FAILURE 3> Cannot cast value for field " + name + " to ArrayReference."); log.complain("debuger FAILURE 3> Exception: " + e); testFailed = true; continue; } // Try to get values from the first element with length // from MIN_LENGTH to -2 (-1 is legal) and from arrayRef.length() // to MAX_LENGTH for (int j = MIN_LENGTH; j < MAX_LENGTH; j++) { if ( (j < -1) || (j > length) ) { List listOfValues; try { listOfValues = arrayRef.getValues(0, j); log.complain("debuger FAILURE 4> List of values of " + "field " + name + " with length " + j + " is " + listOfValues + ", but " + "IndexOutOfBoundsException expected."); testFailed = true; } catch (ObjectCollectedException e) { log.display("debuger FAILURE 5> Cannot get list of " + "values with length " + j + " from field " + name); log.display("debuger FAILURE 5> Exception: " + e); testFailed = true; } catch (IndexOutOfBoundsException e) { // Length is negative or too large, so // IndexOutOfBoundsException is expected log.display("debuger> " + i + " field: cannot get " + "list of components with length " + j + ". Expected exception: " + e); } } } log.display("debuger> " + i + " field checked.\n"); } pipe.println("quit"); debugee.waitFor(); int status = debugee.getStatus(); if (testFailed) { log.complain("debuger FAILURE> TEST FAILED"); return 2; } else { if (status == 95) { log.display("debuger> expected Debugee's exit " + "status - " + status); return 0; } else { log.complain("debuger FAILURE> UNEXPECTED Debugee's exit " + "status (not 95) - " + status); return 2; } } } }
share
/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ import org.testng.annotations.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.*; import java.security.SecureRandom; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import static java.lang.Boolean.TRUE; import static java.lang.String.format; import static java.util.stream.Collectors.joining; import static org.testng.Assert.*; /** * @test * @bug 8230870 * @summary Test ZIP Filesystem behavior with ~64k entries * @modules jdk.zipfs * @run testng LargeEntriesTest */ public class LargeEntriesTest { private static final Path HERE = Path.of("."); /** * Number of ZIP entries which results in the use of ZIP64 */ private static final int ZIP64_ENTRIES = 65535; /** * Classes and MANIFEST attribute used for invoking Main via java -jar */ private static final String MANIFEST_MAIN_CLASS = "LargeEntriesTest$Main"; private static final String MAIN_CLASS = "LargeEntriesTest$Main.class"; private static final String THIS_CLASS = "LargeEntriesTest.class"; /** * Number of entries included in the JAR file including META-INF, * MANIFEST.MF, and the classes associated with this test */ private static final int ADDITIONAL_JAR_ENTRIES = 4; /** * Value used for creating the required entries in a ZIP or JAR file */ private static final String ZIP_FILE_VALUE = "US Open 2019"; private static final byte[] [MASK] = ZIP_FILE_VALUE.getBytes(StandardCharsets.UTF_8); /** * Location of the classes to be added to the JAR file */ static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); private static final SecureRandom random = new SecureRandom(); /** * Fields used for timing runs */ private static int testNumberRunning; private static long runningTestTime; private static long startTestRunTime; private static final double NANOS_IN_SECOND = 1_000_000_000.0; @BeforeTest(enabled = false) public void beforeTest() { startTestRunTime = System.nanoTime(); } @AfterTest(enabled = false) public void afterTest() { long endTestRunTime = System.nanoTime(); long duration = endTestRunTime - startTestRunTime; System.out.printf("#### Completed test run, total running time: %.4f in seconds%n", duration / NANOS_IN_SECOND); } @BeforeMethod(enabled = false) public static void beforeMethod() { runningTestTime = System.nanoTime(); System.out.printf("**** Starting test number: %s%n", testNumberRunning); } @AfterMethod(enabled = false) public void afterMethod() { long endRunningTestTime = System.nanoTime(); long duration = endRunningTestTime - runningTestTime; System.out.printf("**** Completed test number: %s, Time: %.4f%n", testNumberRunning, duration / NANOS_IN_SECOND); testNumberRunning++; } /** * Validate that you can create a ZIP file with and without compression * and that the ZIP file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testZip(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, entries); verify(zipfile, compression, entries, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } } /** * Validate that when the forceZIP64End property is set to true, * that ZIP64 is used. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the ZIP file */ @Test(dataProvider = "zip64Map", enabled = true) public void testForceZIP64End(Map<String, String> env, int compression) throws Exception { System.out.printf("ZIP FS Map = %s, Compression mode= %s%n ", formatMap(env), compression); // Generate a ZIP file path Path zipfile = generatePath(HERE, "test", ".zip"); Files.deleteIfExists(zipfile); createZipFile(zipfile, env, 1); verify(zipfile, compression, 1, isTrue(env, "forceZIP64End"), 0); Files.deleteIfExists(zipfile); } /** * Validate that you can create a JAR file with and without compression * and that the JAR file is created using ZIP64 if there are 65535 or * more entries. * * @param env Properties used for creating the ZIP Filesystem * @param compression Indicates whether the files are DEFLATED(default) * or STORED * @throws Exception If an error occurs during the creation, verification or * deletion of the JAR file */ @Test(dataProvider = "zipfsMap", enabled = true) public void testJar(Map<String, String> env, int compression) throws Exception { for (int entries = ZIP64_ENTRIES - 1; entries < ZIP64_ENTRIES + 2; entries++) { Path jar = generatePath(HERE, "test", ".jar"); Files.deleteIfExists(jar); createJarFile(jar, env, entries); // Now run the Main-Class specified the Manifest runJar(jar.getFileName().toString()).assertSuccess() .validate(r -> assertTrue(r.output.matches("\\AMain\\Z"))); verify(jar, compression, entries, isTrue(env, "forceZIP64End"), ADDITIONAL_JAR_ENTRIES); Files.deleteIfExists(jar); } } /** * Create a ZIP File System using the specified properties and a ZIP file * with the specified number of entries * * @param zipFile Path to the ZIP File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the ZIP File * @throws IOException If an error occurs while creating the ZIP file */ private void createZipFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env)) { for (int i = 0; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /** * Create a ZIP File System using the specified properties and a JAR file * with the specified number of entries * * @param zipFile Path to the JAR File to create * @param env Properties used for creating the ZIP Filesystem * @param entries Number of entries to add to the JAR File * @throws IOException If an error occurs while creating the JAR file */ private void createJarFile(Path zipFile, Map<String, String> env, int entries) throws IOException { System.out.printf("Creating file = %s%n", zipFile); String jdkVendor = System.getProperty("java.vendor"); String jdkVersion = System.getProperty("java.version"); String manifest = "Manifest-Version: 1.0" + System.lineSeparator() + "Main-Class: " + MANIFEST_MAIN_CLASS + System.lineSeparator() + "Created-By: " + jdkVersion + " (" + jdkVendor + ")"; try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, env); InputStream in = new ByteArrayInputStream(manifest.getBytes())) { // Get ZIP FS path to META-INF/MANIFEST.MF Path metadir = zipfs.getPath("/", "META-INF"); Path manifestFile = metadir.resolve("MANIFEST.MF"); // Create META-INF directory if it does not already exist and // add the MANIFEST.MF file if (!Files.exists(metadir)) Files.createDirectory(zipfs.getPath("/", "META-INF")); Files.copy(in, manifestFile); // Add the needed test classes Path target = zipfs.getPath("/"); Files.copy(TEST_CLASSES.resolve(MAIN_CLASS), target.resolve(MAIN_CLASS)); Files.copy(TEST_CLASSES.resolve(THIS_CLASS), target.resolve(THIS_CLASS)); // Add the remaining entries that are required for (int i = ADDITIONAL_JAR_ENTRIES; i < entries; i++) { Files.writeString(zipfs.getPath("Entry-" + i), ZIP_FILE_VALUE); } } } /* * DataProvider used to validate that you can create a ZIP file with and * without compression. */ @DataProvider(name = "zipfsMap") private Object[][] zipfsMap() { return new Object[][]{ {Map.of("create", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false"), ZipEntry.DEFLATED} }; } /* * DataProvider used to validate that you can create a ZIP file with/without * ZIP64 format extensions */ @DataProvider(name = "zip64Map") private Object[][] zip64Map() { return new Object[][]{ {Map.of("create", "true", "forceZIP64End", "true"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "true"), ZipEntry.STORED}, {Map.of("create", "true", "noCompression", "false", "forceZIP64End", "false"), ZipEntry.DEFLATED}, {Map.of("create", "true", "noCompression", "true", "forceZIP64End", "false"), ZipEntry.STORED} }; } /** * Verify that the given path is a ZIP file containing the * expected entries. * * @param zipfile ZIP file to be validated * @param method Expected Compression method: STORED or DEFLATED * @param entries Number of expected entries * @param isZip64Forced true if ZIP64 use is being forced; false otherwise * @param start Starting number for verifying entries * @throws Exception If an error occurs while examining the ZIP file */ private static void verify(Path zipfile, int method, int entries, boolean isZip64Forced, int start) throws Exception { // check entries with ZIP API try (ZipFile zf = new ZipFile(zipfile.toFile())) { // check entry count assertEquals(entries, zf.size()); // check compression method and content of each entry for (int i = start; i < entries; i++) { ZipEntry ze = zf.getEntry("Entry-" + i); assertNotNull(ze); assertEquals(method, ze.getMethod()); try (InputStream is = zf.getInputStream(ze)) { byte[] bytes = is.readAllBytes(); assertTrue(Arrays.equals(bytes, [MASK] )); } } } // check entries with FileSystem API try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { // check entry count Path top = fs.getPath("/"); long count = Files.find(top, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() || (attrs.isDirectory() && path.getFileName() != null && path.getFileName().toString().equals("META-INF"))) .count(); assertEquals(entries, count); // check content of each entry for (int i = start; i < entries; i++) { Path file = fs.getPath("Entry-" + i); byte[] bytes = Files.readAllBytes(file); assertTrue(Arrays.equals(bytes, [MASK] )); } } // Check for a ZIP64 End of Central Directory Locator boolean foundZip64 = usesZip64(zipfile.toFile()); // Is ZIP64 required? boolean requireZip64 = entries >= ZIP64_ENTRIES || isZip64Forced; System.out.printf(" isZip64Forced = %s, foundZip64= %s, requireZip64= %s%n", isZip64Forced, foundZip64, requireZip64); assertEquals(requireZip64, foundZip64); } /** * Determine if the specified property name=true/"true" * * @param env ZIP Filesystem Map * @param name property to validate * @return true if the property value is set to true/"true"; false otherwise */ private static boolean isTrue(Map<String, ?> env, String name) { return "true".equals(env.get(name)) || TRUE.equals(env.get(name)); } /** * Check to see if the ZIP64 End of Central Directory Locator has been found * * @param b byte array to check for the locator in * @param n starting offset for the search * @return true if the Zip64 End of Central Directory Locator is found; false * otherwise */ private static boolean end64SigAt(byte[] b, int n) { return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == 6 & b[n + 3] == 6; } /** * Utility method that checks the ZIP file for the use of the ZIP64 * End of Central Directory Locator * * @param zipFile ZIP file to check * @return true if the ZIP64 End of Central Directory Locator is found; false * otherwise * * @throws Exception If an error occurs while traversing the file */ private static boolean usesZip64(File zipFile) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) { byte[] buf = new byte[4096]; long seeklen = raf.length() - buf.length; if (seeklen < 0) seeklen = 0; raf.seek(seeklen); raf.read(buf); for (int i = 0; i < buf.length - 4; i++) { // Is there a ZIP64 End of Central Directory Locator? if (end64SigAt(buf, i)) { return true; } } } return false; } /** * Generate a temporary file Path * * @param dir Directory used to create the path * @param prefix The prefix string used to create the path * @param suffix The suffix string used to create the path * @return Path that was generated */ private static Path generatePath(Path dir, String prefix, String suffix) { long n = random.nextLong(); String s = prefix + Long.toUnsignedString(n) + suffix; Path name = dir.getFileSystem().getPath(s); // the generated name should be a simple file name if (name.getParent() != null) throw new IllegalArgumentException("Invalid prefix or suffix"); return dir.resolve(name); } /** * Utility method to return a formatted String of the key:value entries for * a Map * * @param env Map to format * @return Formatted string of the Map entries */ private static String formatMap(Map<String, String> env) { return env.entrySet().stream() .map(e -> format("(%s:%s)", e.getKey(), e.getValue())) .collect(joining(", ")); } /** * Validates that a jar created using ZIP FS can be used by the java * tool to run a program specified in the Main-Class Manifest attribute * * @param jarFile Name of the JAR file to specify to the -jar option * @return A Result object representing the return code and output from the * program that was invoked */ private static Result runJar(String jarFile) { String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", jarFile}; String output; ProcessBuilder pb = new ProcessBuilder(cmd); Process p; try { p = pb.start(); output = toString(p.getInputStream(), p.getErrorStream()); p.waitFor(); } catch (IOException | InterruptedException e) { throw new RuntimeException( format("Error invoking: '%s', Exception= %s", pb.command(), e)); } return new Result(p.exitValue(), output); } /** * Utility method to combine the output and error streams for the Process * started by ProcessBuilder * * @param is Process Outputstream * @param is2 Process ErrorStream * @return String representing the combination of the OutputStream & ErrorStream * @throws IOException If an error occurs while combining the streams */ private static String toString(InputStream is, InputStream is2) throws IOException { try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); InputStream concatenated = new SequenceInputStream(is, is2)) { concatenated.transferTo(dst); return new String(dst.toByteArray(), StandardCharsets.UTF_8); } } /** * Wrapper class used to verify the results from a ProcessBuilder invocation */ private static class Result { final int ec; // Return code for command that was executed final String output; // Output from the command that was executed /** * Constructor * * @param ec Return code from the ProcessBuilder invocation * @param output ProcessBuilder output to be validated */ private Result(int ec, String output) { this.ec = ec; this.output = output; } /** * Validate that the command that was executed completed successfully * * @return This Result object */ Result assertSuccess() { assertEquals(ec, 0, format("Expected ec 0, received: %s, output [%s]", ec, output)); return this; } /** * Validate that the expected result is received * * @param r The operation to perform * @return This Result object */ Result validate(Consumer<Result> r) { r.accept(this); return this; } } /** * Trivial class used to validate that a JAR created using ZIP FS * can be successfully executed */ public static class Main { public static void main(String[] args) { System.out.print("Main"); } } }
ZIP_FILE_ENTRY
/* * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.espresso.nodes.interop; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.espresso.meta.EspressoError; import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; import com.oracle.truffle.espresso.runtime.staticobject.Static [MASK] ; /** * Handles conversions of (potentially) foreign objects to primitive types. */ @NodeInfo(shortName = "Convert value to primitive") public abstract class ToPrimitive extends ToEspressoNode { @NodeInfo(shortName = "To boolean") @GenerateUncached public abstract static class ToBoolean extends ToPrimitive { protected static final int LIMIT = 2; @Specialization boolean doHost(Boolean value) { return value; } @Specialization boolean doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && value.getKlass() == getMeta().java_lang_Boolean) { return (boolean) getMeta().java_lang_Boolean_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to boolean")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostBoolean(value)", "interop.isBoolean(value)" }) boolean doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asBoolean(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isBoolean returns true, asBoolean must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "boolean"); } static boolean isHostBoolean( [MASK] value) { return value instanceof Boolean; } } @NodeInfo(shortName = "To int") @GenerateUncached public abstract static class ToInt extends ToPrimitive { protected static final int LIMIT = 2; @Specialization int doHost(Integer value) { return value; } @Specialization int doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInInt(value)) { try { return EspressoInterop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to int")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostInteger(value)", "interop.fitsInInt(value)" }) int doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asInt(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "int"); } static boolean isHostInteger( [MASK] value) { return value instanceof Integer; } } @NodeInfo(shortName = "To byte") @GenerateUncached public abstract static class ToByte extends ToPrimitive { protected static final int LIMIT = 2; @Specialization byte doHost(Byte value) { return value; } @Specialization byte doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInByte(value)) { try { return EspressoInterop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to byte")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostByte(value)", "interop.fitsInByte(value)" }) byte doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asByte(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "byte"); } static boolean isHostByte( [MASK] value) { return value instanceof Byte; } } @NodeInfo(shortName = "To short") @GenerateUncached public abstract static class ToShort extends ToPrimitive { protected static final int LIMIT = 2; @Specialization short doHost(Short value) { return value; } @Specialization short doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInShort(value)) { try { return EspressoInterop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to short")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostShort(value)", "interop.fitsInShort(value)" }) short doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asShort(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "short"); } static boolean isHostShort( [MASK] value) { return value instanceof Short; } } @NodeInfo(shortName = "To char") @GenerateUncached public abstract static class ToChar extends ToPrimitive { protected static final int LIMIT = 2; @Specialization char doHost(Character value) { return value; } @Specialization char doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && value.getKlass() == getMeta().java_lang_Character) { return (char) getMeta().java_lang_Character_value.get(value); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to char")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostCharacter(value)", "interop.isString(value)" }) char doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { try { String s = interop.asString(value); if (s.length() == 1) { return s.charAt(0); } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", s, " to char")); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "char"); } static boolean isHostCharacter( [MASK] value) { return value instanceof Character; } } @NodeInfo(shortName = "To long") @GenerateUncached public abstract static class ToLong extends ToPrimitive { protected static final int LIMIT = 2; @Specialization long doHost(Long value) { return value; } @Specialization long doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInLong(value)) { try { return EspressoInterop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to long")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostLong(value)", "interop.fitsInLong(value)" }) long doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asLong(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "long"); } static boolean isHostLong( [MASK] value) { return value instanceof Long; } } @NodeInfo(shortName = "To float") @GenerateUncached public abstract static class ToFloat extends ToPrimitive { protected static final int LIMIT = 2; @Specialization float doHost(Float value) { return value; } @Specialization float doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInFloat(value)) { try { return EspressoInterop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to float")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostFloat(value)", "interop.fitsInFloat(value)" }) float doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asFloat(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "float"); } static boolean isHostFloat( [MASK] value) { return value instanceof Float; } } @NodeInfo(shortName = "To double") @GenerateUncached public abstract static class ToDouble extends ToPrimitive { protected static final int LIMIT = 2; @Specialization double doHost(Double value) { return value; } @Specialization double doEspresso(Static [MASK] value, @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { if (value != null && !Static [MASK] .isNull(value) && EspressoInterop.fitsInDouble(value)) { try { return EspressoInterop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } exceptionProfile.enter(); throw UnsupportedTypeException.create(new [MASK] []{value}, EspressoError.cat("Cannot cast ", value, " to double")); } @Specialization(guards = { "!isStatic [MASK] (value)", "!isHostDouble(value)", "interop.fitsInDouble(value)" }) double doForeign( [MASK] value, @CachedLibrary(limit = "LIMIT") InteropLibrary interop) { try { return interop.asDouble(value); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreterAndInvalidate(); throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); } } @Fallback Static [MASK] doUnsupportedType( [MASK] value) throws UnsupportedTypeException { throw UnsupportedTypeException.create(new [MASK] []{value}, "double"); } static boolean isHostDouble( [MASK] value) { return value instanceof Double; } } }
Object
/* * Copyright (C) 2016 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.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.Activity; import android.app.UiModeManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.content.res.Resources; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaDrm; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Parcel; import android.os.SystemClock; import android.provider.MediaStore; import android.security.NetworkSecurityPolicy; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.SparseLongArray; import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C.ContentType; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player.Commands; import com.google.common.base.Ascii; import com.google.common.base.Charsets; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Formatter; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import org.checkerframework.checker.initialization.qual.UnknownInitialization; import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.PolyNull; /** * Miscellaneous utility methods. * * @deprecated com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which * contains the same ExoPlayer code). See <a * href="https://developer.android.com/guide/topics/media/media3/getting-started/migration-guide">the * migration guide</a> for more details, including a script to help with the migration. */ @Deprecated public final class Util { /** * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently overridden for * local testing. */ public static final int SDK_INT = Build.VERSION.SDK_INT; /** * Like {@link Build#DEVICE}, but in a place where it can be conveniently overridden for local * testing. */ public static final String DEVICE = Build.DEVICE; /** * Like {@link Build#MANUFACTURER}, but in a place where it can be conveniently overridden for * local testing. */ public static final String MANUFACTURER = Build.MANUFACTURER; /** * Like {@link Build#MODEL}, but in a place where it can be conveniently overridden for local * testing. */ public static final String MODEL = Build.MODEL; /** A concise description of the device that it can be useful to log for debugging purposes. */ public static final String DEVICE_DEBUG_INFO = DEVICE + ", " + MODEL + ", " + MANUFACTURER + ", " + SDK_INT; /** An empty byte array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final String TAG = "Util"; private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile( "(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]" + "(\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?" + "([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?"); private static final Pattern XS_DURATION_PATTERN = Pattern.compile( "^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?" + "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); // https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-deliver-content-overview#URLs private static final Pattern ISM_PATH_PATTERN = Pattern.compile("(?:.*\\.)?isml?(?:/(manifest(.*))?)?", Pattern.CASE_INSENSITIVE); private static final String ISM_HLS_FORMAT_EXTENSION = "format=m3u8-aapl"; private static final String ISM_DASH_FORMAT_EXTENSION = "format=mpd-time-csf"; // Replacement map of ISO language codes used for normalization. @Nullable private static HashMap<String, String> languageTagReplacementMap; private Util() {} /** * Converts the entirety of an {@link InputStream} to a byte array. * * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this * method. * @return a byte array containing all of the inputStream's bytes. * @throws IOException if an error occurs reading from the stream. */ public static byte[] toByteArray(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024 * 4]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } return outputStream.toByteArray(); } /** Converts an integer into an equivalent byte array. */ public static byte[] toByteArray(int value) { return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; } /** * Converts an array of integers into an equivalent byte array. * * <p>Each integer is converted into 4 sequential bytes. */ public static byte[] toByteArray(int... values) { byte[] array = new byte[values.length * 4]; int index = 0; for (int value : values) { byte[] byteArray = toByteArray(value); array[index++] = byteArray[0]; array[index++] = byteArray[1]; array[index++] = byteArray[2]; array[index++] = byteArray[3]; } return array; } /** Converts a float into an equivalent byte array. */ public static byte[] toByteArray(float value) { return toByteArray(Float.floatToIntBits(value)); } /** Converts a byte array into a float. */ public static float toFloat(byte[] bytes) { checkArgument(bytes.length == 4); int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF); return Float.intBitsToFloat(intBits); } /** Converts a byte array into an integer. */ public static int toInteger(byte[] bytes) { checkArgument(bytes.length == 4); return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]; } /** * Registers a {@link BroadcastReceiver} that's not intended to receive broadcasts from other * apps. This will be enforced by specifying {@link Context#RECEIVER_NOT_EXPORTED} if {@link * #SDK_INT} is 33 or above. * * <p>Do not use this method if registering a receiver for a <a * href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/AndroidManifest.xml">protected * system broadcast</a>. * * @param context The context on which {@link Context#registerReceiver} will be called. * @param receiver The {@link BroadcastReceiver} to register. This value may be null. * @param filter Selects the Intent broadcasts to be received. * @return The first sticky intent found that matches {@code filter}, or null if there are none. */ @Nullable public static Intent registerReceiverNotExported( Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { if (SDK_INT < 33) { return context.registerReceiver(receiver, filter); } else { return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); } } /** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ @Nullable public static ComponentName startForegroundService(Context context, Intent intent) { if (SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission read the specified {@link Uri}s, requesting the permission if necessary. * * @param activity The host activity for checking and requesting the permission. * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) { if (SDK_INT < 23) { return false; } for (Uri uri : uris) { if (maybeRequestReadExternalStoragePermission(activity, uri)) { return true; } } return false; } /** * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE} * permission for the specified {@link MediaItem media items}, requesting the permission if * necessary. * * @param activity The host activity for checking and requesting the permission. * @param mediaItems {@link MediaItem Media items}s that may require {@link * permission#READ_EXTERNAL_STORAGE} to read. * @return Whether a permission request was made. */ public static boolean maybeRequestReadExternalStoragePermission( Activity activity, MediaItem... mediaItems) { if (SDK_INT < 23) { return false; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (maybeRequestReadExternalStoragePermission(activity, mediaItem.localConfiguration.uri)) { return true; } List<MediaItem.SubtitleConfiguration> subtitleConfigs = mediaItem.localConfiguration.subtitleConfigurations; for (int i = 0; i < subtitleConfigs.size(); i++) { if (maybeRequestReadExternalStoragePermission(activity, subtitleConfigs.get(i).uri)) { return true; } } } return false; } private static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri uri) { return SDK_INT >= 23 && (isLocalFileUri(uri) || isMediaStoreExternalContentUri(uri)) && requestExternalStoragePermission(activity); } private static boolean isMediaStoreExternalContentUri(Uri uri) { if (!"content".equals(uri.getScheme()) || !MediaStore.AUTHORITY.equals(uri.getAuthority())) { return false; } List<String> pathSegments = uri.getPathSegments(); if (pathSegments.isEmpty()) { return false; } String firstPathSegment = pathSegments.get(0); return MediaStore.VOLUME_EXTERNAL.equals(firstPathSegment) || MediaStore.VOLUME_EXTERNAL_PRIMARY.equals(firstPathSegment); } /** * Returns whether it may be possible to load the URIs of the given media items based on the * network security policy's cleartext traffic permissions. * * @param mediaItems A list of {@link MediaItem media items}. * @return Whether it may be possible to load the URIs of the given media items. */ public static boolean checkCleartextTrafficPermitted(MediaItem... mediaItems) { if (SDK_INT < 24) { // We assume cleartext traffic is permitted. return true; } for (MediaItem mediaItem : mediaItems) { if (mediaItem.localConfiguration == null) { continue; } if (isTrafficRestricted(mediaItem.localConfiguration.uri)) { return false; } for (int i = 0; i < mediaItem.localConfiguration.subtitleConfigurations.size(); i++) { if (isTrafficRestricted(mediaItem.localConfiguration.subtitleConfigurations.get(i).uri)) { return false; } } } return true; } /** * Returns true if the URI is a path to a local file or a reference to a local file. * * @param uri The uri to test. */ public static boolean isLocalFileUri(Uri uri) { String scheme = uri.getScheme(); return TextUtils.isEmpty(scheme) || "file".equals(scheme); } /** * 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(@Nullable Object o1, @Nullable Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Tests whether an {@code items} array contains an object equal to {@code item}, according to * {@link Object#equals(Object)}. * * <p>If {@code item} is null then true is returned if and only if {@code items} contains null. * * @param items The array of items to search. * @param item The item to search for. * @return True if the array contains an object equal to the item being searched for. */ public static boolean contains(@NullableType Object[] items, @Nullable Object item) { for (Object arrayItem : items) { if (areEqual(arrayItem, item)) { return true; } } return false; } /** * Removes an indexed range from a List. * * <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}. * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). * @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code * list.size()}, or {@code fromIndex} &gt; {@code toIndex}. */ public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IllegalArgumentException(); } else if (fromIndex != toIndex) { // Checking index inequality prevents an unnecessary allocation. list.subList(fromIndex, toIndex).clear(); } } /** * Casts a nullable variable to a non-null variable without runtime null check. * * <p>Use {@link Assertions#checkNotNull(Object)} to throw if the value is null. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T castNonNull(@Nullable T value) { return value; } /** Casts a nullable type array to a non-null type array without runtime null check. */ @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"}) @EnsuresNonNull("#1") public static <T> T[] castNonNullTypeArray(@NullableType T[] value) { return value; } /** * Copies and optionally truncates an array. Prevents null array elements created by {@link * Arrays#copyOf(Object[], int)} by ensuring the new length does not exceed the current length. * * @param input The input array. * @param length The output array length. Must be less or equal to the length of the input array. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopy(T[] input, int length) { checkArgument(length <= input.length); return Arrays.copyOf(input, length); } /** * Copies a subset of an array. * * @param input The input array. * @param from The start the range to be copied, inclusive * @param to The end of the range to be copied, exclusive. * @return The copied array. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static <T> T[] nullSafeArrayCopyOfRange(T[] input, int from, int to) { checkArgument(0 <= from); checkArgument(to <= input.length); return Arrays.copyOfRange(input, from, to); } /** * Creates a new array containing {@code original} with {@code newElement} appended. * * @param original The input array. * @param newElement The element to append. * @return The new array. */ public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) { @NullableType T[] result = Arrays.copyOf(original, original.length + 1); result[original.length] = newElement; return castNonNullTypeArray(result); } /** * Creates a new array containing the concatenation of two non-null type arrays. * * @param first The first array. * @param second The second array. * @return The concatenated result. */ @SuppressWarnings("nullness:assignment") public static <T> T[] nullSafeArrayConcatenation(T[] first, T[] second) { T[] concatenation = Arrays.copyOf(first, first.length + second.length); System.arraycopy( /* src= */ second, /* srcPos= */ 0, /* dest= */ concatenation, /* destPos= */ first.length, /* length= */ second.length); return concatenation; } /** * Copies the contents of {@code list} into {@code array}. * * <p>{@code list.size()} must be the same as {@code array.length} to ensure the contents can be * copied into {@code array} without leaving any nulls at the end. * * @param list The list to copy items from. * @param array The array to copy items to. */ @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") public static <T> void nullSafeListToArray(List<T> list, T[] array) { Assertions.checkState(list.size() == array.length); list.toArray(array); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper() { return createHandlerForCurrentLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. * @throws IllegalStateException If the current thread doesn't have a {@link Looper}. */ public static Handler createHandlerForCurrentLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(Assertions.checkStateNotNull(Looper.myLooper()), callback); } /** * Creates a {@link Handler} on the current {@link Looper} thread. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. */ public static Handler createHandlerForCurrentOrMainLooper() { return createHandlerForCurrentOrMainLooper(/* callback= */ null); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link * Looper} is used. * * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ public static Handler createHandlerForCurrentOrMainLooper( @Nullable Handler.@UnknownInitialization Callback callback) { return createHandler(getCurrentOrMainLooper(), callback); } /** * Creates a {@link Handler} with the specified {@link Handler.Callback} on the specified {@link * Looper} thread. * * <p>The method accepts partially initialized objects as callback under the assumption that the * Handler won't be used to send messages until the callback is fully initialized. * * @param looper A {@link Looper} to run the callback on. * @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no * callback is required. * @return A {@link Handler} with the specified callback on the current {@link Looper} thread. */ @SuppressWarnings({"nullness:argument", "nullness:return"}) public static Handler createHandler( Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) { return new Handler(looper, callback); } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or * run. {@code false} otherwise. */ public static boolean postOrRun(Handler handler, Runnable runnable) { Looper looper = handler.getLooper(); if (!looper.getThread().isAlive()) { return false; } if (handler.getLooper() == Looper.myLooper()) { runnable.run(); return true; } else { return handler.post(runnable); } } /** * Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link * Handler}. Otherwise, runs the {@link Runnable} directly. Also returns a {@link * ListenableFuture} for when the {@link Runnable} has run. * * @param handler The handler to which the {@link Runnable} will be posted. * @param runnable The runnable to either post or run. * @param successValue The value to set in the {@link ListenableFuture} once the runnable * completes. * @param <T> The type of {@code successValue}. * @return A {@link ListenableFuture} for when the {@link Runnable} has run. */ public static <T> ListenableFuture<T> postOrRunWithCompletion( Handler handler, Runnable runnable, T successValue) { SettableFuture<T> outputFuture = SettableFuture.create(); postOrRun( handler, () -> { try { if (outputFuture.isCancelled()) { return; } runnable.run(); outputFuture.set(successValue); } catch ( [MASK] e) { outputFuture.setException(e); } }); return outputFuture; } /** * Asynchronously transforms the result of a {@link ListenableFuture}. * * <p>The transformation function is called using a {@linkplain MoreExecutors#directExecutor() * direct executor}. * * <p>The returned Future attempts to keep its cancellation state in sync with that of the input * future and that of the future returned by the transform function. That is, if the returned * Future is cancelled, it will attempt to cancel the other two, and if either of the other two is * cancelled, the returned Future will also be cancelled. All forwarded cancellations will not * attempt to interrupt. * * @param future The input {@link ListenableFuture}. * @param transformFunction The function transforming the result of the input future. * @param <T> The result type of the input future. * @param <U> The result type of the transformation function. * @return A {@link ListenableFuture} for the transformed result. */ public static <T, U> ListenableFuture<T> transformFutureAsync( ListenableFuture<U> future, AsyncFunction<U, T> transformFunction) { // This is a simplified copy of Guava's Futures.transformAsync. SettableFuture<T> outputFuture = SettableFuture.create(); outputFuture.addListener( () -> { if (outputFuture.isCancelled()) { future.cancel(/* mayInterruptIfRunning= */ false); } }, MoreExecutors.directExecutor()); future.addListener( () -> { U inputFutureResult; try { inputFutureResult = Futures.getDone(future); } catch (CancellationException cancellationException) { outputFuture.cancel(/* mayInterruptIfRunning= */ false); return; } catch (ExecutionException exception) { @Nullable [MASK] cause = exception.getCause(); outputFuture.setException(cause == null ? exception : cause); return; } catch (RuntimeException | Error error) { outputFuture.setException(error); return; } try { outputFuture.setFuture(transformFunction.apply(inputFutureResult)); } catch ( [MASK] exception) { outputFuture.setException(exception); } }, MoreExecutors.directExecutor()); return outputFuture; } /** * Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the * application's main thread if the current thread doesn't have a {@link Looper}. */ public static Looper getCurrentOrMainLooper() { @Nullable Looper myLooper = Looper.myLooper(); return myLooper != null ? myLooper : Looper.getMainLooper(); } /** * 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(String threadName) { return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, 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(String threadName) { return Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, threadName)); } /** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(@Nullable Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } } /** * Reads an integer from a {@link Parcel} and interprets it as a boolean, with 0 mapping to false * and all other values mapping to true. * * @param parcel The {@link Parcel} to read from. * @return The read value. */ public static boolean readBoolean(Parcel parcel) { return parcel.readInt() != 0; } /** * Writes a boolean to a {@link Parcel}. The boolean is written as an integer with value 1 (true) * or 0 (false). * * @param parcel The {@link Parcel} to write to. * @param value The value to write. */ public static void writeBoolean(Parcel parcel, boolean value) { parcel.writeInt(value ? 1 : 0); } /** * Returns the language tag for a {@link Locale}. * * <p>For API levels &ge; 21, this tag is IETF BCP 47 compliant. Use {@link * #normalizeLanguageCode(String)} to retrieve a normalized IETF BCP 47 language tag for all API * levels if needed. * * @param locale A {@link Locale}. * @return The language tag. */ public static String getLocaleLanguageTag(Locale locale) { return SDK_INT >= 21 ? getLocaleLanguageTagV21(locale) : locale.toString(); } /** * Returns a normalized IETF BCP 47 language tag for {@code language}. * * @param language A case-insensitive language code supported by {@link * Locale#forLanguageTag(String)}. * @return The all-lowercase normalized code, or null if the input was null, or {@code * language.toLowerCase()} if the language could not be normalized. */ public static @PolyNull String normalizeLanguageCode(@PolyNull String language) { if (language == null) { return null; } // Locale data (especially for API < 21) may produce tags with '_' instead of the // standard-conformant '-'. String normalizedTag = language.replace('_', '-'); if (normalizedTag.isEmpty() || normalizedTag.equals(C.LANGUAGE_UNDETERMINED)) { // Tag isn't valid, keep using the original. normalizedTag = language; } normalizedTag = Ascii.toLowerCase(normalizedTag); String mainLanguage = splitAtFirst(normalizedTag, "-")[0]; if (languageTagReplacementMap == null) { languageTagReplacementMap = createIsoLanguageReplacementMap(); } @Nullable String replacedLanguage = languageTagReplacementMap.get(mainLanguage); if (replacedLanguage != null) { normalizedTag = replacedLanguage + normalizedTag.substring(/* beginIndex= */ mainLanguage.length()); mainLanguage = replacedLanguage; } if ("no".equals(mainLanguage) || "i".equals(mainLanguage) || "zh".equals(mainLanguage)) { normalizedTag = maybeReplaceLegacyLanguageTags(normalizedTag); } return normalizedTag; } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. * * @param bytes The UTF-8 encoded bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes) { return new String(bytes, Charsets.UTF_8); } /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes in a subarray. * * @param bytes The UTF-8 encoded bytes to decode. * @param offset The index of the first byte to decode. * @param length The number of bytes to decode. * @return The string. */ public static String fromUtf8Bytes(byte[] bytes, int offset, int length) { return new String(bytes, offset, length, Charsets.UTF_8); } /** * Returns a new byte array containing the code points of a {@link String} encoded using UTF-8. * * @param value The {@link String} whose bytes should be obtained. * @return The code points encoding using UTF-8. */ public static byte[] getUtf8Bytes(String value) { return value.getBytes(Charsets.UTF_8); } /** * Splits a string using {@code value.split(regex, -1}). Note: this is is similar to {@link * String#split(String)} but empty matches at the end of the string will not be omitted from the * returned array. * * @param value The string to split. * @param regex A delimiting regular expression. * @return The array of strings resulting from splitting the string. */ public static String[] split(String value, String regex) { return value.split(regex, /* limit= */ -1); } /** * Splits the string at the first occurrence of the delimiter {@code regex}. If the delimiter does * not match, returns an array with one element which is the input string. If the delimiter does * match, returns an array with the portion of the string before the delimiter and the rest of the * string. * * @param value The string. * @param regex A delimiting regular expression. * @return The string split by the first occurrence of the delimiter. */ public static String[] splitAtFirst(String value, String regex) { return value.split(regex, /* limit= */ 2); } /** * Returns whether the given character is a carriage return ('\r') or a line feed ('\n'). * * @param c The character. * @return Whether the given character is a linebreak. */ public static boolean isLinebreak(int c) { return c == '\n' || c == '\r'; } /** * Formats a string using {@link Locale#US}. * * @see String#format(String, Object...) */ public static String formatInvariant(String format, Object... args) { return String.format(Locale.US, format, args); } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static int ceilDivide(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; } /** * Divides a {@code numerator} by a {@code denominator}, returning the ceiled result. * * @param numerator The numerator to divide. * @param denominator The denominator to divide by. * @return The ceiled result of the division. */ public static long ceilDivide(long numerator, long denominator) { return (numerator + denominator - 1) / denominator; } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static int constrainValue(int value, int min, int max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static long constrainValue(long value, long min, long max) { return max(min, min(value, max)); } /** * Constrains a value to the specified bounds. * * @param value The value to constrain. * @param min The lower bound. * @param max The upper bound. * @return The constrained value {@code Math.max(min, Math.min(value, max))}. */ public static float constrainValue(float value, float min, float max) { return max(min, min(value, max)); } /** * Returns the sum of two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x + y} overflows. * @return {@code x + y}, or {@code overflowResult} if the result overflows. */ public static long addWithOverflowDefault(long x, long y, long overflowResult) { long result = x + y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ result) & (y ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the difference between two arguments, or a third argument if the result overflows. * * @param x The first value. * @param y The second value. * @param overflowResult The return value if {@code x - y} overflows. * @return {@code x - y}, or {@code overflowResult} if the result overflows. */ public static long subtractWithOverflowDefault(long x, long y, long overflowResult) { long result = x - y; // See Hacker's Delight 2-13 (H. Warren Jr). if (((x ^ y) & (x ^ result)) < 0) { return overflowResult; } return result; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the first occurrence of {@code value} in {@code array}, or {@link * C#INDEX_UNSET} if {@code value} is not contained in {@code array}. * * @param array The array to search. * @param value The value to search for. * @return The index of the first occurrence of value in {@code array}, or {@link C#INDEX_UNSET} * if {@code value} is not contained in {@code array}. */ public static int linearSearch(long[] array, long value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return C.INDEX_UNSET; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code array} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && array[index] == value) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code list} that is less than (or optionally equal * to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the first one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the list. If false then -1 will be returned. * @return The index of the largest element in {@code list} that is less than (or optionally equal * to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchFloor( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = -(index + 2); } else { while (--index >= 0 && list.get(index).compareTo(value) == 0) {} if (inclusive) { index++; } } return stayInBounds ? max(0, index) : index; } /** * Returns the index of the largest element in {@code longArray} that is less than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the first one will be returned. * * @param longArray The array to search. * @param value The value being searched for. * @param 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 element strictly less * than the value. * @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than * the smallest element in the array. If false then -1 will be returned. * @return The index of the largest element in {@code array} that is less than (or optionally * equal to) {@code value}. */ public static int binarySearchFloor( LongArray longArray, long value, boolean inclusive, boolean stayInBounds) { int lowIndex = 0; int highIndex = longArray.size() - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) >>> 1; if (longArray.get(midIndex) < value) { lowIndex = midIndex + 1; } else { highIndex = midIndex - 1; } } if (inclusive && highIndex + 1 < longArray.size() && longArray.get(highIndex + 1) == value) { highIndex++; } else if (stayInBounds && highIndex == -1) { highIndex = 0; } return highIndex; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( int[] array, int value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code array} that is greater than (or optionally * equal to) a specified {@code value}. * * <p>The search is performed using a binary search algorithm, so the array must be sorted. If the * array contains multiple elements equal to {@code value} and {@code inclusive} is true, the * index of the last one will be returned. * * @param array The array to search. * @param value The value being searched for. * @param inclusive If the value is present in the array, whether to return the corresponding * index. If false then the returned index corresponds to the smallest element strictly * greater than the value. * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the * value is greater than the largest element in the array. If false then {@code a.length} will * be returned. * @return The index of the smallest element in {@code array} that is greater than (or optionally * equal to) {@code value}. */ public static int binarySearchCeil( long[] array, long value, boolean inclusive, boolean stayInBounds) { int index = Arrays.binarySearch(array, value); if (index < 0) { index = ~index; } else { while (++index < array.length && array[index] == value) {} if (inclusive) { index--; } } return stayInBounds ? min(array.length - 1, index) : index; } /** * Returns the index of the smallest element in {@code list} that is greater than (or optionally * equal to) a specified value. * * <p>The search is performed using a binary search algorithm, so the list must be sorted. If the * list contains multiple elements equal to {@code value} and {@code inclusive} is true, the index * of the last one will be returned. * * @param <T> The type of values being searched. * @param list The list to search. * @param value The value being searched for. * @param 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 element strictly * greater than the value. * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that * the value is greater than the largest element in the list. If false then {@code * list.size()} will be returned. * @return The index of the smallest element in {@code list} that is greater than (or optionally * equal to) {@code value}. */ public static <T extends Comparable<? super T>> int binarySearchCeil( List<? extends Comparable<? super T>> list, T value, boolean inclusive, boolean stayInBounds) { int index = Collections.binarySearch(list, value); if (index < 0) { index = ~index; } else { int listSize = list.size(); while (++index < listSize && list.get(index).compareTo(value) == 0) {} if (inclusive) { index--; } } return stayInBounds ? min(list.size() - 1, index) : index; } /** * Compares two long values and returns the same value as {@code Long.compare(long, long)}. * * @param left The left operand. * @param right The right operand. * @return 0, if left == right, a negative value if left &lt; right, or a positive value if left * &gt; right. */ public static int compareLong(long left, long right) { return left < right ? -1 : left == right ? 0 : 1; } /** * Returns the minimum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The minimum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long minValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long min = Long.MAX_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { min = min(min, sparseLongArray.valueAt(i)); } return min; } /** * Returns the maximum value in the given {@link SparseLongArray}. * * @param sparseLongArray The {@link SparseLongArray}. * @return The maximum value. * @throws NoSuchElementException If the array is empty. */ @RequiresApi(18) public static long maxValue(SparseLongArray sparseLongArray) { if (sparseLongArray.size() == 0) { throw new NoSuchElementException(); } long max = Long.MIN_VALUE; for (int i = 0; i < sparseLongArray.size(); i++) { max = max(max, sparseLongArray.valueAt(i)); } return max; } /** * Converts a time in microseconds to the corresponding time in milliseconds, preserving {@link * C#TIME_UNSET} and {@link C#TIME_END_OF_SOURCE} values. * * @param timeUs The time in microseconds. * @return The corresponding time in milliseconds. */ public static long usToMs(long timeUs) { return (timeUs == C.TIME_UNSET || timeUs == C.TIME_END_OF_SOURCE) ? timeUs : (timeUs / 1000); } /** * Converts a time in milliseconds to the corresponding time in microseconds, preserving {@link * C#TIME_UNSET} values and {@link C#TIME_END_OF_SOURCE} values. * * @param timeMs The time in milliseconds. * @return The corresponding time in microseconds. */ public static long msToUs(long timeMs) { return (timeMs == C.TIME_UNSET || timeMs == C.TIME_END_OF_SOURCE) ? timeMs : (timeMs * 1000); } /** * Returns the total duration (in microseconds) of {@code sampleCount} samples of equal duration * at {@code sampleRate}. * * <p>If {@code sampleRate} is less than {@link C#MICROS_PER_SECOND}, the duration produced by * this method can be reversed to the original sample count using {@link * #durationUsToSampleCount(long, int)}. * * @param sampleCount The number of samples. * @param sampleRate The sample rate, in samples per second. * @return The total duration, in microseconds, of {@code sampleCount} samples. */ public static long sampleCountToDurationUs(long sampleCount, int sampleRate) { return (sampleCount * C.MICROS_PER_SECOND) / sampleRate; } /** * Returns the number of samples required to represent {@code durationUs} of media at {@code * sampleRate}, assuming all samples are equal duration except the last one which may be shorter. * * <p>The result of this method <b>cannot</b> be generally reversed to the original duration with * {@link #sampleCountToDurationUs(long, int)}, due to information lost when rounding to a whole * number of samples. * * @param durationUs The duration in microseconds. * @param sampleRate The sample rate in samples per second. * @return The number of samples required to represent {@code durationUs}. */ public static long durationUsToSampleCount(long durationUs, int sampleRate) { return Util.ceilDivide(durationUs * sampleRate, C.MICROS_PER_SECOND); } /** * Parses an xs:duration attribute value, returning the parsed duration in milliseconds. * * @param value The attribute value to decode. * @return The parsed duration in milliseconds. */ public static long parseXsDuration(String value) { Matcher matcher = XS_DURATION_PATTERN.matcher(value); if (matcher.matches()) { boolean negated = !TextUtils.isEmpty(matcher.group(1)); // Durations containing years and months aren't completely defined. We assume there are // 30.4368 days in a month, and 365.242 days in a year. String years = matcher.group(3); double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0; String months = matcher.group(5); durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0; String days = matcher.group(7); durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0; String hours = matcher.group(10); durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0; String minutes = matcher.group(12); durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0; String seconds = matcher.group(14); durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0; long durationMillis = (long) (durationSeconds * 1000); return negated ? -durationMillis : durationMillis; } else { return (long) (Double.parseDouble(value) * 3600 * 1000); } } /** * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since the * epoch. * * @param value The attribute value to decode. * @return The parsed timestamp in milliseconds since the epoch. * @throws ParserException if an error occurs parsing the dateTime attribute value. */ // incompatible types in argument. // dereference of possibly-null reference matcher.group(9) @SuppressWarnings({"nullness:argument", "nullness:dereference.of.nullable"}) public static long parseXsDateTime(String value) throws ParserException { Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value); if (!matcher.matches()) { throw ParserException.createForMalformedContainer( "Invalid date/time format: " + value, /* cause= */ null); } int timezoneShift; if (matcher.group(9) == null) { // No time zone specified. timezoneShift = 0; } else if (matcher.group(9).equalsIgnoreCase("Z")) { timezoneShift = 0; } else { timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13)))); if ("-".equals(matcher.group(11))) { timezoneShift *= -1; } } Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT")); dateTime.clear(); // Note: The month value is 0-based, hence the -1 on group(2) dateTime.set( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); // we care only for milliseconds, so movePointRight(3) dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue()); } long time = dateTime.getTimeInMillis(); if (timezoneShift != 0) { time -= timezoneShift * 60000L; } return time; } /** * Scales a large timestamp. * * <p>Logically, scaling consists of a multiplication followed by a division. The actual * operations performed are designed to minimize the probability of overflow. * * @param timestamp The timestamp to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamp. */ public static long scaleLargeTimestamp(long timestamp, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; return timestamp / divisionFactor; } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; return timestamp * multiplicationFactor; } else { double multiplicationFactor = (double) multiplier / divisor; return (long) (timestamp * multiplicationFactor); } } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to a list of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. * @return The scaled timestamps. */ public static long[] scaleLargeTimestamps(List<Long> timestamps, long multiplier, long divisor) { long[] scaledTimestamps = new long[timestamps.size()]; if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) / divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = timestamps.get(i) * multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < scaledTimestamps.length; i++) { scaledTimestamps[i] = (long) (timestamps.get(i) * multiplicationFactor); } } return scaledTimestamps; } /** * Applies {@link #scaleLargeTimestamp(long, long, long)} to an array of unscaled timestamps. * * @param timestamps The timestamps to scale. * @param multiplier The multiplier. * @param divisor The divisor. */ public static void scaleLargeTimestampsInPlace(long[] timestamps, long multiplier, long divisor) { if (divisor >= multiplier && (divisor % multiplier) == 0) { long divisionFactor = divisor / multiplier; for (int i = 0; i < timestamps.length; i++) { timestamps[i] /= divisionFactor; } } else if (divisor < multiplier && (multiplier % divisor) == 0) { long multiplicationFactor = multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] *= multiplicationFactor; } } else { double multiplicationFactor = (double) multiplier / divisor; for (int i = 0; i < timestamps.length; i++) { timestamps[i] = (long) (timestamps[i] * multiplicationFactor); } } } /** * Returns the duration of media that will elapse in {@code playoutDuration}. * * @param playoutDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code playoutDuration}. */ public static long getMediaDurationForPlayoutDuration(long playoutDuration, float speed) { if (speed == 1f) { return playoutDuration; } return Math.round((double) playoutDuration * speed); } /** * Returns the playout duration of {@code mediaDuration} of media. * * @param mediaDuration The duration to scale. * @param speed The factor by which playback is sped up. * @return The scaled duration, in the same units as {@code mediaDuration}. */ public static long getPlayoutDurationForMediaDuration(long mediaDuration, float speed) { if (speed == 1f) { return mediaDuration; } return Math.round((double) mediaDuration / speed); } /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. * * @param string A string no more than four characters long. */ public static int getIntegerCodeForString(String string) { int length = string.length(); checkArgument(length <= 4); int result = 0; for (int i = 0; i < length; i++) { result <<= 8; result |= string.charAt(i); } return result; } /** * Converts an integer to a long by unsigned conversion. * * <p>This method is equivalent to {@link Integer#toUnsignedLong(int)} for API 26+. */ public static long toUnsignedLong(int x) { // x is implicitly casted to a long before the bit operation is executed but this does not // impact the method correctness. return x & 0xFFFFFFFFL; } /** * Returns the long that is composed of the bits of the 2 specified integers. * * @param mostSignificantBits The 32 most significant bits of the long to return. * @param leastSignificantBits The 32 least significant bits of the long to return. * @return a long where its 32 most significant bits are {@code mostSignificantBits} bits and its * 32 least significant bits are {@code leastSignificantBits}. */ public static long toLong(int mostSignificantBits, int leastSignificantBits) { return (toUnsignedLong(mostSignificantBits) << 32) | toUnsignedLong(leastSignificantBits); } /** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. */ public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; } /** * Returns a string containing a lower-case hex representation of the bytes provided. * * @param bytes The byte data to convert to hex. * @return A String containing the hex representation of {@code bytes}. */ public static String toHexString(byte[] bytes) { StringBuilder result = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { result .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) .append(Character.forDigit(bytes[i] & 0xF, 16)); } return result.toString(); } /** * Returns a string with comma delimited simple names of each object's class. * * @param objects The objects whose simple class names should be comma delimited and returned. * @return A string with comma delimited simple names of each object's class. */ public static String getCommaDelimitedSimpleClassNames(Object[] objects) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < objects.length; i++) { stringBuilder.append(objects[i].getClass().getSimpleName()); if (i < objects.length - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } /** * Returns a user agent string based on the given application name and the library version. * * @param context A valid context of the calling application. * @param applicationName String that will be prefix'ed to the generated user agent. * @return A user agent string generated using the applicationName and the library version. */ public static String getUserAgent(Context context, String applicationName) { String versionName; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionName = info.versionName; } catch (NameNotFoundException e) { versionName = "?"; } return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY; } /** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */ public static int getCodecCountOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); int count = 0; for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { count++; } } return count; } /** * Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. * * @param codecs A codec sequence string, as defined in RFC 6381. * @param trackType The {@link C.TrackType track type}. * @return A copy of {@code codecs} without the codecs whose track type doesn't match {@code * trackType}. If this ends up empty, or {@code codecs} is null, returns null. */ @Nullable public static String getCodecsOfType(@Nullable String codecs, @C.TrackType int trackType) { String[] codecArray = splitCodecs(codecs); if (codecArray.length == 0) { return null; } StringBuilder builder = new StringBuilder(); for (String codec : codecArray) { if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) { if (builder.length() > 0) { builder.append(","); } builder.append(codec); } } return builder.length() > 0 ? builder.toString() : null; } /** * Splits a codecs sequence string, as defined in RFC 6381, into individual codec strings. * * @param codecs A codec sequence string, as defined in RFC 6381. * @return The split codecs, or an array of length zero if the input was empty or null. */ public static String[] splitCodecs(@Nullable String codecs) { if (TextUtils.isEmpty(codecs)) { return new String[0]; } return split(codecs.trim(), "(\\s*,\\s*)"); } /** * Gets a PCM {@link Format} with the specified parameters. * * @param pcmEncoding The {@link C.PcmEncoding}. * @param channels The number of channels, or {@link Format#NO_VALUE} if unknown. * @param sampleRate The sample rate in Hz, or {@link Format#NO_VALUE} if unknown. * @return The PCM format. */ public static Format getPcmFormat(@C.PcmEncoding int pcmEncoding, int channels, int sampleRate) { return new Format.Builder() .setSampleMimeType(MimeTypes.AUDIO_RAW) .setChannelCount(channels) .setSampleRate(sampleRate) .setPcmEncoding(pcmEncoding) .build(); } /** * Converts a sample bit depth to a corresponding PCM encoding constant. * * @param bitDepth The bit depth. Supported values are 8, 16, 24 and 32. * @return The corresponding encoding. One of {@link C#ENCODING_PCM_8BIT}, {@link * C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT} and {@link C#ENCODING_PCM_32BIT}. If * the bit depth is unsupported then {@link C#ENCODING_INVALID} is returned. */ public static @C.PcmEncoding int getPcmEncoding(int bitDepth) { switch (bitDepth) { case 8: return C.ENCODING_PCM_8BIT; case 16: return C.ENCODING_PCM_16BIT; case 24: return C.ENCODING_PCM_24BIT; case 32: return C.ENCODING_PCM_32BIT; default: return C.ENCODING_INVALID; } } /** * Returns whether {@code encoding} is one of the linear PCM encodings. * * @param encoding The encoding of the audio data. * @return Whether the encoding is one of the PCM encodings. */ public static boolean isEncodingLinearPcm(@C.Encoding int encoding) { return encoding == C.ENCODING_PCM_8BIT || encoding == C.ENCODING_PCM_16BIT || encoding == C.ENCODING_PCM_16BIT_BIG_ENDIAN || encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns whether {@code encoding} is high resolution (&gt; 16-bit) PCM. * * @param encoding The encoding of the audio data. * @return Whether the encoding is high resolution PCM. */ public static boolean isEncodingHighResolutionPcm(@C.PcmEncoding int encoding) { return encoding == C.ENCODING_PCM_24BIT || encoding == C.ENCODING_PCM_32BIT || encoding == C.ENCODING_PCM_FLOAT; } /** * Returns the audio track channel configuration for the given channel count, or {@link * AudioFormat#CHANNEL_INVALID} if output is not possible. * * @param channelCount The number of channels in the input audio. * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not * possible. */ @SuppressLint("InlinedApi") // Inlined AudioFormat constants. public static int getAudioTrackChannelConfig(int channelCount) { switch (channelCount) { case 1: return AudioFormat.CHANNEL_OUT_MONO; case 2: return AudioFormat.CHANNEL_OUT_STEREO; case 3: return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 4: return AudioFormat.CHANNEL_OUT_QUAD; case 5: return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; case 6: return AudioFormat.CHANNEL_OUT_5POINT1; case 7: return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; case 8: return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; case 10: if (Util.SDK_INT >= 32) { return AudioFormat.CHANNEL_OUT_5POINT1POINT4; } else { // Before API 32, height channel masks are not available. For those 10-channel streams // supported on the audio output devices (e.g. DTS:X P2), we use 7.1-surround instead. return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND; } case 12: return AudioFormat.CHANNEL_OUT_7POINT1POINT4; default: return AudioFormat.CHANNEL_INVALID; } } /** * Returns the frame size for audio with {@code channelCount} channels in the specified encoding. * * @param pcmEncoding The encoding of the audio data. * @param channelCount The channel count. * @return The size of one audio frame in bytes. */ public static int getPcmFrameSize(@C.PcmEncoding int pcmEncoding, int channelCount) { switch (pcmEncoding) { case C.ENCODING_PCM_8BIT: return channelCount; case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT_BIG_ENDIAN: return channelCount * 2; case C.ENCODING_PCM_24BIT: return channelCount * 3; case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_FLOAT: return channelCount * 4; case C.ENCODING_INVALID: case Format.NO_VALUE: default: throw new IllegalArgumentException(); } } /** Returns the {@link C.AudioUsage} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioUsage int getAudioUsageForStreamType(@C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: return C.USAGE_ALARM; case C.STREAM_TYPE_DTMF: return C.USAGE_VOICE_COMMUNICATION_SIGNALLING; case C.STREAM_TYPE_NOTIFICATION: return C.USAGE_NOTIFICATION; case C.STREAM_TYPE_RING: return C.USAGE_NOTIFICATION_RINGTONE; case C.STREAM_TYPE_SYSTEM: return C.USAGE_ASSISTANCE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.USAGE_VOICE_COMMUNICATION; case C.STREAM_TYPE_MUSIC: default: return C.USAGE_MEDIA; } } /** Returns the {@link C.AudioContentType} corresponding to the specified {@link C.StreamType}. */ public static @C.AudioContentType int getAudioContentTypeForStreamType( @C.StreamType int streamType) { switch (streamType) { case C.STREAM_TYPE_ALARM: case C.STREAM_TYPE_DTMF: case C.STREAM_TYPE_NOTIFICATION: case C.STREAM_TYPE_RING: case C.STREAM_TYPE_SYSTEM: return C.AUDIO_CONTENT_TYPE_SONIFICATION; case C.STREAM_TYPE_VOICE_CALL: return C.AUDIO_CONTENT_TYPE_SPEECH; case C.STREAM_TYPE_MUSIC: default: return C.AUDIO_CONTENT_TYPE_MUSIC; } } /** Returns the {@link C.StreamType} corresponding to the specified {@link C.AudioUsage}. */ public static @C.StreamType int getStreamTypeForAudioUsage(@C.AudioUsage int usage) { switch (usage) { case C.USAGE_MEDIA: case C.USAGE_GAME: case C.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: return C.STREAM_TYPE_MUSIC; case C.USAGE_ASSISTANCE_SONIFICATION: return C.STREAM_TYPE_SYSTEM; case C.USAGE_VOICE_COMMUNICATION: return C.STREAM_TYPE_VOICE_CALL; case C.USAGE_VOICE_COMMUNICATION_SIGNALLING: return C.STREAM_TYPE_DTMF; case C.USAGE_ALARM: return C.STREAM_TYPE_ALARM; case C.USAGE_NOTIFICATION_RINGTONE: return C.STREAM_TYPE_RING; case C.USAGE_NOTIFICATION: case C.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case C.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case C.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case C.USAGE_NOTIFICATION_EVENT: return C.STREAM_TYPE_NOTIFICATION; case C.USAGE_ASSISTANCE_ACCESSIBILITY: case C.USAGE_ASSISTANT: case C.USAGE_UNKNOWN: default: return C.STREAM_TYPE_DEFAULT; } } /** * Returns a newly generated audio session identifier, or {@link AudioManager#ERROR} if an error * occurred in which case audio playback may fail. * * @see AudioManager#generateAudioSessionId() */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { @Nullable AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } /** * Derives a DRM {@link UUID} from {@code drmScheme}. * * @param drmScheme A UUID string, or {@code "widevine"}, {@code "playready"} or {@code * "clearkey"}. * @return The derived {@link UUID}, or {@code null} if one could not be derived. */ @Nullable public static UUID getDrmUuid(String drmScheme) { switch (Ascii.toLowerCase(drmScheme)) { case "widevine": return C.WIDEVINE_UUID; case "playready": return C.PLAYREADY_UUID; case "clearkey": return C.CLEARKEY_UUID; default: try { return UUID.fromString(drmScheme); } catch (RuntimeException e) { return null; } } } /** * Returns a {@link PlaybackException.ErrorCode} value that corresponds to the provided {@link * MediaDrm.ErrorCodes} value. Returns {@link PlaybackException#ERROR_CODE_DRM_SYSTEM_ERROR} if * the provided error code isn't recognised. */ public static @PlaybackException.ErrorCode int getErrorCodeForMediaDrmErrorCode( int mediaDrmErrorCode) { switch (mediaDrmErrorCode) { case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CONFIG: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_PARSE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_CERTIFICATE: case MediaDrm.ErrorCodes.ERROR_PROVISIONING_RETRY: return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_PARSE: case MediaDrm.ErrorCodes.ERROR_LICENSE_RELEASE: case MediaDrm.ErrorCodes.ERROR_LICENSE_REQUEST_REJECTED: case MediaDrm.ErrorCodes.ERROR_LICENSE_RESTORE: case MediaDrm.ErrorCodes.ERROR_LICENSE_STATE: case MediaDrm.ErrorCodes.ERROR_CERTIFICATE_MALFORMED: return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; case MediaDrm.ErrorCodes.ERROR_LICENSE_POLICY: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_OUTPUT_PROTECTION: case MediaDrm.ErrorCodes.ERROR_INSUFFICIENT_SECURITY: case MediaDrm.ErrorCodes.ERROR_KEY_EXPIRED: case MediaDrm.ErrorCodes.ERROR_KEY_NOT_LOADED: return PlaybackException.ERROR_CODE_DRM_DISALLOWED_OPERATION; case MediaDrm.ErrorCodes.ERROR_INIT_DATA: case MediaDrm.ErrorCodes.ERROR_FRAME_TOO_LARGE: return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; default: return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; } } /** * @deprecated Use {@link #inferContentTypeForExtension(String)} when {@code overrideExtension} is * non-empty, and {@link #inferContentType(Uri)} otherwise. */ @Deprecated public static @ContentType int inferContentType(Uri uri, @Nullable String overrideExtension) { return TextUtils.isEmpty(overrideExtension) ? inferContentType(uri) : inferContentTypeForExtension(overrideExtension); } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri}. * * @param uri The {@link Uri}. * @return The content type. */ public static @ContentType int inferContentType(Uri uri) { @Nullable String scheme = uri.getScheme(); if (scheme != null && Ascii.equalsIgnoreCase("rtsp", scheme)) { return C.CONTENT_TYPE_RTSP; } @Nullable String lastPathSegment = uri.getLastPathSegment(); if (lastPathSegment == null) { return C.CONTENT_TYPE_OTHER; } int lastDotIndex = lastPathSegment.lastIndexOf('.'); if (lastDotIndex >= 0) { @C.ContentType int contentType = inferContentTypeForExtension(lastPathSegment.substring(lastDotIndex + 1)); if (contentType != C.CONTENT_TYPE_OTHER) { // If contentType is TYPE_SS that indicates the extension is .ism or .isml and shows the ISM // URI is missing the "/manifest" suffix, which contains the information used to // disambiguate between Smooth Streaming, HLS and DASH below - so we can just return TYPE_SS // here without further checks. return contentType; } } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(checkNotNull(uri.getPath())); if (ismMatcher.matches()) { @Nullable String extensions = ismMatcher.group(2); if (extensions != null) { if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_DASH; } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) { return C.CONTENT_TYPE_HLS; } } return C.CONTENT_TYPE_SS; } return C.CONTENT_TYPE_OTHER; } /** * @deprecated Use {@link Uri#parse(String)} and {@link #inferContentType(Uri)} for full file * paths or {@link #inferContentTypeForExtension(String)} for extensions. */ @Deprecated public static @ContentType int inferContentType(String fileName) { return inferContentType(Uri.parse("file:///" + fileName)); } /** * Makes a best guess to infer the {@link ContentType} from a file extension. * * @param fileExtension The extension of the file (excluding the '.'). * @return The content type. */ public static @ContentType int inferContentTypeForExtension(String fileExtension) { fileExtension = Ascii.toLowerCase(fileExtension); switch (fileExtension) { case "mpd": return C.CONTENT_TYPE_DASH; case "m3u8": return C.CONTENT_TYPE_HLS; case "ism": case "isml": return C.TYPE_SS; default: return C.CONTENT_TYPE_OTHER; } } /** * Makes a best guess to infer the {@link ContentType} from a {@link Uri} and optional MIME type. * * @param uri The {@link Uri}. * @param mimeType If MIME type, or {@code null}. * @return The content type. */ public static @ContentType int inferContentTypeForUriAndMimeType( Uri uri, @Nullable String mimeType) { if (mimeType == null) { return inferContentType(uri); } switch (mimeType) { case MimeTypes.APPLICATION_MPD: return C.CONTENT_TYPE_DASH; case MimeTypes.APPLICATION_M3U8: return C.CONTENT_TYPE_HLS; case MimeTypes.APPLICATION_SS: return C.CONTENT_TYPE_SS; case MimeTypes.APPLICATION_RTSP: return C.CONTENT_TYPE_RTSP; default: return C.CONTENT_TYPE_OTHER; } } /** * Returns the MIME type corresponding to the given adaptive {@link ContentType}, or {@code null} * if the content type is not adaptive. */ @Nullable public static String getAdaptiveMimeTypeForContentType(@ContentType int contentType) { switch (contentType) { case C.CONTENT_TYPE_DASH: return MimeTypes.APPLICATION_MPD; case C.CONTENT_TYPE_HLS: return MimeTypes.APPLICATION_M3U8; case C.CONTENT_TYPE_SS: return MimeTypes.APPLICATION_SS; case C.CONTENT_TYPE_RTSP: case C.CONTENT_TYPE_OTHER: default: return null; } } /** * If the provided URI is an ISM Presentation URI, returns the URI with "Manifest" appended to its * path (i.e., the corresponding default manifest URI). Else returns the provided URI without * modification. See [MS-SSTR] v20180912, section 2.2.1. * * @param uri The original URI. * @return The fixed URI. */ public static Uri fixSmoothStreamingIsmManifestUri(Uri uri) { @Nullable String path = uri.getPath(); if (path == null) { return uri; } Matcher ismMatcher = ISM_PATH_PATTERN.matcher(path); if (ismMatcher.matches() && ismMatcher.group(1) == null) { // Add missing "Manifest" suffix. return Uri.withAppendedPath(uri, "Manifest"); } return uri; } /** * Returns the specified millisecond time formatted as a string. * * @param builder The builder that {@code formatter} will write to. * @param formatter The formatter. * @param timeMs The time to format as a string, in milliseconds. * @return The time formatted as a string. */ public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) { if (timeMs == C.TIME_UNSET) { timeMs = 0; } String prefix = timeMs < 0 ? "-" : ""; timeMs = abs(timeMs); long totalSeconds = (timeMs + 500) / 1000; long seconds = totalSeconds % 60; long minutes = (totalSeconds / 60) % 60; long hours = totalSeconds / 3600; builder.setLength(0); return hours > 0 ? formatter.format("%s%d:%02d:%02d", prefix, hours, minutes, seconds).toString() : formatter.format("%s%02d:%02d", prefix, minutes, seconds).toString(); } /** * Escapes a string so that it's safe for use as a file or directory name on at least FAT32 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today. * * <p>For simplicity, this only handles common characters known to be illegal on FAT32: &lt;, * &gt;, :, ", /, \, |, ?, 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 {@link * #unescapeFileName(String)} can be used to retrieve the original file name. * * @param fileName File name to be escaped. * @return An escaped file name which will be safe for use on at least FAT32 filesystems. */ public static String escapeFileName(String fileName) { int length = fileName.length(); int charactersToEscapeCount = 0; for (int i = 0; i < length; i++) { if (shouldEscapeCharacter(fileName.charAt(i))) { charactersToEscapeCount++; } } if (charactersToEscapeCount == 0) { return fileName; } int i = 0; StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2); while (charactersToEscapeCount > 0) { char c = fileName.charAt(i++); if (shouldEscapeCharacter(c)) { builder.append('%').append(Integer.toHexString(c)); charactersToEscapeCount--; } else { builder.append(c); } } if (i < length) { builder.append(fileName, i, length); } return builder.toString(); } private static boolean shouldEscapeCharacter(char c) { switch (c) { case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': case '%': return true; default: return false; } } /** * Unescapes an escaped file or directory name back to its original value. * * <p>See {@link #escapeFileName(String)} for more information. * * @param fileName File name to be unescaped. * @return The original value of the file name before it was escaped, or null if the escaped * fileName seems invalid. */ @Nullable public static String unescapeFileName(String fileName) { int length = fileName.length(); int percentCharacterCount = 0; for (int i = 0; i < length; i++) { if (fileName.charAt(i) == '%') { percentCharacterCount++; } } if (percentCharacterCount == 0) { return fileName; } int expectedLength = length - percentCharacterCount * 2; StringBuilder builder = new StringBuilder(expectedLength); Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; } if (startOfNotEscaped < length) { builder.append(fileName, startOfNotEscaped, length); } if (builder.length() != expectedLength) { return null; } return builder.toString(); } /** Returns a data URI with the specified MIME type and data. */ public static Uri getDataUriForString(String mimeType, String data) { return Uri.parse( "data:" + mimeType + ";base64," + Base64.encodeToString(data.getBytes(), Base64.NO_WRAP)); } /** * A hacky method that always throws {@code t} even if {@code t} is a checked exception, and is * not declared to be thrown. */ public static void sneakyThrow( [MASK] t) { sneakyThrowInternal(t); } @SuppressWarnings("unchecked") private static <T extends [MASK] > void sneakyThrowInternal( [MASK] t) throws T { throw (T) t; } /** Recursively deletes a directory and its content. */ public static void recursiveDelete(File fileOrDirectory) { File[] directoryFiles = fileOrDirectory.listFiles(); if (directoryFiles != null) { for (File child : directoryFiles) { recursiveDelete(child); } } fileOrDirectory.delete(); } /** Creates an empty directory in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempDirectory(Context context, String prefix) throws IOException { File tempFile = createTempFile(context, prefix); tempFile.delete(); // Delete the temp file. tempFile.mkdir(); // Create a directory with the same name. return tempFile; } /** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, checkNotNull(context.getCacheDir())); } /** * Returns the result of updating a CRC-32 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc32(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = (initialValue << 8) ^ CRC32_BYTES_MSBF[((initialValue >>> 24) ^ (bytes[i] & 0xFF)) & 0xFF]; } return initialValue; } /** * Returns the result of updating a CRC-8 with the specified bytes in a "most significant bit * first" order. * * @param bytes Array containing the bytes to update the crc value with. * @param start The index to the first byte in the byte range to update the crc with. * @param end The index after the last byte in the byte range to update the crc with. * @param initialValue The initial value for the crc calculation. * @return The result of updating the initial value with the specified bytes. */ public static int crc8(byte[] bytes, int start, int end, int initialValue) { for (int i = start; i < end; i++) { initialValue = CRC8_BYTES_MSBF[initialValue ^ (bytes[i] & 0xFF)]; } return initialValue; } /** Compresses {@code input} using gzip and returns the result in a newly allocated byte array. */ public static byte[] gzip(byte[] input) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPOutputStream os = new GZIPOutputStream(output)) { os.write(input); } catch (IOException e) { // A ByteArrayOutputStream wrapped in a GZipOutputStream should never throw IOException since // no I/O is happening. throw new IllegalStateException(e); } return output.toByteArray(); } /** * Absolute <i>get</i> method for reading an int value in {@link ByteOrder#BIG_ENDIAN} in a {@link * ByteBuffer}. Same as {@link ByteBuffer#getInt(int)} except the buffer's order as returned by * {@link ByteBuffer#order()} is ignored and {@link ByteOrder#BIG_ENDIAN} is used instead. * * @param buffer The buffer from which to read an int in big endian. * @param index The index from which the bytes will be read. * @return The int value at the given index with the buffer bytes ordered most significant to * least significant. */ public static int getBigEndianInt(ByteBuffer buffer, int index) { int value = buffer.getInt(index); return buffer.order() == ByteOrder.BIG_ENDIAN ? value : Integer.reverseBytes(value); } /** * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC * (Mobile Country Code), or the country code of the default Locale if not available. * * @param context A context to access the telephony service. If null, only the Locale can be used. * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable. */ public static String getCountryCode(@Nullable Context context) { if (context != null) { @Nullable TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { String countryCode = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryCode)) { return Ascii.toUpperCase(countryCode); } } } return Ascii.toUpperCase(Locale.getDefault().getCountry()); } /** * Returns a non-empty array of normalized IETF BCP 47 language tags for the system languages * ordered by preference. */ public static String[] getSystemLanguageCodes() { String[] systemLocales = getSystemLocales(); for (int i = 0; i < systemLocales.length; i++) { systemLocales[i] = normalizeLanguageCode(systemLocales[i]); } return systemLocales; } /** Returns the default {@link Locale.Category#DISPLAY DISPLAY} {@link Locale}. */ public static Locale getDefaultDisplayLocale() { return SDK_INT >= 24 ? Locale.getDefault(Locale.Category.DISPLAY) : Locale.getDefault(); } /** * Uncompresses the data in {@code input}. * * @param input Wraps the compressed input data. * @param output Wraps an output buffer to be used to store the uncompressed data. If {@code * output.data} isn't big enough to hold the uncompressed data, a new array is created. If * {@code true} is returned then the output's position will be set to 0 and its limit will be * set to the length of the uncompressed data. * @param inflater If not null, used to uncompressed the input. Otherwise a new {@link Inflater} * is created. * @return Whether the input is uncompressed successfully. */ public static boolean inflate( ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) { if (input.bytesLeft() <= 0) { return false; } if (output.capacity() < input.bytesLeft()) { output.ensureCapacity(2 * input.bytesLeft()); } if (inflater == null) { inflater = new Inflater(); } inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft()); try { int outputSize = 0; while (true) { outputSize += inflater.inflate(output.getData(), outputSize, output.capacity() - outputSize); if (inflater.finished()) { output.setLimit(outputSize); return true; } if (inflater.needsDictionary() || inflater.needsInput()) { return false; } if (outputSize == output.capacity()) { output.ensureCapacity(output.capacity() * 2); } } } catch (DataFormatException e) { return false; } finally { inflater.reset(); } } /** * Returns whether the app is running on a TV device. * * @param context Any context. * @return Whether the app is running on a TV device. */ public static boolean isTv(Context context) { // See https://developer.android.com/training/tv/start/hardware.html#runtime-check. @Nullable UiModeManager uiModeManager = (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE); return uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; } /** * Returns whether the app is running on an automotive device. * * @param context Any context. * @return Whether the app is running on an automotive device. */ public static boolean isAutomotive(Context context) { return SDK_INT >= 23 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } /** * Gets the size of the current mode of the default display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { @Nullable Display defaultDisplay = null; if (SDK_INT >= 17) { @Nullable DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); // We don't expect displayManager to ever be null, so this check is just precautionary. // Consider removing it when the library minSdkVersion is increased to 17 or higher. if (displayManager != null) { defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY); } } if (defaultDisplay == null) { WindowManager windowManager = checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); defaultDisplay = windowManager.getDefaultDisplay(); } return getCurrentDisplayModeSize(context, defaultDisplay); } /** * Gets the size of the current mode of the specified display, in pixels. * * <p>Note that due to application UI scaling, the number of pixels made available to applications * (as reported by {@link Display#getSize(Point)} may differ from the mode's actual resolution (as * reported by this function). For example, applications running on a display configured with a 4K * mode may have their UI laid out and rendered in 1080p and then scaled up. Applications can take * advantage of the full mode resolution through a {@link SurfaceView} using full size buffers. * * @param context Any context. * @param display The display whose size is to be returned. * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context, Display display) { if (display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) { // On Android TVs it's common for the UI to be driven at a lower resolution than the physical // resolution of the display (e.g., driving the UI at 1080p when the display is 4K). // SurfaceView outputs are still able to use the full physical resolution on such devices. // // Prior to API level 26, the Display object did not provide a way to obtain the true physical // resolution of the display. From API level 26, Display.getMode().getPhysical[Width|Height] // is expected to return the display's true physical resolution, but we still see devices // setting their hardware compositor output size incorrectly, which makes this unreliable. // Hence for TV devices, we try and read the display's true physical resolution from system // properties. // // From API level 28, Treble may prevent the system from writing sys.display-size, so we check // vendor.display-size instead. @Nullable String displaySize = SDK_INT < 28 ? getSystemProperty("sys.display-size") : getSystemProperty("vendor.display-size"); // If we managed to read the display size, attempt to parse it. if (!TextUtils.isEmpty(displaySize)) { try { String[] displaySizeParts = split(displaySize.trim(), "x"); if (displaySizeParts.length == 2) { int width = Integer.parseInt(displaySizeParts[0]); int height = Integer.parseInt(displaySizeParts[1]); if (width > 0 && height > 0) { return new Point(width, height); } } } catch (NumberFormatException e) { // Do nothing. } Log.e(TAG, "Invalid display size: " + displaySize); } // Sony Android TVs advertise support for 4k output via a system feature. if ("Sony".equals(MANUFACTURER) && MODEL.startsWith("BRAVIA") && context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) { return new Point(3840, 2160); } } Point displaySize = new Point(); if (SDK_INT >= 23) { getDisplaySizeV23(display, displaySize); } else if (SDK_INT >= 17) { getDisplaySizeV17(display, displaySize); } else { getDisplaySizeV16(display, displaySize); } return displaySize; } /** * Returns a string representation of a {@link C.TrackType}. * * @param trackType A {@link C.TrackType} constant, * @return A string representation of this constant. */ public static String getTrackTypeString(@C.TrackType int trackType) { switch (trackType) { case C.TRACK_TYPE_DEFAULT: return "default"; case C.TRACK_TYPE_AUDIO: return "audio"; case C.TRACK_TYPE_VIDEO: return "video"; case C.TRACK_TYPE_TEXT: return "text"; case C.TRACK_TYPE_IMAGE: return "image"; case C.TRACK_TYPE_METADATA: return "metadata"; case C.TRACK_TYPE_CAMERA_MOTION: return "camera motion"; case C.TRACK_TYPE_NONE: return "none"; case C.TRACK_TYPE_UNKNOWN: return "unknown"; default: return trackType >= C.TRACK_TYPE_CUSTOM_BASE ? "custom (" + trackType + ")" : "?"; } } /** * Returns the current time in milliseconds since the epoch. * * @param elapsedRealtimeEpochOffsetMs The offset between {@link SystemClock#elapsedRealtime()} * and the time since the Unix epoch, or {@link C#TIME_UNSET} if unknown. * @return The Unix time in milliseconds since the epoch. */ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) { return elapsedRealtimeEpochOffsetMs == C.TIME_UNSET ? System.currentTimeMillis() : SystemClock.elapsedRealtime() + elapsedRealtimeEpochOffsetMs; } /** * Moves the elements starting at {@code fromIndex} to {@code newFromIndex}. * * @param items The list of which to move elements. * @param fromIndex The index at which the items to move start. * @param toIndex The index up to which elements should be moved (exclusive). * @param newFromIndex The new from index. */ @SuppressWarnings("ExtendsObject") // See go/lsc-extends-object public static <T extends Object> void moveItems( List<T> items, int fromIndex, int toIndex, int newFromIndex) { ArrayDeque<T> removedItems = new ArrayDeque<>(); int removedItemsLength = toIndex - fromIndex; for (int i = removedItemsLength - 1; i >= 0; i--) { removedItems.addFirst(items.remove(fromIndex + i)); } items.addAll(min(newFromIndex, items.size()), removedItems); } /** Returns whether the table exists in the database. */ public static boolean tableExists(SQLiteDatabase database, String tableName) { long count = DatabaseUtils.queryNumEntries( database, "sqlite_master", "tbl_name = ?", new String[] {tableName}); return count > 0; } /** * Attempts to parse an error code from a diagnostic string found in framework media exceptions. * * <p>For example: android.media.MediaCodec.error_1 or android.media.MediaDrm.error_neg_2. * * @param diagnosticsInfo A string from which to parse the error code. * @return The parser error code, or 0 if an error code could not be parsed. */ public static int getErrorCodeFromPlatformDiagnosticsInfo(@Nullable String diagnosticsInfo) { // TODO (internal b/192337376): Change 0 for ERROR_UNKNOWN once available. if (diagnosticsInfo == null) { return 0; } String[] strings = split(diagnosticsInfo, "_"); int length = strings.length; if (length < 2) { return 0; } String digitsSection = strings[length - 1]; boolean isNegative = length >= 3 && "neg".equals(strings[length - 2]); try { int errorCode = Integer.parseInt(Assertions.checkNotNull(digitsSection)); return isNegative ? -errorCode : errorCode; } catch (NumberFormatException e) { return 0; } } /** * Returns the number of maximum pending output frames that are allowed on a {@link MediaCodec} * decoder. */ public static int getMaxPendingFramesCountForMediaCodecDecoders( Context context, String codecName, boolean requestedHdrToneMapping) { if (SDK_INT < 29 || context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29) { // Prior to API 29, decoders may drop frames to keep their output surface from growing out of // bounds. From API 29, if the app targets API 29 or later, the {@link // MediaFormat#KEY_ALLOW_FRAME_DROP} key prevents frame dropping even when the surface is // full. // Frame dropping is never desired, so a workaround is needed for older API levels. // Allow a maximum of one frame to be pending at a time to prevent frame dropping. // TODO(b/226330223): Investigate increasing this limit. return 1; } // Limit the maximum amount of frames for all decoders. This is a tentative value that should be // large enough to avoid significant performance degradation, but small enough to bypass decoder // issues. // // TODO: b/278234847 - Evaluate whether this reduces decoder timeouts, and consider restoring // prior higher limits as appropriate. // // Some OMX decoders don't correctly track their number of output buffers available, and get // stuck if too many frames are rendered without being processed. This value is experimentally // determined. See also // b/213455700, b/230097284, b/229978305, and b/245491744. // // OMX video codecs should no longer exist from android.os.Build.DEVICE_INITIAL_SDK_INT 31+. return 5; } /** * Returns string representation of a {@link C.FormatSupport} flag. * * @param formatSupport A {@link C.FormatSupport} flag. * @return A string representation of the flag. */ public static String getFormatSupportString(@C.FormatSupport int formatSupport) { switch (formatSupport) { case C.FORMAT_HANDLED: return "YES"; case C.FORMAT_EXCEEDS_CAPABILITIES: return "NO_EXCEEDS_CAPABILITIES"; case C.FORMAT_UNSUPPORTED_DRM: return "NO_UNSUPPORTED_DRM"; case C.FORMAT_UNSUPPORTED_SUBTYPE: return "NO_UNSUPPORTED_TYPE"; case C.FORMAT_UNSUPPORTED_TYPE: return "NO"; default: throw new IllegalStateException(); } } /** * Returns the {@link Commands} available in the {@link Player}. * * @param player The {@link Player}. * @param permanentAvailableCommands The commands permanently available in the player. * @return The available {@link Commands}. */ public static Commands getAvailableCommands(Player player, Commands permanentAvailableCommands) { boolean isPlayingAd = player.isPlayingAd(); boolean isCurrentMediaItemSeekable = player.isCurrentMediaItemSeekable(); boolean hasPreviousMediaItem = player.hasPreviousMediaItem(); boolean hasNextMediaItem = player.hasNextMediaItem(); boolean isCurrentMediaItemLive = player.isCurrentMediaItemLive(); boolean isCurrentMediaItemDynamic = player.isCurrentMediaItemDynamic(); boolean isTimelineEmpty = player.getCurrentTimeline().isEmpty(); return new Commands.Builder() .addAll(permanentAvailableCommands) .addIf(COMMAND_SEEK_TO_DEFAULT_POSITION, !isPlayingAd) .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPreviousMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_PREVIOUS, !isTimelineEmpty && (hasPreviousMediaItem || !isCurrentMediaItemLive || isCurrentMediaItemSeekable) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNextMediaItem && !isPlayingAd) .addIf( COMMAND_SEEK_TO_NEXT, !isTimelineEmpty && (hasNextMediaItem || (isCurrentMediaItemLive && isCurrentMediaItemDynamic)) && !isPlayingAd) .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd) .addIf(COMMAND_SEEK_BACK, isCurrentMediaItemSeekable && !isPlayingAd) .addIf(COMMAND_SEEK_FORWARD, isCurrentMediaItemSeekable && !isPlayingAd) .build(); } /** * Returns the sum of all summands of the given array. * * @param summands The summands to calculate the sum from. * @return The sum of all summands. */ public static long sum(long... summands) { long sum = 0; for (long summand : summands) { sum += summand; } return sum; } /** * Returns a {@link Drawable} for the given resource or throws a {@link * Resources.NotFoundException} if not found. * * @param context The context to get the theme from starting with API 21. * @param resources The resources to load the drawable from. * @param drawableRes The drawable resource int. * @return The loaded {@link Drawable}. */ public static Drawable getDrawable( Context context, Resources resources, @DrawableRes int drawableRes) { return SDK_INT >= 21 ? Api21.getDrawable(context, resources, drawableRes) : resources.getDrawable(drawableRes); } /** * Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}. * * @param i An integer to be converted to String. */ public static String intToStringMaxRadix(int i) { return Integer.toString(i, Character.MAX_RADIX); } /** * Returns whether a play button should be presented on a UI element for playback control. If * {@code false}, a pause button should be shown instead. * * <p>Use {@link #handlePlayPauseButtonAction}, {@link #handlePlayButtonAction} or {@link * #handlePauseButtonAction} to handle the interaction with the play or pause button UI element. * * @param player The {@link Player}. May be null. */ @EnsuresNonNullIf(result = false, expression = "#1") public static boolean shouldShowPlayButton(@Nullable Player player) { return player == null || !player.getPlayWhenReady() || player.getPlaybackState() == Player.STATE_IDLE || player.getPlaybackState() == Player.STATE_ENDED; } /** * Updates the player to handle an interaction with a play button. * * <p>This method assumes the play button is enabled if {@link #shouldShowPlayButton} returns * true. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayButtonAction(@Nullable Player player) { if (player == null) { return false; } @Player.State int state = player.getPlaybackState(); boolean methodTriggered = false; if (state == Player.STATE_IDLE && player.isCommandAvailable(COMMAND_PREPARE)) { player.prepare(); methodTriggered = true; } else if (state == Player.STATE_ENDED && player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) { player.seekToDefaultPosition(); methodTriggered = true; } if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.play(); methodTriggered = true; } return methodTriggered; } /** * Updates the player to handle an interaction with a pause button. * * <p>This method assumes the pause button is enabled if {@link #shouldShowPlayButton} returns * false. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePauseButtonAction(@Nullable Player player) { if (player != null && player.isCommandAvailable(COMMAND_PLAY_PAUSE)) { player.pause(); return true; } return false; } /** * Updates the player to handle an interaction with a play or pause button. * * <p>This method assumes that the UI element enables a play button if {@link * #shouldShowPlayButton} returns true and a pause button otherwise. * * @param player The {@link Player}. May be null. * @return Whether a player method was triggered to handle this action. */ public static boolean handlePlayPauseButtonAction(@Nullable Player player) { if (shouldShowPlayButton(player)) { return handlePlayButtonAction(player); } else { return handlePauseButtonAction(player); } } @Nullable private static String getSystemProperty(String name) { try { @SuppressLint("PrivateApi") Class<?> systemProperties = Class.forName("android.os.SystemProperties"); Method getMethod = systemProperties.getMethod("get", String.class); return (String) getMethod.invoke(systemProperties, name); } catch (Exception e) { Log.e(TAG, "Failed to read system property " + name, e); return null; } } @RequiresApi(23) private static void getDisplaySizeV23(Display display, Point outSize) { Display.Mode mode = display.getMode(); outSize.x = mode.getPhysicalWidth(); outSize.y = mode.getPhysicalHeight(); } @RequiresApi(17) private static void getDisplaySizeV17(Display display, Point outSize) { display.getRealSize(outSize); } private static void getDisplaySizeV16(Display display, Point outSize) { display.getSize(outSize); } private static String[] getSystemLocales() { Configuration config = Resources.getSystem().getConfiguration(); return SDK_INT >= 24 ? getSystemLocalesV24(config) : new String[] {getLocaleLanguageTag(config.locale)}; } @RequiresApi(24) private static String[] getSystemLocalesV24(Configuration config) { return split(config.getLocales().toLanguageTags(), ","); } @RequiresApi(21) private static String getLocaleLanguageTagV21(Locale locale) { return locale.toLanguageTag(); } private static HashMap<String, String> createIsoLanguageReplacementMap() { String[] iso2Languages = Locale.getISOLanguages(); HashMap<String, String> replacedLanguages = new HashMap<>( /* initialCapacity= */ iso2Languages.length + additionalIsoLanguageReplacements.length); for (String iso2 : iso2Languages) { try { // This returns the ISO 639-2/T code for the language. String iso3 = new Locale(iso2).getISO3Language(); if (!TextUtils.isEmpty(iso3)) { replacedLanguages.put(iso3, iso2); } } catch (MissingResourceException e) { // Shouldn't happen for list of known languages, but we don't want to throw either. } } // Add additional replacement mappings. for (int i = 0; i < additionalIsoLanguageReplacements.length; i += 2) { replacedLanguages.put( additionalIsoLanguageReplacements[i], additionalIsoLanguageReplacements[i + 1]); } return replacedLanguages; } @RequiresApi(api = Build.VERSION_CODES.M) private static boolean requestExternalStoragePermission(Activity activity) { if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions( new String[] {permission.READ_EXTERNAL_STORAGE}, /* requestCode= */ 0); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceLegacyLanguageTags(String languageTag) { for (int i = 0; i < isoLegacyTagReplacements.length; i += 2) { if (languageTag.startsWith(isoLegacyTagReplacements[i])) { return isoLegacyTagReplacements[i + 1] + languageTag.substring(/* beginIndex= */ isoLegacyTagReplacements[i].length()); } } return languageTag; } // Additional mapping from ISO3 to ISO2 language codes. private static final String[] additionalIsoLanguageReplacements = new String[] { // Bibliographical codes defined in ISO 639-2/B, replaced by terminological code defined in // ISO 639-2/T. See https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes. "alb", "sq", "arm", "hy", "baq", "eu", "bur", "my", "tib", "bo", "chi", "zh", "cze", "cs", "dut", "nl", "ger", "de", "gre", "el", "fre", "fr", "geo", "ka", "ice", "is", "mac", "mk", "mao", "mi", "may", "ms", "per", "fa", "rum", "ro", "scc", "hbs-srp", "slo", "sk", "wel", "cy", // Deprecated 2-letter codes, replaced by modern equivalent (including macrolanguage) // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes, "ISO 639:1988" "id", "ms-ind", "iw", "he", "heb", "he", "ji", "yi", // Individual macrolanguage codes mapped back to full macrolanguage code. // See https://en.wikipedia.org/wiki/ISO_639_macrolanguage "arb", "ar-arb", "in", "ms-ind", "ind", "ms-ind", "nb", "no-nob", "nob", "no-nob", "nn", "no-nno", "nno", "no-nno", "tw", "ak-twi", "twi", "ak-twi", "bs", "hbs-bos", "bos", "hbs-bos", "hr", "hbs-hrv", "hrv", "hbs-hrv", "sr", "hbs-srp", "srp", "hbs-srp", "cmn", "zh-cmn", "hak", "zh-hak", "nan", "zh-nan", "hsn", "zh-hsn" }; // Legacy tags that have been replaced by modern equivalents (including macrolanguage) // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry. private static final String[] isoLegacyTagReplacements = new String[] { "i-lux", "lb", "i-hak", "zh-hak", "i-navajo", "nv", "no-bok", "no-nob", "no-nyn", "no-nno", "zh-guoyu", "zh-cmn", "zh-hakka", "zh-hak", "zh-min-nan", "zh-nan", "zh-xiang", "zh-hsn" }; /** * Allows the CRC-32 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC32_BYTES_MSBF = { 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 }; /** * Allows the CRC-8 calculation to be done byte by byte instead of bit per bit in the order "most * significant bit first". */ private static final int[] CRC8_BYTES_MSBF = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; @RequiresApi(21) private static final class Api21 { @DoNotInline public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) { return resources.getDrawable(res, context.getTheme()); } } }
Throwable