1 /* 2 * Copyright (C) 2014 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 libcore.io; 18 19 import com.android.layoutlib.bridge.impl.DelegateManager; 20 import com.android.layoutlib.bridge.libcore.io.BridgeBufferIterator; 21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 22 23 import android.system.ErrnoException; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.io.RandomAccessFile; 28 import java.nio.ByteOrder; 29 import java.nio.MappedByteBuffer; 30 import java.nio.channels.FileChannel.MapMode; 31 import java.util.HashMap; 32 import java.util.Map; 33 34 /** 35 * Delegate used to provide alternate implementation of select methods of {@link MemoryMappedFile}. 36 */ 37 public class MemoryMappedFile_Delegate { 38 39 private static final DelegateManager<MemoryMappedFile_Delegate> sManager = 40 new DelegateManager<>(MemoryMappedFile_Delegate.class); 41 42 private static final Map<MemoryMappedFile, Long> sMemoryMappedFileMap = new HashMap<>(); 43 44 private final MappedByteBuffer mMappedByteBuffer; 45 private final long mSize; 46 47 /** Path on the target device where the data file is available. */ 48 private static final String TARGET_PATH = System.getenv("ANDROID_ROOT") + "/usr/share/zoneinfo"; 49 /** Path on the host (inside the SDK) where the data files are available. */ 50 private static File sRootPath; 51 52 @LayoutlibDelegate mmapRO(String path)53 static MemoryMappedFile mmapRO(String path) throws ErrnoException { 54 if (!path.startsWith(TARGET_PATH)) { 55 throw new ErrnoException("Custom timezone data files are not supported.", 1); 56 } 57 if (sRootPath == null) { 58 throw new ErrnoException("Bridge has not been initialized properly.", 1); 59 } 60 path = path.substring(TARGET_PATH.length()); 61 try { 62 File f = new File(sRootPath, path); 63 if (!f.exists()) { 64 throw new ErrnoException("File not found: " + f.getPath(), 1); 65 } 66 try (RandomAccessFile file = new RandomAccessFile(f, "r")) { 67 long size = file.length(); 68 MemoryMappedFile_Delegate newDelegate = new MemoryMappedFile_Delegate(file); 69 long filePointer = file.getFilePointer(); 70 MemoryMappedFile mmFile = new MemoryMappedFile(filePointer, size); 71 long delegateIndex = sManager.addNewDelegate(newDelegate); 72 sMemoryMappedFileMap.put(mmFile, delegateIndex); 73 return mmFile; 74 } 75 } catch (IOException e) { 76 throw new ErrnoException("mmapRO", 1, e); 77 } 78 } 79 80 @LayoutlibDelegate close(MemoryMappedFile thisFile)81 static void close(MemoryMappedFile thisFile) throws ErrnoException { 82 Long index = sMemoryMappedFileMap.get(thisFile); 83 if (index != null) { 84 sMemoryMappedFileMap.remove(thisFile); 85 sManager.removeJavaReferenceFor(index); 86 } 87 } 88 89 @LayoutlibDelegate bigEndianIterator(MemoryMappedFile file)90 static BufferIterator bigEndianIterator(MemoryMappedFile file) { 91 MemoryMappedFile_Delegate delegate = getDelegate(file); 92 return new BridgeBufferIterator(delegate.mSize, delegate.mMappedByteBuffer.duplicate()); 93 } 94 95 // TODO: implement littleEndianIterator() 96 MemoryMappedFile_Delegate(RandomAccessFile file)97 private MemoryMappedFile_Delegate(RandomAccessFile file) throws IOException { 98 mSize = file.length(); 99 // It's weird that map() takes size as long, but returns MappedByteBuffer which uses an int 100 // to store the marker to the position. 101 mMappedByteBuffer = file.getChannel().map(MapMode.READ_ONLY, 0, mSize); 102 assert mMappedByteBuffer.order() == ByteOrder.BIG_ENDIAN; 103 } 104 setDataDir(File path)105 public static void setDataDir(File path) { 106 sRootPath = path; 107 } 108 getDelegate(MemoryMappedFile file)109 private static MemoryMappedFile_Delegate getDelegate(MemoryMappedFile file) { 110 Long index = sMemoryMappedFileMap.get(file); 111 return index == null ? null : sManager.getDelegate(index); 112 } 113 114 } 115