1 /*
   2  * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.channels;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.io.IOException;
  30 
  31 /**
  32  * A byte channel that maintains a current <i>position</i> and allows the
  33  * position to be changed.
  34  *
  35  * <p> A seekable byte channel is connected to an entity, typically a file,
  36  * that contains a variable-length sequence of bytes that can be read and
  37  * written. The current position can be {@link #position() <i>queried</i>} and
  38  * {@link #position(long) <i>modified</i>}. The channel also provides access to
  39  * the current <i>size</i> of the entity to which the channel is connected. The
  40  * size increases when bytes are written beyond its current size; the size
  41  * decreases when it is {@link #truncate <i>truncated</i>}.
  42  *
  43  * <p> The {@link #position(long) position} and {@link #truncate truncate} methods
  44  * which do not otherwise have a value to return are specified to return the
  45  * channel upon which they are invoked. This allows method invocations to be
  46  * chained. Implementations of this interface should specialize the return type
  47  * so that method invocations on the implementation class can be chained.
  48  *
  49  * @since 1.7
  50  */
  51 
  52 public interface SeekableByteChannel
  53     extends ByteChannel
  54 {
  55     /**
  56      * Reads a sequence of bytes from this channel into the given buffer.
  57      *
  58      * <p> Bytes are read starting at this channel's current position, and
  59      * then the position is updated with the number of bytes actually read.
  60      * Otherwise this method behaves exactly as specified in the {@link
  61      * ReadableByteChannel} interface.
  62      */
  63     @Override
  64     int read(ByteBuffer dst) throws IOException;
  65 
  66     /**
  67      * Writes a sequence of bytes to this channel from the given buffer.
  68      *
  69      * <p> Bytes are written starting at this channel's current position, unless
  70      * the channel is connected to an entity such as a file that is opened with
  71      * the {@link java.nio.file.StandardOpenOption#APPEND APPEND} option, in
  72      * which case the position is first advanced to the end. The entity to which
  73      * the channel is connected is grown, if necessary, to accommodate the
  74      * written bytes, and then the position is updated with the number of bytes
  75      * actually written. Otherwise this method behaves exactly as specified by
  76      * the {@link WritableByteChannel} interface.
  77      */
  78     @Override
  79     int write(ByteBuffer src) throws IOException;
  80 
  81     /**
  82      * Returns this channel's position.
  83      *
  84      * @return  This channel's position,
  85      *          a non-negative integer counting the number of bytes
  86      *          from the beginning of the entity to the current position
  87      *
  88      * @throws  ClosedChannelException
  89      *          If this channel is closed
  90      * @throws  IOException
  91      *          If some other I/O error occurs
  92      */
  93     long position() throws IOException;
  94 
  95     /**
  96      * Sets this channel's position.
  97      *
  98      * <p> Setting the position to a value that is greater than the current size
  99      * is legal but does not change the size of the entity.  A later attempt to
 100      * read bytes at such a position will immediately return an end-of-file
 101      * indication.  A later attempt to write bytes at such a position will cause
 102      * the entity to grow to accommodate the new bytes; the values of any bytes
 103      * between the previous end-of-file and the newly-written bytes are
 104      * unspecified.
 105      *
 106      * <p> Setting the channel's position is not recommended when connected to
 107      * an entity, typically a file, that is opened with the {@link
 108      * java.nio.file.StandardOpenOption#APPEND APPEND} option. When opened for
 109      * append, the position is first advanced to the end before writing.
 110      *
 111      * @param  newPosition
 112      *         The new position, a non-negative integer counting
 113      *         the number of bytes from the beginning of the entity
 114      *
 115      * @return  This channel
 116      *
 117      * @throws  ClosedChannelException
 118      *          If this channel is closed
 119      * @throws  IllegalArgumentException
 120      *          If the new position is negative
 121      * @throws  IOException
 122      *          If some other I/O error occurs
 123      */
 124     SeekableByteChannel position(long newPosition) throws IOException;
 125 
 126     /**
 127      * Returns the current size of entity to which this channel is connected.
 128      *
 129      * @return  The current size, measured in bytes
 130      *
 131      * @throws  ClosedChannelException
 132      *          If this channel is closed
 133      * @throws  IOException
 134      *          If some other I/O error occurs
 135      */
 136     long size() throws IOException;
 137 
 138     /**
 139      * Truncates the entity, to which this channel is connected, to the given
 140      * size.
 141      *
 142      * <p> If the given size is less than the current size then the entity is
 143      * truncated, discarding any bytes beyond the new end. If the given size is
 144      * greater than or equal to the current size then the entity is not modified.
 145      * In either case, if the current position is greater than the given size
 146      * then it is set to that size.
 147      *
 148      * <p> An implementation of this interface may prohibit truncation when
 149      * connected to an entity, typically a file, opened with the {@link
 150      * java.nio.file.StandardOpenOption#APPEND APPEND} option.
 151      *
 152      * @param  size
 153      *         The new size, a non-negative byte count
 154      *
 155      * @return  This channel
 156      *
 157      * @throws  NonWritableChannelException
 158      *          If this channel was not opened for writing
 159      * @throws  ClosedChannelException
 160      *          If this channel is closed
 161      * @throws  IllegalArgumentException
 162      *          If the new size is negative
 163      * @throws  IOException
 164      *          If some other I/O error occurs
 165      */
 166     SeekableByteChannel truncate(long size) throws IOException;
 167 }