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 android.adservices.common; 18 19 import android.annotation.NonNull; 20 import android.content.res.AssetFileDescriptor; 21 import android.os.ParcelFileDescriptor; 22 23 import com.android.adservices.LoggerFactory; 24 25 import java.io.DataInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.util.Objects; 29 import java.util.concurrent.ExecutorService; 30 31 /** 32 * Utility class used to set up the read and write pipes for the usage of reading pointers from 33 * shared memory. 34 * 35 * @hide 36 */ 37 public class AssetFileDescriptorUtil { 38 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 39 AssetFileDescriptorUtil()40 private AssetFileDescriptorUtil() {} 41 42 /** 43 * Creates a read and write pipe, writes the data in {@code buffer} into the write end, and 44 * returns the read end in the form of a {@link AssetFileDescriptor}. 45 * 46 * @throws IOException if an exception is encountered while creating or writing to the pipe 47 */ setupAssetFileDescriptorResponse( @onNull byte[] buffer, @NonNull ExecutorService executorService)48 public static AssetFileDescriptor setupAssetFileDescriptorResponse( 49 @NonNull byte[] buffer, @NonNull ExecutorService executorService) throws IOException { 50 Objects.requireNonNull(buffer); 51 Objects.requireNonNull(executorService); 52 53 ParcelFileDescriptor[] descriptors = ParcelFileDescriptor.createPipe(); 54 ParcelFileDescriptor writeDescriptor = descriptors[1]; 55 56 executorService.execute( 57 () -> { 58 try (FileOutputStream outputStream = 59 new FileOutputStream(writeDescriptor.getFileDescriptor())) { 60 outputStream.write(buffer); 61 } catch (IOException e) { 62 sLogger.e( 63 e, "Encountered IO Exception while writing byte array to stream."); 64 } 65 }); 66 return new AssetFileDescriptor(descriptors[0], 0, buffer.length); 67 } 68 69 /** 70 * Reads the content the {@link AssetFileDescriptor} points to into a buffer and returns the 71 * number of bytes read. 72 * 73 * @throws IOException if an exception is encountered while reading the content. 74 */ readAssetFileDescriptorIntoBuffer( @onNull AssetFileDescriptor assetFileDescriptor)75 public static byte[] readAssetFileDescriptorIntoBuffer( 76 @NonNull AssetFileDescriptor assetFileDescriptor) throws IOException { 77 Objects.requireNonNull(assetFileDescriptor); 78 79 byte[] result = new byte[(int) assetFileDescriptor.getLength()]; 80 81 try (DataInputStream inputStream = 82 new DataInputStream(assetFileDescriptor.createInputStream())) { 83 inputStream.readFully(result); 84 } 85 86 return result; 87 } 88 } 89