com.google.android.exoplayer2.util.Util.closeQuietly()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中com.google.android.exoplayer2.util.Util.closeQuietly()方法的一些代码示例,展示了Util.closeQuietly()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.closeQuietly()方法的具体详情如下:
包路径:com.google.android.exoplayer2.util.Util
类名称:Util
方法名:closeQuietly

Util.closeQuietly介绍

[英]Closes a DataSource, suppressing any IOException that may occur.
[中]关闭数据源,抑制任何可能发生的IOException。

代码示例

代码示例来源:origin: google/ExoPlayer

  1. @SuppressWarnings("ThrowFromFinallyBlock")
  2. private void closeCurrentOutputStream() throws IOException {
  3. if (outputStream == null) {
  4. return;
  5. }
  6. boolean success = false;
  7. try {
  8. outputStream.flush();
  9. if (syncFileDescriptor) {
  10. underlyingFileOutputStream.getFD().sync();
  11. }
  12. success = true;
  13. } finally {
  14. Util.closeQuietly(outputStream);
  15. outputStream = null;
  16. File fileToCommit = file;
  17. file = null;
  18. if (success) {
  19. cache.commitFile(fileToCommit);
  20. } else {
  21. fileToCommit.delete();
  22. }
  23. }
  24. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Loads {@link DownloadAction}s from file.
  3. *
  4. * @param deserializers {@link Deserializer}s to deserialize DownloadActions.
  5. * @return Loaded DownloadActions. If the action file doesn't exists returns an empty array.
  6. * @throws IOException If there is an error during loading.
  7. */
  8. public DownloadAction[] load(Deserializer... deserializers) throws IOException {
  9. if (!actionFile.exists()) {
  10. return new DownloadAction[0];
  11. }
  12. InputStream inputStream = null;
  13. try {
  14. inputStream = atomicFile.openRead();
  15. DataInputStream dataInputStream = new DataInputStream(inputStream);
  16. int version = dataInputStream.readInt();
  17. if (version > VERSION) {
  18. throw new IOException("Unsupported action file version: " + version);
  19. }
  20. int actionCount = dataInputStream.readInt();
  21. DownloadAction[] actions = new DownloadAction[actionCount];
  22. for (int i = 0; i < actionCount; i++) {
  23. actions[i] = DownloadAction.deserializeFromStream(deserializers, dataInputStream);
  24. }
  25. return actions;
  26. } finally {
  27. Util.closeQuietly(inputStream);
  28. }
  29. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Stores {@link DownloadAction}s to file.
  3. *
  4. * @param downloadActions DownloadActions to store to file.
  5. * @throws IOException If there is an error during storing.
  6. */
  7. public void store(DownloadAction... downloadActions) throws IOException {
  8. DataOutputStream output = null;
  9. try {
  10. output = new DataOutputStream(atomicFile.startWrite());
  11. output.writeInt(VERSION);
  12. output.writeInt(downloadActions.length);
  13. for (DownloadAction action : downloadActions) {
  14. DownloadAction.serializeToStream(action, output);
  15. }
  16. atomicFile.endWrite(output);
  17. // Avoid calling close twice.
  18. output = null;
  19. } finally {
  20. Util.closeQuietly(output);
  21. }
  22. }

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(reader);

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public void load() throws IOException, InterruptedException {
  3. // We always load from the beginning, so reset bytesRead to 0.
  4. dataSource.resetBytesRead();
  5. try {
  6. // Create and open the input.
  7. dataSource.open(dataSpec);
  8. // Load the sample data.
  9. int result = 0;
  10. while (result != C.RESULT_END_OF_INPUT) {
  11. int sampleSize = (int) dataSource.getBytesRead();
  12. if (sampleData == null) {
  13. sampleData = new byte[INITIAL_SAMPLE_SIZE];
  14. } else if (sampleSize == sampleData.length) {
  15. sampleData = Arrays.copyOf(sampleData, sampleData.length * 2);
  16. }
  17. result = dataSource.read(sampleData, sampleSize, sampleData.length - sampleSize);
  18. }
  19. } finally {
  20. Util.closeQuietly(dataSource);
  21. }
  22. }

代码示例来源:origin: google/ExoPlayer

  1. private long readInputLength() throws IOException {
  2. DataSpec dataSpec =
  3. new DataSpec(Uri.parse("asset:///" + PS_FILE_PATH), 0, C.LENGTH_UNSET, null);
  4. long totalInputLength = dataSource.open(dataSpec);
  5. Util.closeQuietly(dataSource);
  6. return totalInputLength;
  7. }

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(inputStream);

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public final void load() throws IOException, InterruptedException {
  3. try {
  4. dataSource.open(dataSpec);
  5. int limit = 0;
  6. int bytesRead = 0;
  7. while (bytesRead != C.RESULT_END_OF_INPUT && !loadCanceled) {
  8. maybeExpandData(limit);
  9. bytesRead = dataSource.read(data, limit, READ_GRANULARITY);
  10. if (bytesRead != -1) {
  11. limit += bytesRead;
  12. }
  13. }
  14. if (!loadCanceled) {
  15. consume(data, limit);
  16. }
  17. } finally {
  18. Util.closeQuietly(dataSource);
  19. }
  20. }

代码示例来源:origin: google/ExoPlayer

  1. private void maybeLoadInitData() throws IOException, InterruptedException {
  2. if (initLoadCompleted || initDataSpec == null) {
  3. // Note: The HLS spec forbids initialization segments for packed audio.
  4. return;
  5. }
  6. DataSpec initSegmentDataSpec = initDataSpec.subrange(initSegmentBytesLoaded);
  7. try {
  8. DefaultExtractorInput input = prepareExtraction(initDataSource, initSegmentDataSpec);
  9. try {
  10. int result = Extractor.RESULT_CONTINUE;
  11. while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
  12. result = extractor.read(input, null);
  13. }
  14. } finally {
  15. initSegmentBytesLoaded = (int) (input.getPosition() - initDataSpec.absoluteStreamPosition);
  16. }
  17. } finally {
  18. Util.closeQuietly(initDataSource);
  19. }
  20. initLoadCompleted = true;
  21. }

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public final void load() throws IOException {
  3. // We always load from the beginning, so reset bytesRead to 0.
  4. dataSource.resetBytesRead();
  5. DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
  6. try {
  7. inputStream.open();
  8. Uri dataSourceUri = Assertions.checkNotNull(dataSource.getUri());
  9. result = parser.parse(dataSourceUri, inputStream);
  10. } finally {
  11. Util.closeQuietly(inputStream);
  12. }
  13. }
  14. }

代码示例来源:origin: google/ExoPlayer

  1. private SeekMap extractSeekMapAndTracks(PsExtractor extractor, FakeExtractorOutput output)
  2. throws IOException, InterruptedException {
  3. ExtractorInput input = getExtractorInputFromPosition(0);
  4. extractor.init(output);
  5. int readResult = Extractor.RESULT_CONTINUE;
  6. while (true) {
  7. try {
  8. // Keep reading until we can get the seek map
  9. while (readResult == Extractor.RESULT_CONTINUE
  10. && (output.seekMap == null || !output.tracksEnded)) {
  11. readResult = extractor.read(input, positionHolder);
  12. }
  13. } finally {
  14. Util.closeQuietly(dataSource);
  15. }
  16. if (readResult == Extractor.RESULT_SEEK) {
  17. input = getExtractorInputFromPosition(positionHolder.position);
  18. readResult = Extractor.RESULT_CONTINUE;
  19. } else if (readResult == Extractor.RESULT_END_OF_INPUT) {
  20. throw new IOException("EOF encountered without seekmap");
  21. }
  22. if (output.seekMap != null) {
  23. return output.seekMap;
  24. }
  25. }
  26. }

代码示例来源:origin: google/ExoPlayer

  1. @SuppressWarnings("NonAtomicVolatileUpdate")
  2. @Override
  3. public void load() throws IOException, InterruptedException {
  4. DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  5. try {
  6. // Create and open the input.
  7. ExtractorInput input = new DefaultExtractorInput(dataSource,
  8. loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
  9. if (nextLoadPosition == 0) {
  10. extractorWrapper.init(
  11. /* trackOutputProvider= */ null,
  12. /* startTimeUs= */ C.TIME_UNSET,
  13. /* endTimeUs= */ C.TIME_UNSET);
  14. }
  15. // Load and decode the initialization data.
  16. try {
  17. Extractor extractor = extractorWrapper.extractor;
  18. int result = Extractor.RESULT_CONTINUE;
  19. while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
  20. result = extractor.read(input, DUMMY_POSITION_HOLDER);
  21. }
  22. Assertions.checkState(result != Extractor.RESULT_SEEK);
  23. } finally {
  24. nextLoadPosition = input.getPosition() - dataSpec.absoluteStreamPosition;
  25. }
  26. } finally {
  27. Util.closeQuietly(dataSource);
  28. }
  29. }

代码示例来源:origin: google/ExoPlayer

  1. private void readInputFileOnce(PsExtractor extractor, FakeExtractorOutput extractorOutput)
  2. throws IOException, InterruptedException {
  3. extractor.init(extractorOutput);
  4. int readResult = Extractor.RESULT_CONTINUE;
  5. ExtractorInput input = getExtractorInputFromPosition(0);
  6. while (readResult != Extractor.RESULT_END_OF_INPUT) {
  7. try {
  8. while (readResult == Extractor.RESULT_CONTINUE) {
  9. readResult = extractor.read(input, positionHolder);
  10. }
  11. } finally {
  12. Util.closeQuietly(dataSource);
  13. }
  14. if (readResult == Extractor.RESULT_SEEK) {
  15. input = getExtractorInputFromPosition(positionHolder.position);
  16. readResult = Extractor.RESULT_CONTINUE;
  17. }
  18. }
  19. }

代码示例来源:origin: google/ExoPlayer

  1. private void readInputFileOnce(
  2. TsExtractor extractor, FakeExtractorOutput extractorOutput, Uri fileUri)
  3. throws IOException, InterruptedException {
  4. extractor.init(extractorOutput);
  5. int readResult = Extractor.RESULT_CONTINUE;
  6. ExtractorInput input = TestUtil.getExtractorInputFromPosition(dataSource, 0, fileUri);
  7. while (readResult != Extractor.RESULT_END_OF_INPUT) {
  8. try {
  9. while (readResult == Extractor.RESULT_CONTINUE) {
  10. readResult = extractor.read(input, positionHolder);
  11. }
  12. } finally {
  13. Util.closeQuietly(dataSource);
  14. }
  15. if (readResult == Extractor.RESULT_SEEK) {
  16. input =
  17. TestUtil.getExtractorInputFromPosition(dataSource, positionHolder.position, fileUri);
  18. readResult = Extractor.RESULT_CONTINUE;
  19. }
  20. }
  21. }

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(dataSource);

代码示例来源:origin: google/ExoPlayer

  1. @SuppressWarnings("NonAtomicVolatileUpdate")
  2. @Override
  3. public void load() throws IOException, InterruptedException {
  4. DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  5. try {
  6. // Create and open the input.
  7. long length = dataSource.open(loadDataSpec);
  8. if (length != C.LENGTH_UNSET) {
  9. length += nextLoadPosition;
  10. }
  11. ExtractorInput extractorInput =
  12. new DefaultExtractorInput(dataSource, nextLoadPosition, length);
  13. BaseMediaChunkOutput output = getOutput();
  14. output.setSampleOffsetUs(0);
  15. TrackOutput trackOutput = output.track(0, trackType);
  16. trackOutput.format(sampleFormat);
  17. // Load the sample data.
  18. int result = 0;
  19. while (result != C.RESULT_END_OF_INPUT) {
  20. nextLoadPosition += result;
  21. result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
  22. }
  23. int sampleSize = (int) nextLoadPosition;
  24. trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  25. } finally {
  26. Util.closeQuietly(dataSource);
  27. }
  28. loadCompleted = true;
  29. }

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(dataSource);

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(dataSource);

代码示例来源:origin: google/ExoPlayer

  1. positionHolder.position = input.getPosition();
  2. Util.closeQuietly(dataSource);

代码示例来源:origin: google/ExoPlayer

  1. Util.closeQuietly(dataSource);

相关文章