Home | History | Annotate | Download | only in ch
      1 /*
      2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package sun.nio.ch;
     27 
     28 import dalvik.system.BlockGuard;
     29 import dalvik.system.SocketTagger;
     30 
     31 import java.io.*;
     32 import java.net.SocketException;
     33 
     34 class FileDispatcherImpl extends FileDispatcher
     35 {
     36 
     37     FileDispatcherImpl(boolean append) {
     38         /* append is ignored */
     39     }
     40 
     41     FileDispatcherImpl() {
     42     }
     43 
     44     int read(FileDescriptor fd, long address, int len) throws IOException {
     45         BlockGuard.getThreadPolicy().onReadFromDisk();
     46         return read0(fd, address, len);
     47     }
     48 
     49     int pread(FileDescriptor fd, long address, int len, long position)
     50         throws IOException
     51     {
     52         BlockGuard.getThreadPolicy().onReadFromDisk();
     53         return pread0(fd, address, len, position);
     54     }
     55 
     56     long readv(FileDescriptor fd, long address, int len) throws IOException {
     57         BlockGuard.getThreadPolicy().onReadFromDisk();
     58         return readv0(fd, address, len);
     59     }
     60 
     61     int write(FileDescriptor fd, long address, int len) throws IOException {
     62         BlockGuard.getThreadPolicy().onWriteToDisk();
     63         return write0(fd, address, len);
     64     }
     65 
     66     int pwrite(FileDescriptor fd, long address, int len, long position)
     67         throws IOException
     68     {
     69         BlockGuard.getThreadPolicy().onWriteToDisk();
     70         return pwrite0(fd, address, len, position);
     71     }
     72 
     73     long writev(FileDescriptor fd, long address, int len)
     74         throws IOException
     75     {
     76         BlockGuard.getThreadPolicy().onWriteToDisk();
     77         return writev0(fd, address, len);
     78     }
     79 
     80     int force(FileDescriptor fd, boolean metaData) throws IOException {
     81         BlockGuard.getThreadPolicy().onWriteToDisk();
     82         return force0(fd, metaData);
     83     }
     84 
     85     int truncate(FileDescriptor fd, long size) throws IOException {
     86         BlockGuard.getThreadPolicy().onWriteToDisk();
     87         return truncate0(fd, size);
     88     }
     89 
     90     long size(FileDescriptor fd) throws IOException {
     91         BlockGuard.getThreadPolicy().onReadFromDisk();
     92         return size0(fd);
     93     }
     94 
     95     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
     96              boolean shared) throws IOException
     97     {
     98         BlockGuard.getThreadPolicy().onWriteToDisk();
     99         return lock0(fd, blocking, pos, size, shared);
    100     }
    101 
    102     void release(FileDescriptor fd, long pos, long size) throws IOException {
    103         BlockGuard.getThreadPolicy().onWriteToDisk();
    104         release0(fd, pos, size);
    105     }
    106 
    107     void close(FileDescriptor fd) throws IOException {
    108         close0(fd);
    109     }
    110 
    111     void preClose(FileDescriptor fd) throws IOException {
    112         preCloseImpl(fd);
    113     }
    114 
    115     static void preCloseImpl(FileDescriptor fd) throws IOException {
    116         if (fd.isSocket$()) {
    117             // Always untag sockets before the preClose. The file descriptor will describe
    118             // a different file (/dev/null) after preClose0.
    119             try {
    120                 SocketTagger.get().untag(fd);
    121             } catch (SocketException ignored) {
    122             }
    123         }
    124 
    125         preClose0(fd);
    126     }
    127 
    128     FileDescriptor duplicateForMapping(FileDescriptor fd) {
    129         // file descriptor not required for mapping operations; okay
    130         // to return invalid file descriptor.
    131         return new FileDescriptor();
    132     }
    133 
    134     // -- Native methods --
    135 
    136     static native int read0(FileDescriptor fd, long address, int len)
    137         throws IOException;
    138 
    139     static native int pread0(FileDescriptor fd, long address, int len,
    140                              long position) throws IOException;
    141 
    142     static native long readv0(FileDescriptor fd, long address, int len)
    143         throws IOException;
    144 
    145     static native int write0(FileDescriptor fd, long address, int len)
    146         throws IOException;
    147 
    148     static native int pwrite0(FileDescriptor fd, long address, int len,
    149                              long position) throws IOException;
    150 
    151     static native long writev0(FileDescriptor fd, long address, int len)
    152         throws IOException;
    153 
    154     static native int force0(FileDescriptor fd, boolean metaData)
    155         throws IOException;
    156 
    157     static native int truncate0(FileDescriptor fd, long size)
    158         throws IOException;
    159 
    160     static native long size0(FileDescriptor fd) throws IOException;
    161 
    162     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
    163                             long size, boolean shared) throws IOException;
    164 
    165     static native void release0(FileDescriptor fd, long pos, long size)
    166         throws IOException;
    167 
    168     static native void close0(FileDescriptor fd) throws IOException;
    169 
    170     private static native void preClose0(FileDescriptor fd) throws IOException;
    171 
    172     static native void closeIntFD(int fd) throws IOException;
    173 
    174 }
    175