1 /* 2 * Copyright (C) 2016 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.apksig.util; 18 19 import com.android.apksig.internal.util.ByteBufferDataSource; 20 import com.android.apksig.internal.util.RandomAccessFileDataSource; 21 import java.io.RandomAccessFile; 22 import java.nio.ByteBuffer; 23 24 /** 25 * Utility methods for working with {@link DataSource} abstraction. 26 */ 27 public abstract class DataSources { 28 private DataSources() {} 29 30 /** 31 * Returns a {@link DataSource} backed by the provided {@link ByteBuffer}. The data source 32 * represents the data contained between the position and limit of the buffer. Changes to the 33 * buffer's contents will be visible in the data source. 34 */ 35 public static DataSource asDataSource(ByteBuffer buffer) { 36 if (buffer == null) { 37 throw new NullPointerException(); 38 } 39 return new ByteBufferDataSource(buffer); 40 } 41 42 /** 43 * Returns a {@link DataSource} backed by the provided {@link RandomAccessFile}. Changes to the 44 * file, including changes to size of file, will be visible in the data source. 45 */ 46 public static DataSource asDataSource(RandomAccessFile file) { 47 if (file == null) { 48 throw new NullPointerException(); 49 } 50 return new RandomAccessFileDataSource(file); 51 } 52 53 /** 54 * Returns a {@link DataSource} backed by the provided region of the {@link RandomAccessFile}. 55 * Changes to the file will be visible in the data source. 56 */ 57 public static DataSource asDataSource(RandomAccessFile file, long offset, long size) { 58 if (file == null) { 59 throw new NullPointerException(); 60 } 61 return new RandomAccessFileDataSource(file, offset, size); 62 } 63 } 64