1 /* 2 * Copyright (C) 2024 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.ondevicepersonalization.services.util; 18 19 import android.content.Context; 20 import android.os.ParcelFileDescriptor; 21 22 import com.android.ondevicepersonalization.internal.util.LoggerFactory; 23 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.nio.ByteBuffer; 29 import java.nio.channels.FileChannel; 30 31 /** Contains static I/O-related utility methods. */ 32 public class IoUtils { 33 34 private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger(); 35 private static final String TAG = IoUtils.class.getSimpleName(); 36 IoUtils()37 private IoUtils() {} 38 39 /** Get content as Bytebuffer by reading from {@link ParcelFileDescriptor}. */ getByteBufferFromFd(ParcelFileDescriptor fileDescriptor)40 public static ByteBuffer getByteBufferFromFd(ParcelFileDescriptor fileDescriptor) { 41 try { 42 FileInputStream inputStream = 43 new ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor); 44 FileChannel fileChannel = inputStream.getChannel(); 45 return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); 46 } catch (IOException e) { 47 sLogger.e(TAG + ": Failed to read file descriptor to ByteBuffer.", e); 48 return null; 49 } 50 } 51 52 /** Create {@link ParcelFileDescriptor} based on the input file. */ createFileDescriptor(String fileName, int mode)53 public static ParcelFileDescriptor createFileDescriptor(String fileName, int mode) { 54 ParcelFileDescriptor fileDescriptor; 55 try { 56 fileDescriptor = ParcelFileDescriptor.open(new File(fileName), mode); 57 } catch (IOException e) { 58 sLogger.e(TAG + ": Failed to createTempFileDescriptor %s", fileName); 59 throw new RuntimeException(e); 60 } 61 return fileDescriptor; 62 } 63 64 /** Write the provided data to new created file. */ writeToFile(Context context, String fileName, byte[] data)65 public static String writeToFile(Context context, String fileName, byte[] data) { 66 try { 67 File file = new File(context.getDataDir(), fileName); 68 String filePath = file.getAbsolutePath(); 69 FileOutputStream out = new FileOutputStream(file); 70 out.write(data); 71 out.close(); 72 return filePath; 73 } catch (IOException e) { 74 sLogger.e(e, TAG + e.getMessage()); 75 return null; 76 } 77 } 78 79 /** Create a temporary file based on provided name and extension. */ writeToTempFile(String name, byte[] data)80 public static String writeToTempFile(String name, byte[] data) { 81 try { 82 File tempFile = File.createTempFile(name, ""); 83 String filePath = tempFile.getAbsolutePath(); 84 FileOutputStream out = new FileOutputStream(tempFile); 85 out.write(data); 86 out.close(); 87 return filePath; 88 } catch (IOException e) { 89 throw new RuntimeException(e); 90 } 91 } 92 } 93