1/* 2 * Copyright (C) 2010 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 17package libcore.io; 18 19import android.system.ErrnoException; 20import java.io.FileDescriptor; 21import java.io.IOException; 22import java.io.RandomAccessFile; 23import java.nio.ByteOrder; 24import java.nio.channels.FileChannel; 25import java.nio.NioUtils; 26import libcore.io.Libcore; 27import libcore.io.Memory; 28import static android.system.OsConstants.*; 29 30/** 31 * A memory-mapped file. Use {@link #mmapRO} to map a file, {@link #close} to unmap a file, 32 * and either {@link #bigEndianIterator} or {@link #littleEndianIterator} to get a seekable 33 * {@link BufferIterator} over the mapped data. This class is not thread safe. 34 */ 35public final class MemoryMappedFile implements AutoCloseable { 36 private boolean closed; 37 private final long address; 38 private final int size; 39 40 /** Public for layoutlib only. */ 41 public MemoryMappedFile(long address, long size) { 42 this.address = address; 43 // For simplicity when bounds checking, only sizes up to Integer.MAX_VALUE are supported. 44 if (size < 0 || size > Integer.MAX_VALUE) { 45 throw new IllegalArgumentException("Unsupported file size=" + size); 46 } 47 this.size = (int) size; 48 } 49 50 /** 51 * Use this to mmap the whole file read-only. 52 */ 53 public static MemoryMappedFile mmapRO(String path) throws ErrnoException { 54 FileDescriptor fd = Libcore.os.open(path, O_RDONLY, 0); 55 try { 56 long size = Libcore.os.fstat(fd).st_size; 57 long address = Libcore.os.mmap(0L, size, PROT_READ, MAP_SHARED, fd, 0); 58 return new MemoryMappedFile(address, size); 59 } finally { 60 Libcore.os.close(fd); 61 } 62 } 63 64 /** 65 * Unmaps this memory-mapped file using munmap(2). This is a no-op if close has already been 66 * called. Note that this class does <i>not</i> use finalization; you must call {@code close} 67 * yourself. 68 * 69 * Calling this method invalidates any iterators over this {@code MemoryMappedFile}. It is an 70 * error to use such an iterator after calling {@code close}. 71 */ 72 public void close() throws ErrnoException { 73 if (!closed) { 74 closed = true; 75 Libcore.os.munmap(address, size); 76 } 77 } 78 79 public boolean isClosed() { 80 return closed; 81 } 82 83 /** 84 * Returns a new iterator that treats the mapped data as big-endian. 85 */ 86 public BufferIterator bigEndianIterator() { 87 return new NioBufferIterator( 88 this, address, size, ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN); 89 } 90 91 /** 92 * Returns a new iterator that treats the mapped data as little-endian. 93 */ 94 public BufferIterator littleEndianIterator() { 95 return new NioBufferIterator( 96 this, this.address, this.size, ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN); 97 } 98 99 /** Throws {@link IllegalStateException} if the file is closed. */ 100 void checkNotClosed() { 101 if (closed) { 102 throw new IllegalStateException("MemoryMappedFile is closed"); 103 } 104 } 105 106 /** 107 * Returns the size in bytes of the memory-mapped region. 108 */ 109 public int size() { 110 checkNotClosed(); 111 return size; 112 } 113} 114