• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.launcher3.util;
18 
19 import android.os.FileUtils;
20 import android.util.Log;
21 
22 import com.android.launcher3.config.FeatureFlags;
23 
24 import java.io.ByteArrayOutputStream;
25 import java.io.Closeable;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 
32 /**
33  * Supports various IO utility functions
34  */
35 public class IOUtils {
36 
37     private static final int BUF_SIZE = 0x1000; // 4K
38     private static final String TAG = "IOUtils";
39 
toByteArray(File file)40     public static byte[] toByteArray(File file) throws IOException {
41         try (InputStream in = new FileInputStream(file)) {
42             return toByteArray(in);
43         }
44     }
45 
toByteArray(InputStream in)46     public static byte[] toByteArray(InputStream in) throws IOException {
47         ByteArrayOutputStream out = new ByteArrayOutputStream();
48         copy(in, out);
49         return out.toByteArray();
50     }
51 
copy(InputStream from, OutputStream to)52     public static long copy(InputStream from, OutputStream to) throws IOException {
53         return FileUtils.copy(from, to);
54     }
55 
closeSilently(Closeable c)56     public static void closeSilently(Closeable c) {
57         if (c != null) {
58             try {
59                 c.close();
60             } catch (IOException e) {
61                 if (FeatureFlags.IS_STUDIO_BUILD) {
62                     Log.d(TAG, "Error closing", e);
63                 }
64             }
65         }
66     }
67 }
68