001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.platform;
019:
020: import java.io.FileDescriptor;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023:
024: /**
025: * This is the portable implementation of the file system interface.
026: *
027: */
028: class OSFileSystem extends OSComponent implements IFileSystem {
029:
030: /**
031: *
032: */
033: public OSFileSystem() {
034: super ();
035: }
036:
037: private final void validateLockArgs(int type, long start,
038: long length) {
039: if ((type != IFileSystem.SHARED_LOCK_TYPE)
040: && (type != IFileSystem.EXCLUSIVE_LOCK_TYPE)) {
041: throw new IllegalArgumentException(
042: "Illegal lock type requested."); //$NON-NLS-1$
043: }
044:
045: // Start position
046: if (start < 0) {
047: throw new IllegalArgumentException(
048: "Lock start position must be non-negative"); //$NON-NLS-1$
049: }
050:
051: // Length of lock stretch
052: if (length < 0) {
053: throw new IllegalArgumentException(
054: "Lock length must be non-negative"); //$NON-NLS-1$
055: }
056: }
057:
058: private native int lockImpl(long fileDescriptor, long start,
059: long length, int type, boolean wait);
060:
061: /**
062: * Returns the granularity for virtual memory allocation.
063: * Note that this value for Windows differs from the one for the
064: * page size (64K and 4K respectively).
065: */
066: public native int getAllocGranularity() throws IOException;
067:
068: public boolean lock(long fileDescriptor, long start, long length,
069: int type, boolean waitFlag) throws IOException {
070: // Validate arguments
071: validateLockArgs(type, start, length);
072: int result = lockImpl(fileDescriptor, start, length, type,
073: waitFlag);
074: return result != -1;
075: }
076:
077: private native int unlockImpl(long fileDescriptor, long start,
078: long length);
079:
080: public void unlock(long fileDescriptor, long start, long length)
081: throws IOException {
082: // Validate arguments
083: validateLockArgs(IFileSystem.SHARED_LOCK_TYPE, start, length);
084: int result = unlockImpl(fileDescriptor, start, length);
085: if (result == -1) {
086: throw new IOException();
087: }
088: }
089:
090: private native int fflushImpl(long fd, boolean metadata);
091:
092: public void fflush(long fileDescriptor, boolean metadata)
093: throws IOException {
094: int result = fflushImpl(fileDescriptor, metadata);
095: if (result == -1) {
096: throw new IOException();
097: }
098: }
099:
100: /*
101: * File position seeking.
102: */
103:
104: private native long seekImpl(long fd, long offset, int whence);
105:
106: public long seek(long fileDescriptor, long offset, int whence)
107: throws IOException {
108: long pos = seekImpl(fileDescriptor, offset, whence);
109: if (pos == -1) {
110: throw new IOException();
111: }
112: return pos;
113: }
114:
115: /*
116: * Direct read/write APIs work on addresses.
117: */
118: private native long readDirectImpl(long fileDescriptor,
119: long address, int offset, int length);
120:
121: public long readDirect(long fileDescriptor, long address,
122: int offset, int length) throws IOException {
123: long bytesRead = readDirectImpl(fileDescriptor, address,
124: offset, length);
125: if (bytesRead < -1) {
126: throw new IOException();
127: }
128: return bytesRead;
129: }
130:
131: private native long writeDirectImpl(long fileDescriptor,
132: long address, int offset, int length);
133:
134: public long writeDirect(long fileDescriptor, long address,
135: int offset, int length) throws IOException {
136: long bytesWritten = writeDirectImpl(fileDescriptor, address,
137: offset, length);
138: if (bytesWritten < 0) {
139: throw new IOException();
140: }
141: return bytesWritten;
142: }
143:
144: /*
145: * Indirect read/writes work on byte[]'s
146: */
147: private native long readImpl(long fileDescriptor, byte[] bytes,
148: int offset, int length);
149:
150: public long read(long fileDescriptor, byte[] bytes, int offset,
151: int length) throws IOException {
152: if (bytes == null) {
153: throw new NullPointerException();
154: }
155: long bytesRead = readImpl(fileDescriptor, bytes, offset, length);
156: if (bytesRead < -1) {
157: /*
158: * TODO: bytesRead is never less than -1 so this code
159: * does nothing?
160: * The native code throws an exception in only one case
161: * so perhaps this should be 'bytesRead < 0' to handle
162: * any other cases. But the other cases have been
163: * ignored until now so fixing this could break things
164: */
165: throw new IOException();
166: }
167: return bytesRead;
168: }
169:
170: private native long writeImpl(long fileDescriptor, byte[] bytes,
171: int offset, int length);
172:
173: public long write(long fileDescriptor, byte[] bytes, int offset,
174: int length) throws IOException {
175: long bytesWritten = writeImpl(fileDescriptor, bytes, offset,
176: length);
177: if (bytesWritten < 0) {
178: throw new IOException();
179: }
180: return bytesWritten;
181: }
182:
183: /*
184: * Scatter/gather calls.
185: */
186: public long readv(long fileDescriptor, long[] addresses,
187: int[] offsets, int[] lengths, int size) throws IOException {
188: long bytesRead = readvImpl(fileDescriptor, addresses, offsets,
189: lengths, size);
190: if (bytesRead < -1) {
191: throw new IOException();
192: }
193: return bytesRead;
194: }
195:
196: private native long readvImpl(long fileDescriptor,
197: long[] addresses, int[] offsets, int[] lengths, int size);
198:
199: public long writev(long fileDescriptor, long[] addresses,
200: int[] offsets, int[] lengths, int size) throws IOException {
201: long bytesWritten = writevImpl(fileDescriptor, addresses,
202: offsets, lengths, size);
203: if (bytesWritten < 0) {
204: throw new IOException();
205: }
206: return bytesWritten;
207: }
208:
209: private native long writevImpl(long fileDescriptor,
210: long[] addresses, int[] offsets, int[] lengths, int size);
211:
212: private native int closeImpl(long fileDescriptor);
213:
214: /*
215: * (non-Javadoc)
216: *
217: * @see org.apache.harmony.luni.platform.IFileSystem#close(long)
218: */
219: public void close(long fileDescriptor) throws IOException {
220: int rc = closeImpl(fileDescriptor);
221: if (rc == -1) {
222: throw new IOException();
223: }
224: }
225:
226: public void truncate(long fileDescriptor, long size)
227: throws IOException {
228: int rc = truncateImpl(fileDescriptor, size);
229: if (rc < 0) {
230: throw new IOException();
231: }
232: }
233:
234: private native int truncateImpl(long fileDescriptor, long size);
235:
236: public long open(byte[] fileName, int mode)
237: throws FileNotFoundException {
238: if (fileName == null) {
239: throw new NullPointerException();
240: }
241: long handler = openImpl(fileName, mode);
242: if (handler < 0) {
243: throw new FileNotFoundException(new String(fileName));
244: }
245: return handler;
246: }
247:
248: private native long openImpl(byte[] fileName, int mode);
249:
250: public long transfer(long fileHandler,
251: FileDescriptor socketDescriptor, long offset, long count)
252: throws IOException {
253: long result = transferImpl(fileHandler, socketDescriptor,
254: offset, count);
255: if (result < 0)
256: throw new IOException();
257: return result;
258: }
259:
260: private native long transferImpl(long fileHandler,
261: FileDescriptor socketDescriptor, long offset, long count);
262:
263: public long ttyAvailable() throws IOException {
264: long nChar = ttyAvailableImpl();
265: if (nChar < 0) {
266: throw new IOException();
267: }
268: return nChar;
269: }
270:
271: private native long ttyAvailableImpl();
272:
273: public long ttyRead(byte[] bytes, int offset, int length)
274: throws IOException {
275: long nChar = ttyReadImpl(bytes, offset, length);
276: if (nChar < 0) {
277: throw new IOException();
278: }
279: return nChar;
280: }
281:
282: private native long ttyReadImpl(byte[] bytes, int offset, int length);
283: }
|