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 
FileDispatcherImpl(boolean append)36     FileDispatcherImpl(boolean append) {
37         /* append is ignored */
38     }
39 
FileDispatcherImpl()40     FileDispatcherImpl() {
41     }
42 
read(FileDescriptor fd, long address, int len)43     int read(FileDescriptor fd, long address, int len) throws IOException {
44         BlockGuard.getThreadPolicy().onReadFromDisk();
45         return read0(fd, address, len);
46     }
47 
pread(FileDescriptor fd, long address, int len, long position)48     int pread(FileDescriptor fd, long address, int len, long position)
49         throws IOException
50     {
51         BlockGuard.getThreadPolicy().onReadFromDisk();
52         return pread0(fd, address, len, position);
53     }
54 
readv(FileDescriptor fd, long address, int len)55     long readv(FileDescriptor fd, long address, int len) throws IOException {
56         BlockGuard.getThreadPolicy().onReadFromDisk();
57         return readv0(fd, address, len);
58     }
59 
write(FileDescriptor fd, long address, int len)60     int write(FileDescriptor fd, long address, int len) throws IOException {
61         BlockGuard.getThreadPolicy().onWriteToDisk();
62         return write0(fd, address, len);
63     }
64 
pwrite(FileDescriptor fd, long address, int len, long position)65     int pwrite(FileDescriptor fd, long address, int len, long position)
66         throws IOException
67     {
68         BlockGuard.getThreadPolicy().onWriteToDisk();
69         return pwrite0(fd, address, len, position);
70     }
71 
writev(FileDescriptor fd, long address, int len)72     long writev(FileDescriptor fd, long address, int len)
73         throws IOException
74     {
75         BlockGuard.getThreadPolicy().onWriteToDisk();
76         return writev0(fd, address, len);
77     }
78 
force(FileDescriptor fd, boolean metaData)79     int force(FileDescriptor fd, boolean metaData) throws IOException {
80         BlockGuard.getThreadPolicy().onWriteToDisk();
81         return force0(fd, metaData);
82     }
83 
truncate(FileDescriptor fd, long size)84     int truncate(FileDescriptor fd, long size) throws IOException {
85         BlockGuard.getThreadPolicy().onWriteToDisk();
86         return truncate0(fd, size);
87     }
88 
size(FileDescriptor fd)89     long size(FileDescriptor fd) throws IOException {
90         BlockGuard.getThreadPolicy().onReadFromDisk();
91         return size0(fd);
92     }
93 
lock(FileDescriptor fd, boolean blocking, long pos, long size, boolean shared)94     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
95              boolean shared) throws IOException
96     {
97         BlockGuard.getThreadPolicy().onWriteToDisk();
98         return lock0(fd, blocking, pos, size, shared);
99     }
100 
release(FileDescriptor fd, long pos, long size)101     void release(FileDescriptor fd, long pos, long size) throws IOException {
102         BlockGuard.getThreadPolicy().onWriteToDisk();
103         release0(fd, pos, size);
104     }
105 
close(FileDescriptor fd)106     void close(FileDescriptor fd) throws IOException {
107         close0(fd);
108     }
109 
preClose(FileDescriptor fd)110     void preClose(FileDescriptor fd) throws IOException {
111         preCloseImpl(fd);
112     }
113 
preCloseImpl(FileDescriptor fd)114     static void preCloseImpl(FileDescriptor fd) throws IOException {
115         if (fd.isSocket$()) {
116             // Always untag sockets before the preClose. The file descriptor will describe
117             // a different file (/dev/null) after preClose0.
118             try {
119                 SocketTagger.get().untag(fd);
120             } catch (SocketException ignored) {
121             }
122         }
123 
124         preClose0(fd);
125     }
126 
duplicateForMapping(FileDescriptor fd)127     FileDescriptor duplicateForMapping(FileDescriptor fd) {
128         // file descriptor not required for mapping operations; okay
129         // to return invalid file descriptor.
130         return new FileDescriptor();
131     }
132 
canTransferToDirectly(java.nio.channels.SelectableChannel sc)133     boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
134         return true;
135     }
136 
transferToDirectlyNeedsPositionLock()137     boolean transferToDirectlyNeedsPositionLock() {
138         return false;
139     }
140 
141     // -- Native methods --
142 
read0(FileDescriptor fd, long address, int len)143     static native int read0(FileDescriptor fd, long address, int len)
144         throws IOException;
145 
pread0(FileDescriptor fd, long address, int len, long position)146     static native int pread0(FileDescriptor fd, long address, int len,
147                              long position) throws IOException;
148 
readv0(FileDescriptor fd, long address, int len)149     static native long readv0(FileDescriptor fd, long address, int len)
150         throws IOException;
151 
write0(FileDescriptor fd, long address, int len)152     static native int write0(FileDescriptor fd, long address, int len)
153         throws IOException;
154 
pwrite0(FileDescriptor fd, long address, int len, long position)155     static native int pwrite0(FileDescriptor fd, long address, int len,
156                              long position) throws IOException;
157 
writev0(FileDescriptor fd, long address, int len)158     static native long writev0(FileDescriptor fd, long address, int len)
159         throws IOException;
160 
force0(FileDescriptor fd, boolean metaData)161     static native int force0(FileDescriptor fd, boolean metaData)
162         throws IOException;
163 
truncate0(FileDescriptor fd, long size)164     static native int truncate0(FileDescriptor fd, long size)
165         throws IOException;
166 
size0(FileDescriptor fd)167     static native long size0(FileDescriptor fd) throws IOException;
168 
lock0(FileDescriptor fd, boolean blocking, long pos, long size, boolean shared)169     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
170                             long size, boolean shared) throws IOException;
171 
release0(FileDescriptor fd, long pos, long size)172     static native void release0(FileDescriptor fd, long pos, long size)
173         throws IOException;
174 
close0(FileDescriptor fd)175     static native void close0(FileDescriptor fd) throws IOException;
176 
preClose0(FileDescriptor fd)177     private static native void preClose0(FileDescriptor fd) throws IOException;
178 
closeIntFD(int fd)179     static native void closeIntFD(int fd) throws IOException;
180 
181 }
182