1 /*
2  * Copyright (C) 2023 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.federatedcompute.services.common;
18 
19 import android.os.ParcelFileDescriptor;
20 
21 import com.android.federatedcompute.internal.util.LogUtil;
22 
23 import java.io.BufferedInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 
31 /** Utils related to {@link File} and {@link ParcelFileDescriptor}. */
32 public class FileUtils {
33     private static final String TAG = FileUtils.class.getSimpleName();
34 
35     private static final int BUFFER_SIZE = 1024;
36 
37     /** Create {@link ParcelFileDescriptor} based on the input file. */
createTempFileDescriptor(String fileName, int mode)38     public static ParcelFileDescriptor createTempFileDescriptor(String fileName, int mode) {
39         ParcelFileDescriptor fileDescriptor;
40         try {
41             fileDescriptor = ParcelFileDescriptor.open(new File(fileName), mode);
42         } catch (IOException e) {
43             LogUtil.e(TAG, e, "Failed to createTempFileDescriptor %s", fileName);
44             throw new RuntimeException(e);
45         }
46         return fileDescriptor;
47     }
48 
49     /** Create a temporary file based on provided name and extension. */
createTempFile(String name, String extension)50     public static String createTempFile(String name, String extension) {
51         String fileName;
52         try {
53             File tempFile = File.createTempFile(name, extension);
54             fileName = tempFile.getAbsolutePath();
55         } catch (IOException e) {
56             throw new RuntimeException(e);
57         }
58         return fileName;
59     }
60 
61     /** Write the provided data to the file. */
writeToFile(String fileName, byte[] data)62     public static void writeToFile(String fileName, byte[] data) throws IOException {
63         FileOutputStream out = new FileOutputStream(fileName);
64         out.write(data);
65         out.close();
66     }
67 
68     /** Read the input file content to a byte array. */
readFileAsByteArray(String filePath)69     public static byte[] readFileAsByteArray(String filePath) throws IOException {
70         File file = new File(filePath);
71         long fileLength = file.length();
72         ByteArrayOutputStream outputStream = new ByteArrayOutputStream((int) fileLength);
73         try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
74             byte[] buffer = new byte[BUFFER_SIZE];
75             for (int len = inputStream.read(buffer); len > 0; len = inputStream.read(buffer)) {
76                 outputStream.write(buffer, 0, len);
77             }
78         } catch (IOException e) {
79             LogUtil.e(TAG, e, "Failed to read the content of binary file %s", filePath);
80             throw e;
81         }
82         return outputStream.toByteArray();
83     }
84 
85     /** Read the content from a file descriptor to a byte array. */
readFileDescriptorAsByteArray(ParcelFileDescriptor fd)86     public static byte[] readFileDescriptorAsByteArray(ParcelFileDescriptor fd) {
87         InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(fd);
88         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
89 
90         byte[] buffer = new byte[1024];
91         int bytesRead;
92         try {
93             while ((bytesRead = inputStream.read(buffer)) != -1) {
94                 outputStream.write(buffer, 0, bytesRead);
95             }
96             inputStream.close();
97         } catch (IOException e) {
98             LogUtil.e(TAG, e, "Failed to read the content of binary file %d", fd.getFd());
99         }
100         return outputStream.toByteArray();
101     }
102 }
103