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.content.Context;
20 import android.util.Log;
21 
22 import com.android.launcher3.Utilities;
23 import com.android.launcher3.config.FeatureFlags;
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.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.UUID;
34 
35 /**
36  * Supports various IO utility functions
37  */
38 public class IOUtils {
39 
40     private static final int BUF_SIZE = 0x1000; // 4K
41     private static final String TAG = "IOUtils";
42 
toByteArray(File file)43     public static byte[] toByteArray(File file) throws IOException {
44         try (InputStream in = new FileInputStream(file)) {
45             return toByteArray(in);
46         }
47     }
48 
toByteArray(InputStream in)49     public static byte[] toByteArray(InputStream in) throws IOException {
50         ByteArrayOutputStream out = new ByteArrayOutputStream();
51         copy(in, out);
52         return out.toByteArray();
53     }
54 
copy(InputStream from, OutputStream to)55     public static long copy(InputStream from, OutputStream to) throws IOException {
56         byte[] buf = new byte[BUF_SIZE];
57         long total = 0;
58         int r;
59         while ((r = from.read(buf)) != -1) {
60             to.write(buf, 0, r);
61             total += r;
62         }
63         return total;
64     }
65 
66     /**
67      * Utility method to debug binary data
68      */
createTempFile(Context context, byte[] data)69     public static String createTempFile(Context context, byte[] data) {
70         if (!FeatureFlags.IS_DOGFOOD_BUILD) {
71             throw new IllegalStateException("Method only allowed in development mode");
72         }
73 
74         String name = UUID.randomUUID().toString();
75         File file = new File(context.getCacheDir(), name);
76         try (FileOutputStream fo = new FileOutputStream(file)) {
77             fo.write(data);
78             fo.flush();
79         } catch (Exception e) {
80             throw new RuntimeException(e);
81         }
82         return file.getAbsolutePath();
83     }
84 
closeSilently(Closeable c)85     public static void closeSilently(Closeable c) {
86         if (c != null) {
87             try {
88                 c.close();
89             } catch (IOException e) {
90                 if (FeatureFlags.IS_DOGFOOD_BUILD) {
91                     Log.d(TAG, "Error closing", e);
92                 }
93             }
94         }
95     }
96 }
97