BlockGuardOs.java revision 70c820401677ca251ad09ac64cc23c760764e75d
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.net.InetAddress; 22import java.nio.ByteBuffer; 23import static libcore.io.OsConstants.*; 24 25/** 26 * Informs BlockGuard of any activity it should be aware of. 27 */ 28public class BlockGuardOs extends ForwardingOs { 29 public BlockGuardOs(Os os) { 30 super(os); 31 } 32 33 @Override public void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException { 34 BlockGuard.getThreadPolicy().onNetwork(); 35 os.connect(fd, address, port); 36 } 37 38 @Override public void fdatasync(FileDescriptor fd) throws ErrnoException { 39 BlockGuard.getThreadPolicy().onWriteToDisk(); 40 os.fdatasync(fd); 41 } 42 43 @Override public void fsync(FileDescriptor fd) throws ErrnoException { 44 BlockGuard.getThreadPolicy().onWriteToDisk(); 45 os.fsync(fd); 46 } 47 48 @Override public void ftruncate(FileDescriptor fd, long length) throws ErrnoException { 49 BlockGuard.getThreadPolicy().onWriteToDisk(); 50 os.ftruncate(fd, length); 51 } 52 53 @Override public FileDescriptor open(String path, int flags, int mode) throws ErrnoException { 54 BlockGuard.getThreadPolicy().onReadFromDisk(); 55 if ((mode & O_ACCMODE) != O_RDONLY) { 56 BlockGuard.getThreadPolicy().onWriteToDisk(); 57 } 58 return os.open(path, flags, mode); 59 } 60 61 @Override public int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException { 62 BlockGuard.getThreadPolicy().onNetwork(); 63 return os.poll(fds, timeoutMs); 64 } 65 66 @Override public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 67 BlockGuard.getThreadPolicy().onReadFromDisk(); 68 return os.read(fd, buffer); 69 } 70 71 @Override public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 72 BlockGuard.getThreadPolicy().onReadFromDisk(); 73 return os.read(fd, bytes, byteOffset, byteCount); 74 } 75 76 @Override public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException { 77 BlockGuard.getThreadPolicy().onReadFromDisk(); 78 return os.readv(fd, buffers, offsets, byteCounts); 79 } 80 81 @Override public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 82 BlockGuard.getThreadPolicy().onWriteToDisk(); 83 return os.write(fd, buffer); 84 } 85 86 @Override public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 87 BlockGuard.getThreadPolicy().onWriteToDisk(); 88 return os.write(fd, bytes, byteOffset, byteCount); 89 } 90 91 @Override public int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException { 92 BlockGuard.getThreadPolicy().onWriteToDisk(); 93 return os.writev(fd, buffers, offsets, byteCounts); 94 } 95} 96