⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filechannel.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * @(#)FileChannel.java	1.38 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.nio.channels;import java.io.*;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.spi.AbstractInterruptibleChannel;/** * A channel for reading, writing, mapping, and manipulating a file. * * <p> A file channel has a current <i>position</i> within its file which can * be both {@link #position() </code>queried<code>} and {@link #position(long) * </code>modified<code>}.  The file itself contains a variable-length sequence * of bytes that can be read and written and whose current {@link #size * </code><i>size</i><code>} can be queried.  The size of the file increases * when bytes are written beyond its current size; the size of the file * decreases when it is {@link #truncate </code><i>truncated</i><code>}.  The * file may also have some associated <i>metadata</i> such as access * permissions, content type, and last-modification time; this class does not * define methods for metadata access. * * <p> In addition to the familiar read, write, and close operations of byte * channels, this class defines the following file-specific operations: </p> * * <ul> * *   <li><p> Bytes may be {@link #read(ByteBuffer, long) </code>read<code>} or *   {@link #write(ByteBuffer, long) </code>written<code>} at an absolute *   position in a file in a way that does not affect the channel's current *   position.  </p></li> * *   <li><p> A region of a file may be {@link #map </code>mapped<code>} *   directly into memory; for large files this is often much more efficient *   than invoking the usual <tt>read</tt> or <tt>write</tt> methods. *   </p></li> * *   <li><p> Updates made to a file may be {@link #force </code>forced *   out<code>} to the underlying storage device, ensuring that data are not *   lost in the event of a system crash.  </p></li> * *   <li><p> Bytes can be transferred from a file {@link #transferTo </code>to *   some other channel<code>}, and {@link #transferFrom </code>vice *   versa<code>}, in a way that can be optimized by many operating systems *   into a very fast transfer directly to or from the filesystem cache. *   </p></li> * *   <li><p> A region of a file may be {@link FileLock </code>locked<code>} *   against access by other programs.  </p></li> * * </ul> * * <p> File channels are safe for use by multiple concurrent threads.  The * {@link Channel#close close} method may be invoked at any time, as specified * by the {@link Channel} interface.  Only one operation that involves the * channel's position or can change its file's size may be in progress at any * given time; attempts to initiate a second such operation while the first is * still in progress will block until the first operation completes.  Other * operations, in particular those that take an explicit position, may proceed * concurrently; whether they in fact do so is dependent upon the underlying * implementation and is therefore unspecified. * * <p> The view of a file provided by an instance of this class is guaranteed * to be consistent with other views of the same file provided by other * instances in the same program.  The view provided by an instance of this * class may or may not, however, be consistent with the views seen by other * concurrently-running programs due to caching performed by the underlying * operating system and delays induced by network-filesystem protocols.  This * is true regardless of the language in which these other programs are * written, and whether they are running on the same machine or on some other * machine.  The exact nature of any such inconsistencies are system-dependent * and are therefore unspecified. * * <p> This class does not define methods for opening existing files or for * creating new ones; such methods may be added in a future release.  In this * release a file channel can be obtained from an existing {@link * java.io.FileInputStream#getChannel FileInputStream}, {@link * java.io.FileOutputStream#getChannel FileOutputStream}, or {@link * java.io.RandomAccessFile#getChannel RandomAccessFile} object by invoking * that object's <tt>getChannel</tt> method, which returns a file channel that * is connected to the same underlying file. * * <p> The state of a file channel is intimately connected to that of the * object whose <tt>getChannel</tt> method returned the channel.  Changing the * channel's position, whether explicitly or by reading or writing bytes, will * change the file position of the originating object, and vice versa. * Changing the file's length via the file channel will change the length seen * via the originating object, and vice versa.  Changing the file's content by * writing bytes will change the content seen by the originating object, and * vice versa. * * <a name="open-mode"><p> At various points this class specifies that an * instance that is "open for reading," "open for writing," or "open for * reading and writing" is required.  A channel obtained via the {@link * java.io.FileInputStream#getChannel getChannel} method of a {@link * java.io.FileInputStream} instance will be open for reading.  A channel * obtained via the {@link java.io.FileOutputStream#getChannel getChannel} * method of a {@link java.io.FileOutputStream} instance will be open for * writing.  Finally, a channel obtained via the {@link * java.io.RandomAccessFile#getChannel getChannel} method of a {@link * java.io.RandomAccessFile} instance will be open for reading if the instance * was created with mode <tt>"r"</tt> and will be open for reading and writing * if the instance was created with mode <tt>"rw"</tt>. * * <a name="append-mode"><p> A file channel that is open for writing may be in * <i>append mode</i>, for example if it was obtained from a file-output stream * that was created by invoking the {@link * java.io.FileOutputStream#FileOutputStream(java.io.File,boolean) * FileOutputStream(File,boolean)} constructor and passing <tt>true</tt> for * the second parameter.  In this mode each invocation of a relative write * operation first advances the position to the end of the file and then writes * the requested data.  Whether the advancement of the position and the writing * of the data are done in a single atomic operation is system-dependent and * therefore unspecified. * * * @see java.io.FileInputStream#getChannel() * @see java.io.FileOutputStream#getChannel() * @see java.io.RandomAccessFile#getChannel() * * @author Mark Reinhold * @author Mike McCloskey * @author JSR-51 Expert Group * @version 1.38, 03/01/23 * @since 1.4 */public abstract class FileChannel    extends AbstractInterruptibleChannel    implements ByteChannel, GatheringByteChannel, ScatteringByteChannel{    /**     * Initializes a new instance of this class.     */    protected FileChannel() { }    // -- Channel operations --    /**     * Reads a sequence of bytes from this channel into the given buffer.     *     * <p> Bytes are read starting at this channel's current file position, and     * then the file position is updated with the number of bytes actually     * read.  Otherwise this method behaves exactly as specified in the {@link     * ReadableByteChannel} interface. </p>     */    public abstract int read(ByteBuffer dst) throws IOException;    /**     * Reads a sequence of bytes from this channel into a subsequence of the     * given buffers.     *     * <p> Bytes are read starting at this channel's current file position, and     * then the file position is updated with the number of bytes actually     * read.  Otherwise this method behaves exactly as specified in the {@link     * ScatteringByteChannel} interface.  </p>     */    public abstract long read(ByteBuffer[] dsts, int offset, int length)	throws IOException;    /**     * Reads a sequence of bytes from this channel into the given buffers.     *     * <p> Bytes are read starting at this channel's current file position, and     * then the file position is updated with the number of bytes actually     * read.  Otherwise this method behaves exactly as specified in the {@link     * ScatteringByteChannel} interface.  </p>     */    public final long read(ByteBuffer[] dsts) throws IOException {	return read(dsts, 0, dsts.length);    }    /**     * Writes a sequence of bytes to this channel from the given buffer.     *     * <p> Bytes are written starting at this channel's current file position     * unless the channel is in append mode, in which case the position is     * first advanced to the end of the file.  The file is grown, if necessary,     * to accomodate the written bytes, and then the file position is updated     * with the number of bytes actually written.  Otherwise this method     * behaves exactly as specified by the {@link WritableByteChannel}     * interface. </p>     */     public abstract int write(ByteBuffer src) throws IOException;    /**     * Writes a sequence of bytes to this channel from a subsequence of the     * given buffers.     *     * <p> Bytes are written starting at this channel's current file position     * unless the channel is in append mode, in which case the position is     * first advanced to the end of the file.  The file is grown, if necessary,     * to accomodate the written bytes, and then the file position is updated     * with the number of bytes actually written.  Otherwise this method     * behaves exactly as specified in the {@link GatheringByteChannel}     * interface.  </p>     */     public abstract long write(ByteBuffer[] srcs, int offset, int length)	throws IOException;    /**     * Writes a sequence of bytes to this channel from the given buffers.     *     * <p> Bytes are written starting at this channel's current file position     * unless the channel is in append mode, in which case the position is     * first advanced to the end of the file.  The file is grown, if necessary,     * to accomodate the written bytes, and then the file position is updated     * with the number of bytes actually written.  Otherwise this method     * behaves exactly as specified in the {@link GatheringByteChannel}     * interface.  </p>     */     public final long write(ByteBuffer[] srcs) throws IOException {	return write(srcs, 0, srcs.length);    }    // -- Other operations --    /**     * Returns this channel's file position.  </p>     *     * @return  This channel's file position,     *          a non-negative integer counting the number of bytes     *          from the beginning of the file to the current position     *     * @throws  ClosedChannelException     *          If this channel is closed     *     * @throws  IOException     *          If some other I/O error occurs     */    public abstract long position() throws IOException;    /**     * Sets this channel's file position.     *     * <p> Setting the position to a value that is greater than the file's     * current size is legal but does not change the size of the file.  A later     * attempt to read bytes at such a position will immediately return an     * end-of-file indication.  A later attempt to write bytes at such a     * position will cause the file to be grown to accomodate the new bytes;     * the values of any bytes between the previous end-of-file and the     * newly-written bytes are unspecified.  </p>     *     * @param  newPosition     *         The new position, a non-negative integer counting     *         the number of bytes from the beginning of the file     *     * @return  This file channel     *     * @throws  ClosedChannelException     *          If this channel is closed     *     * @throws  IllegalArgumentException     *          If the new position is negative     *     * @throws  IOException     *          If some other I/O error occurs     */    public abstract FileChannel position(long newPosition) throws IOException;    /**     * Returns the current size of this channel's file. </p>     *     * @return  The current size of this channel's file,     *          measured in bytes     *     * @throws  ClosedChannelException     *          If this channel is closed     *     * @throws  IOException     *          If some other I/O error occurs     */    public abstract long size() throws IOException;    /**     * Truncates this channel's file to the given size.     *     * <p> If the given size is less than the file's current size then the file     * is truncated, discarding any bytes beyond the new end of the file.  If     * the given size is greater than or equal to the file's current size then     * the file is not modified.  In either case, if this channel's file     * position is greater than the given size then it is set to that size.     * </p>     *     * @param  size     *         The new size, a non-negative byte count     *     * @return  This file channel     *     * @throws  NonWritableChannelException     *          If this channel was not opened for writing     *     * @throws  ClosedChannelException     *          If this channel is closed     *     * @throws  IllegalArgumentException     *          If the new size is negative     *     * @throws  IOException     *          If some other I/O error occurs     */    public abstract FileChannel truncate(long size) throws IOException;    /**     * Forces any updates to this channel's file to be written to the storage     * device that contains it.     *     * <p> If this channel's file resides on a local storage device then when     * this method returns it is guaranteed that all changes made to the file     * since this channel was created, or since this method was last invoked,     * will have been written to that device.  This is useful for ensuring that     * critical information is not lost in the event of a system crash.     *     * <p> If the file does not reside on a local device then no such guarantee

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -