1/*
2 * Copyright (c) 2000, 2013, 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
26package sun.nio.ch;
27
28import dalvik.system.BlockGuard;
29
30import java.io.FileDescriptor;
31import java.io.IOException;
32
33class FileDispatcherImpl extends FileDispatcher {
34
35    // Android-removed: Code to load native libraries, doesn't make sense on Android.
36    /*
37    static {
38        IOUtil.load();
39        init();
40    }
41    */
42
43    FileDispatcherImpl(boolean append) {
44        /* append is ignored */
45    }
46
47    FileDispatcherImpl() {
48    }
49
50    int read(FileDescriptor fd, long address, int len) throws IOException {
51        // Android-added: BlockGuard support.
52        BlockGuard.getThreadPolicy().onReadFromDisk();
53        return read0(fd, address, len);
54    }
55
56    int pread(FileDescriptor fd, long address, int len, long position)
57        throws IOException
58    {
59        // Android-added: BlockGuard support.
60        BlockGuard.getThreadPolicy().onReadFromDisk();
61        return pread0(fd, address, len, position);
62    }
63
64    long readv(FileDescriptor fd, long address, int len) throws IOException {
65        // Android-added: BlockGuard support.
66        BlockGuard.getThreadPolicy().onReadFromDisk();
67        return readv0(fd, address, len);
68    }
69
70    int write(FileDescriptor fd, long address, int len) throws IOException {
71        // Android-added: BlockGuard support.
72        BlockGuard.getThreadPolicy().onWriteToDisk();
73        return write0(fd, address, len);
74    }
75
76    int pwrite(FileDescriptor fd, long address, int len, long position)
77        throws IOException
78    {
79        // Android-added: BlockGuard support.
80        BlockGuard.getThreadPolicy().onWriteToDisk();
81        return pwrite0(fd, address, len, position);
82    }
83
84    long writev(FileDescriptor fd, long address, int len)
85        throws IOException
86    {
87        // Android-added: BlockGuard support.
88        BlockGuard.getThreadPolicy().onWriteToDisk();
89        return writev0(fd, address, len);
90    }
91
92    int force(FileDescriptor fd, boolean metaData) throws IOException {
93        // Android-added: BlockGuard support.
94        BlockGuard.getThreadPolicy().onWriteToDisk();
95        return force0(fd, metaData);
96    }
97
98    int truncate(FileDescriptor fd, long size) throws IOException {
99        // Android-added: BlockGuard support.
100        BlockGuard.getThreadPolicy().onWriteToDisk();
101        return truncate0(fd, size);
102    }
103
104    long size(FileDescriptor fd) throws IOException {
105        // Android-added: BlockGuard support.
106        BlockGuard.getThreadPolicy().onReadFromDisk();
107        return size0(fd);
108    }
109
110    int lock(FileDescriptor fd, boolean blocking, long pos, long size,
111             boolean shared) throws IOException
112    {
113        // Android-added: BlockGuard support.
114        BlockGuard.getThreadPolicy().onWriteToDisk();
115        return lock0(fd, blocking, pos, size, shared);
116    }
117
118    void release(FileDescriptor fd, long pos, long size) throws IOException {
119        // Android-added: BlockGuard support.
120        BlockGuard.getThreadPolicy().onWriteToDisk();
121        release0(fd, pos, size);
122    }
123
124    void close(FileDescriptor fd) throws IOException {
125        close0(fd);
126    }
127
128    void preClose(FileDescriptor fd) throws IOException {
129        preClose0(fd);
130    }
131
132    FileDescriptor duplicateForMapping(FileDescriptor fd) {
133        // file descriptor not required for mapping operations; okay
134        // to return invalid file descriptor.
135        return new FileDescriptor();
136    }
137
138    boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
139        return true;
140    }
141
142    boolean transferToDirectlyNeedsPositionLock() {
143        return false;
144    }
145
146    // -- Native methods --
147
148    static native int read0(FileDescriptor fd, long address, int len)
149        throws IOException;
150
151    static native int pread0(FileDescriptor fd, long address, int len,
152                             long position) throws IOException;
153
154    static native long readv0(FileDescriptor fd, long address, int len)
155        throws IOException;
156
157    static native int write0(FileDescriptor fd, long address, int len)
158        throws IOException;
159
160    static native int pwrite0(FileDescriptor fd, long address, int len,
161                             long position) throws IOException;
162
163    static native long writev0(FileDescriptor fd, long address, int len)
164        throws IOException;
165
166    static native int force0(FileDescriptor fd, boolean metaData)
167        throws IOException;
168
169    static native int truncate0(FileDescriptor fd, long size)
170        throws IOException;
171
172    static native long size0(FileDescriptor fd) throws IOException;
173
174    static native int lock0(FileDescriptor fd, boolean blocking, long pos,
175                            long size, boolean shared) throws IOException;
176
177    static native void release0(FileDescriptor fd, long pos, long size)
178        throws IOException;
179
180    static native void close0(FileDescriptor fd) throws IOException;
181
182    static native void preClose0(FileDescriptor fd) throws IOException;
183
184    static native void closeIntFD(int fd) throws IOException;
185
186    // Android-removed: Code to load native libraries, doesn't make sense on Android.
187    // static native void init();
188
189}
190