Home | History | Annotate | Download | only in platform
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 // BEGIN android-note
     19 // address length was changed from long to int for performance reasons.
     20 // END android-note
     21 
     22 package org.apache.harmony.luni.platform;
     23 
     24 import java.io.FileDescriptor;
     25 import java.io.FileNotFoundException;
     26 import java.io.IOException;
     27 
     28 class OSFileSystem implements IFileSystem {
     29 
     30     private static final OSFileSystem singleton = new OSFileSystem();
     31 
     32     public static OSFileSystem getOSFileSystem() {
     33         return singleton;
     34     }
     35 
     36     private OSFileSystem() {
     37     }
     38 
     39     private final void validateLockArgs(int type, long start, long length) {
     40         if ((type != IFileSystem.SHARED_LOCK_TYPE)
     41                 && (type != IFileSystem.EXCLUSIVE_LOCK_TYPE)) {
     42             throw new IllegalArgumentException("Illegal lock type requested.");
     43         }
     44 
     45         // Start position
     46         if (start < 0) {
     47             throw new IllegalArgumentException("start < 0");
     48         }
     49 
     50         // Length of lock stretch
     51         if (length < 0) {
     52             throw new IllegalArgumentException("length < 0");
     53         }
     54     }
     55 
     56     private native int lockImpl(int fd, long start, long length, int type, boolean wait);
     57 
     58     /**
     59      * Returns the granularity for virtual memory allocation.
     60      * Note that this value for Windows differs from the one for the
     61      * page size (64K and 4K respectively).
     62      */
     63     public native int getAllocGranularity();
     64 
     65     public native long length(int fd);
     66 
     67     public boolean lock(int fd, long start, long length, int type, boolean waitFlag)
     68             throws IOException {
     69         // Validate arguments
     70         validateLockArgs(type, start, length);
     71         int result = lockImpl(fd, start, length, type, waitFlag);
     72         return result != -1;
     73     }
     74 
     75     private native void unlockImpl(int fd, long start, long length) throws IOException;
     76 
     77     public void unlock(int fd, long start, long length) throws IOException {
     78         // Validate arguments
     79         validateLockArgs(IFileSystem.SHARED_LOCK_TYPE, start, length);
     80         unlockImpl(fd, start, length);
     81     }
     82 
     83     public native void fsync(int fd, boolean metadata) throws IOException;
     84 
     85     /*
     86      * File position seeking.
     87      */
     88     public native long seek(int fd, long offset, int whence) throws IOException;
     89 
     90     /*
     91      * Direct read/write APIs work on addresses.
     92      */
     93     public native long readDirect(int fd, int address, int offset, int length);
     94 
     95     public native long writeDirect(int fd, int address, int offset, int length);
     96 
     97     /*
     98      * Indirect read/writes work on byte[]'s
     99      */
    100     public native long read(int fd, byte[] bytes, int offset, int length) throws IOException;
    101 
    102     public native long write(int fd, byte[] bytes, int offset, int length) throws IOException;
    103 
    104     /*
    105      * Scatter/gather calls.
    106      */
    107     public native long readv(int fd, int[] addresses, int[] offsets, int[] lengths, int size)
    108             throws IOException;
    109 
    110     public native long writev(int fd, int[] addresses, int[] offsets, int[] lengths, int size)
    111             throws IOException;
    112 
    113     public native void truncate(int fd, long size) throws IOException;
    114 
    115     public native int open(String path, int mode) throws FileNotFoundException;
    116 
    117     public native long transfer(int fd, FileDescriptor sd, long offset, long count)
    118             throws IOException;
    119 
    120     public native int ioctlAvailable(FileDescriptor fileDescriptor) throws IOException;
    121 }
    122