1 /* 2 * Copyright (C) 2020 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.server.integrity.parser; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.RandomAccessFile; 22 import java.nio.ByteBuffer; 23 24 /** An interface for random access objects like RandomAccessFile or byte arrays. */ 25 public abstract class RandomAccessObject { 26 27 /** See {@link RandomAccessFile#seek(long)}. */ seek(int position)28 public abstract void seek(int position) throws IOException; 29 30 /** See {@link RandomAccessFile#read()}. */ read()31 public abstract int read() throws IOException; 32 33 /** See {@link RandomAccessFile#read(byte[], int, int)}. */ read(byte[] bytes, int off, int len)34 public abstract int read(byte[] bytes, int off, int len) throws IOException; 35 36 /** See {@link RandomAccessFile#close()}. */ close()37 public abstract void close() throws IOException; 38 39 /** See {@link java.io.RandomAccessFile#length()}. */ length()40 public abstract int length(); 41 42 /** Static constructor from a file. */ ofFile(File file)43 public static RandomAccessObject ofFile(File file) throws IOException { 44 return new RandomAccessFileObject(file); 45 } 46 47 /** Static constructor from a byte array. */ ofBytes(byte[] bytes)48 public static RandomAccessObject ofBytes(byte[] bytes) { 49 return new RandomAccessByteArrayObject(bytes); 50 } 51 52 private static class RandomAccessFileObject extends RandomAccessObject { 53 private final RandomAccessFile mRandomAccessFile; 54 // We cache the length since File.length() invokes file IO. 55 private final int mLength; 56 RandomAccessFileObject(File file)57 RandomAccessFileObject(File file) throws IOException { 58 long length = file.length(); 59 if (length > Integer.MAX_VALUE) { 60 throw new IOException("Unsupported file size (too big) " + length); 61 } 62 63 mRandomAccessFile = new RandomAccessFile(file, /* mode= */ "r"); 64 mLength = (int) length; 65 } 66 67 @Override seek(int position)68 public void seek(int position) throws IOException { 69 mRandomAccessFile.seek(position); 70 } 71 72 @Override read()73 public int read() throws IOException { 74 return mRandomAccessFile.read(); 75 } 76 77 @Override read(byte[] bytes, int off, int len)78 public int read(byte[] bytes, int off, int len) throws IOException { 79 return mRandomAccessFile.read(bytes, off, len); 80 } 81 82 @Override close()83 public void close() throws IOException { 84 mRandomAccessFile.close(); 85 } 86 87 @Override length()88 public int length() { 89 return mLength; 90 } 91 } 92 93 private static class RandomAccessByteArrayObject extends RandomAccessObject { 94 95 private final ByteBuffer mBytes; 96 RandomAccessByteArrayObject(byte[] bytes)97 RandomAccessByteArrayObject(byte[] bytes) { 98 mBytes = ByteBuffer.wrap(bytes); 99 } 100 101 @Override seek(int position)102 public void seek(int position) throws IOException { 103 mBytes.position(position); 104 } 105 106 @Override read()107 public int read() throws IOException { 108 if (!mBytes.hasRemaining()) { 109 return -1; 110 } 111 112 return mBytes.get() & 0xFF; 113 } 114 115 @Override read(byte[] bytes, int off, int len)116 public int read(byte[] bytes, int off, int len) throws IOException { 117 int bytesToCopy = Math.min(len, mBytes.remaining()); 118 if (bytesToCopy <= 0) { 119 return 0; 120 } 121 mBytes.get(bytes, off, len); 122 return bytesToCopy; 123 } 124 125 @Override close()126 public void close() throws IOException {} 127 128 @Override length()129 public int length() { 130 return mBytes.capacity(); 131 } 132 } 133 } 134