• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pump.util;
18 
19 import android.content.res.AssetFileDescriptor;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.WorkerThread;
24 
25 import java.io.ByteArrayOutputStream;
26 import java.io.Closeable;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 
33 @WorkerThread
34 public final class IoUtils {
35     private static final String TAG = Clog.tag(IoUtils.class);
36 
IoUtils()37     private IoUtils() { }
38 
readFromFile(@onNull File file)39     public static @NonNull byte[] readFromFile(@NonNull File file) throws IOException  {
40         InputStream inputStream = new FileInputStream(file);
41         try {
42             return readFromStream(inputStream);
43         } finally {
44             close(inputStream);
45         }
46     }
47 
readFromAssetFileDescriptor( @onNull AssetFileDescriptor assetFileDescriptor)48     public static @NonNull byte[] readFromAssetFileDescriptor(
49             @NonNull AssetFileDescriptor assetFileDescriptor) throws IOException {
50         InputStream inputStream = assetFileDescriptor.createInputStream();
51         try {
52             return readFromStream(inputStream);
53         } finally {
54             close(inputStream);
55         }
56     }
57 
readFromStream(@onNull InputStream inputStream)58     public static @NonNull byte[] readFromStream(@NonNull InputStream inputStream)
59             throws IOException {
60         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
61         try {
62             int num;
63             byte[] buf = new byte[16384];
64             while ((num = inputStream.read(buf, 0, buf.length)) >= 0) {
65                 buffer.write(buf, 0, num);
66             }
67             return buffer.toByteArray();
68         } finally {
69             close(buffer);
70         }
71     }
72 
writeToStream(@onNull OutputStream outputStream, @NonNull byte[] buffer)73     public static void writeToStream(@NonNull OutputStream outputStream, @NonNull byte[] buffer)
74             throws IOException {
75         outputStream.write(buffer);
76         outputStream.flush();
77     }
78 
close(@ullable Closeable closeable)79     public static void close(@Nullable Closeable closeable) {
80         if (closeable == null) return;
81         try {
82             closeable.close();
83         } catch (IOException e) {
84             Clog.w(TAG, "Failed to close '" + closeable + "'", e);
85         }
86     }
87 }
88