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