randomaccessfile.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,012 行 · 第 1/3 页
JAVA
1,012 行
/* RandomAccessFile.java -- Class supporting random file I/O
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.io;
import java.nio.channels.FileChannel;
/**
* This class allows reading and writing of files at random locations.
* Most Java I/O classes are either pure sequential input or output. This
* class fulfills the need to be able to read the bytes of a file in an
* arbitrary order. In addition, this class implements the
* <code>DataInput</code> and <code>DataOutput</code> interfaces to allow
* the reading and writing of Java primitives.
*
* @version 0.0
*
* @author Aaron M. Renn (arenn@urbanophile.com)
* @author E. Prangsma (connection to JNode)
*/
public class RandomAccessFile implements DataOutput, DataInput {
/**
* The native file descriptor for this file
*/
private final VMFileHandle fh;
/**
* Whether or not this file is open in read only mode
*/
private boolean read_only;
/**
* This method initializes a new instance of <code>RandomAccessFile</code>
* to read from the specified file name with the specified access mode.
* The access mode is either "r" for read only access or "rw" for read
* write access.
* <p>
* Note that a <code>SecurityManager</code> check is made prior to
* opening the file to determine whether or not this file is allowed to
* be read or written.
*
* @param name The name of the file to read and/or write
* @param mode "r" for read only or "rw" for read-write access to the file
*
* @exception IllegalArgumentException If <code>mode</code> has an illegal value
* @exception SecurityException If the requested access to the file is not allowed
* @exception IOException If any other error occurs
*/
public RandomAccessFile(String name, String mode)
throws IllegalArgumentException, SecurityException, IOException {
this(new File(name), mode);
}
/**
* This method initializes a new instance of <code>RandomAccessFile</code>
* to read from the specified <code>File</code> object with the specified
* access mode. The access mode is either "r" for read only access or "rw"
* for read-write access.
* <p>
* Note that a <code>SecurityManager</code> check is made prior to
* opening the file to determine whether or not this file is allowed to
* be read or written.
*
* @param file The <code>File</code> object to read and/or write.
* @param mode "r" for read only or "rw" for read-write access to the file
*
* @exception IllegalArgumentException If <code>mode</code> has an illegal value
* @exception SecurityException If the requested access to the file is not allowed
* @exception IOException If any other error occurs
*/
public RandomAccessFile(File file, String mode)
throws IllegalArgumentException, SecurityException, IOException {
// Check the mode
if (!mode.equals("r") && !mode.equals("rw")) {
throw new IllegalArgumentException("Bad mode value: " + mode);
}
// The obligatory SecurityManager stuff
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
if (mode.equals("r")) {
sm.checkRead(file.getPath());
} else if (mode.equals("rw")) {
sm.checkWrite(file.getPath());
}
}
if (mode.equals("r"))
read_only = true;
if (read_only) {
this.fh = VMIOUtils.getAPI().open(file, VMOpenMode.READ);
} else {
this.fh = VMIOUtils.getAPI().open(file, VMOpenMode.READ_WRITE);
}
}
/**
* This method closes the file and frees up all file related system
* resources. Since most operating systems put a limit on how many files
* may be opened at any given time, it is a good idea to close all files
* when no longer needed to avoid hitting this limit
*/
public void close() throws IOException {
if (!fh.isClosed()) {
fh.close();
}
}
/**
* This method returns a <code>FileDescriptor</code> object that
* represents the native file handle for this file.
*
* @return The <code>FileDescriptor</code> object for this file
*
* @exception IOException If an error occurs
*/
public final FileDescriptor getFD() throws IOException {
return new FileDescriptor(fh);
}
/**
* This method returns the current offset in the file at which the next
* read or write will occur
*
* @return The current file position
*
* @exception IOException If an error occurs
*/
public long getFilePointer() throws IOException {
return fh.getPosition();
}
/**
* This method returns the length of the file in bytes
*
* @return The length of the file
*
* @exception IOException If an error occurs
*/
public long length() throws IOException {
return fh.getLength();
}
/**
* This method sets the current file position to the specified offset
* from the beginning of the file. Note that some operating systems will
* allow the file pointer to be set past the current end of the file.
*
* @param pos The offset from the beginning of the file at which to set the file pointer
*
* @exception IOException If an error occurs
*/
public void seek(long pos) throws IOException {
fh.setPosition(pos);
}
/**
* This method sets the length of the file to the specified length. If
* the currently length of the file is longer than the specified length,
* then the file is truncated to the specified length. If the current
* length of the file is shorter than the specified length, the file
* is extended with bytes of an undefined value.
* <p>
* The file must be open for write access for this operation to succeed.
*
* @param newlen The new length of the file
*
* @exception IOException If an error occurs
*/
public void setLength(long newlen) throws IOException {
if (read_only) {
throw new IOException("File is open read only");
}
fh.setLength(newlen);
}
/**
* This method reads a single byte of data from the file and returns it
* as an integer.
*
* @return The byte read as an int, or -1 if the end of the file was reached.
*
* @exception IOException If an error occurs
*/
public int read() throws IOException {
byte[] buf = new byte[1];
int rc = readInternal(buf, 0, buf.length);
if (rc == 0)
return (-1);
return (buf[0] & 0xFF);
}
/**
* This method reads bytes from the file into the specified array. The
* bytes are stored starting at the beginning of the array and up to
* <code>buf.length</code> bytes can be read.
*
* @param buf The buffer to read bytes from the file into
*
* @return The actual number of bytes read or -1 if end of file
*
* @exception IOException If an error occurs
*/
public int read(byte[] buf) throws IOException {
int rc = readInternal(buf, 0, buf.length);
if (rc == 0)
return (-1);
else
return (rc);
}
/**
* This methods reads up to <code>len</code> bytes from the file into the s
* pecified array starting at position <code>offset</code> into the array.
*
* @param buf The array to read the bytes into
* @param offset The index into the array to start storing bytes
* @param len The requested number of bytes to read
*
* @param len The actual number of bytes read, or -1 if end of file
*
* @exception IOException If an error occurs
*/
public int read(byte[] buf, int offset, int len) throws IOException {
int rc = readInternal(buf, offset, len);
if (rc == 0)
return (-1);
else
return (rc);
}
/**
* This native method actually reads the bytes from the file
*/
private int readInternal(byte[] buf, int offset, int len)
throws IOException {
final long max = (fh.getLength() - fh.getPosition());
if (len > max) {
len = (int)max;
}
fh.read(buf, offset, len);
return len;
}
/**
* This method reads a Java boolean value from an input stream. It does
* so by reading a single byte of data. If that byte is zero, then the
* value returned is <code>false</code> If the byte is non-zero, then
* the value returned is <code>true</code>
* <p>
* This method can read a <code>boolean</code> written by an object implementing the
* <code>writeBoolean()</code> method in the <code>DataOutput</code> interface.
*
* @return The <code>boolean</code> value read
*
* @exception EOFException If end of file is reached before reading the boolean
* @exception IOException If any other error occurs
*/
public final boolean readBoolean() throws EOFException, IOException {
int byte_read = read();
if (byte_read == -1)
throw new EOFException("Unexpected end of stream");
return (DataInputStream.convertToBoolean(byte_read));
}
/**
* This method reads a Java byte value from an input stream. The value
* is in the range of -128 to 127.
* <p>
* This method can read a <code>byte</code> written by an object implementing the
* <code>writeByte()</code> method in the <code>DataOutput</code> interface.
*
* @return The <code>byte</code> value read
*
* @exception EOFException If end of file is reached before reading the byte
* @exception IOException If any other error occurs
*
* @see DataOutput
*/
public final byte readByte() throws EOFException, IOException {
int byte_read = read();
if (byte_read == -1)
throw new EOFException("Unexpected end of stream");
return (DataInputStream.convertToByte(byte_read));
}
/**
* This method reads 8 unsigned bits into a Java <code>int</code> value from the
* stream. The value returned is in the range of 0 to 255.
* <p>
* This method can read an unsigned byte written by an object implementing the
* <code>writeUnsignedByte()</code> method in the <code>DataOutput</code> interface.
*
* @return The unsigned bytes value read as a Java <code>int</code>
*
* @exception EOFException If end of file is reached before reading the value
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?