BlockGuardOs.java revision 78c7cc547101002b9f9043cf3845970719d1bda8
1/* 2 * Copyright (C) 2011 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 dalvik.system.BlockGuard; 20import java.io.FileDescriptor; 21import java.nio.ByteBuffer; 22import static libcore.io.OsConstants.*; 23 24/** 25 * Informs BlockGuard of any activity it should be aware of. 26 */ 27public class BlockGuardOs extends ForwardingOs { 28 public BlockGuardOs(Os os) { 29 super(os); 30 } 31 32 public void fdatasync(FileDescriptor fd) throws ErrnoException { 33 BlockGuard.getThreadPolicy().onWriteToDisk(); 34 os.fdatasync(fd); 35 } 36 37 public void fsync(FileDescriptor fd) throws ErrnoException { 38 BlockGuard.getThreadPolicy().onWriteToDisk(); 39 os.fsync(fd); 40 } 41 42 public void ftruncate(FileDescriptor fd, long length) throws ErrnoException { 43 BlockGuard.getThreadPolicy().onWriteToDisk(); 44 os.ftruncate(fd, length); 45 } 46 47 public FileDescriptor open(String path, int flags, int mode) throws ErrnoException { 48 BlockGuard.getThreadPolicy().onReadFromDisk(); 49 if ((mode & O_ACCMODE) != O_RDONLY) { 50 BlockGuard.getThreadPolicy().onWriteToDisk(); 51 } 52 return os.open(path, flags, mode); 53 } 54 55 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 56 BlockGuard.getThreadPolicy().onReadFromDisk(); 57 return os.read(fd, buffer); 58 } 59 60 public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 61 BlockGuard.getThreadPolicy().onReadFromDisk(); 62 return os.read(fd, bytes, byteOffset, byteCount); 63 } 64 65 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 66 BlockGuard.getThreadPolicy().onWriteToDisk(); 67 return os.write(fd, buffer); 68 } 69 70 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 71 BlockGuard.getThreadPolicy().onWriteToDisk(); 72 return os.write(fd, bytes, byteOffset, byteCount); 73 } 74} 75