filedescriptor.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 582 行 · 第 1/2 页
JAVA
582 行
return nativeReadBuf(nativeFd, buf, offset, len); } /** * Returns the number of bytes available for reading * * @return The number of bytes available for reading * * @exception IOException If an error occurs */ int available() throws IOException { if (nativeFd == -1L) throw new IOException("Invalid FileDescriptor"); return nativeAvailable(nativeFd); } /** * Method to do a "seek" operation on the file * * @param offset The number of bytes to seek * @param whence The position to seek from, either * SET (0) for the beginning of the file, CUR (1) for the * current position or END (2) for the end position. * @param stopAtEof <code>true</code> to ensure that there is no * seeking past the end of the file, <code>false</code> otherwise. * * @return The new file position, or -1 if end of file. * * @exception IOException If an error occurs */ long seek(long offset, int whence, boolean stopAtEof) throws IOException { if (nativeFd == -1L) throw new IOException("Invalid FileDescriptor"); if ((whence != SET) && (whence != CUR) && (whence != END)) throw new IllegalArgumentException("Invalid whence value: " + whence); return nativeSeek(nativeFd, offset, whence, stopAtEof); } /** * Returns the current position of the file pointer in the file * * @param fd The native file descriptor * * @exception IOException If an error occurs */ long getFilePointer() throws IOException { if (nativeFd == -1L) throw new IOException("Invalid FileDescriptor"); return nativeGetFilePointer(nativeFd); } /** * Returns the length of the file in bytes * * @return The length of the file in bytes * * @exception IOException If an error occurs */ long getLength() throws IOException { if (nativeFd == -1L) throw new IOException("Invalid FileDescriptor"); return nativeGetLength(nativeFd); } /** * Sets the length of the file to the specified number of bytes * This can result in truncation or extension. * * @param len The new length of the file * * @exception IOException If an error occurs */ void setLength(long len) throws IOException { if (nativeFd == -1L) throw new IOException("Invalid FileDescriptor"); if (len < 0) throw new IllegalArgumentException("Length cannot be less than zero " + len); nativeSetLength(nativeFd, len); } // Don't do anything crazy with this long getNativeFd() { return nativeFd; } private void setNativeFd(long nativeFd) { this.nativeFd = nativeFd; } protected void finalize() throws Throwable { close(); } /* * Native FileDescriptor provider interface * * Platform implementors must implement these methods. Note that this * class guarantees that valid data will be passed to these methods, * so basic error checking on input values can be skipped. */ /** * This method is called in the class initializer to do any require * native library initialization. It is also responsible for initializing * the in, out, and err variables. */ private static native void nativeInit(); /** * Opens the specified file in the specified mode. This can be done * in one of the specified modes: * <ul> * <li>r - Read Only * <li>rw - Read / Write * <li>ra - Read / Write - append to end of file * <li>rws - Read / Write - synchronous writes of data/metadata * <li>rwd - Read / Write - synchronous writes of data. * * @param path Name of the file to open * @param mode Mode to open * * @return The resulting file descriptor for the opened file, or -1 * on failure (exception also signaled). * * @exception IOException If an error occurs. */ private native long nativeOpen(String path, int mode) throws FileNotFoundException; /** * Closes this specified file descriptor * * @param fd The native file descriptor to close * * @return The return code of the native close command. * * @exception IOException If an error occurs */ private native long nativeClose(long fd) throws IOException; /** * Writes a single byte to the file * * @param fd The native file descriptor to write to * @param b The byte to write, encoded in the low eight bits * * @return The return code of the native write command * * @exception IOException If an error occurs */ private native long nativeWriteByte(long fd, int b) throws IOException; /** * Writes a byte buffer to the file * * @param fd The native file descriptor to write to * @param buf The byte buffer to write from * @param int The offset into the buffer to start writing from * @param len The number of bytes to write. * * @return The return code of the native write command * * @exception IOException If an error occurs */ private native long nativeWriteBuf(long fd, byte[] buf, int offset, int len) throws IOException; /** * Reads a single byte from the file * * @param fd The native file descriptor to read from * * @return The byte read, in the low eight bits on a long, or -1 * if end of file * * @exception IOException If an error occurs */ private native int nativeReadByte(long fd) throws IOException; /** * Reads a buffer of bytes from the file * * @param fd The native file descriptor to read from * @param buf The buffer to read bytes into * @param offset The offset into the buffer to start storing bytes * @param len The number of bytes to read. * * @return The number of bytes read, or -1 if end of file. * * @exception IOException If an error occurs */ private native int nativeReadBuf(long fd, byte[] buf, int offset, int len) throws IOException; /** * Returns the number of bytes available for reading * * @param fd The native file descriptor * * @return The number of bytes available for reading * * @exception IOException If an error occurs */ private native int nativeAvailable(long fd) throws IOException; /** * Method to do a "seek" operation on the file * * @param fd The native file descriptor * @param offset The number of bytes to seek * @param whence The position to seek from, either * SET (0) for the beginning of the file, CUR (1) for the * current position or END (2) for the end position. * @param stopAtEof <code>true</code> to ensure that there is no * seeking past the end of the file, <code>false</code> otherwise. * * @return The new file position, or -1 if end of file. * * @exception IOException If an error occurs */ private native long nativeSeek(long fd, long offset, int whence, boolean stopAtEof) throws IOException; /** * Returns the current position of the file pointer in the file * * @param fd The native file descriptor * * @exception IOException If an error occurs */ private native long nativeGetFilePointer(long fd) throws IOException; /** * Returns the length of the file in bytes * * @param fd The native file descriptor * * @return The length of the file in bytes * * @exception IOException If an error occurs */ private native long nativeGetLength(long fd) throws IOException; /** * Sets the length of the file to the specified number of bytes * This can result in truncation or extension. * * @param fd The native file descriptor * @param len The new length of the file * * @exception IOException If an error occurs */ private native void nativeSetLength(long fd, long len) throws IOException; /** * Tests a file descriptor for validity * * @param fd The native file descriptor * * @return <code>true</code> if the fd is valid, <code>false</code> * otherwise */ private native boolean nativeValid(long fd); /** * Flushes any buffered contents to disk * * @param fd The native file descriptor * * @exception IOException If an error occurs */ private native void nativeSync(long fd) throws SyncFailedException;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?