Path: blob/master/src/java.base/share/classes/java/nio/channels/SeekableByteChannel.java
41159 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.nio.ByteBuffer;28import java.io.IOException;2930/**31* A byte channel that maintains a current <i>position</i> and allows the32* position to be changed.33*34* <p> A seekable byte channel is connected to an entity, typically a file,35* that contains a variable-length sequence of bytes that can be read and36* written. The current position can be {@link #position() <i>queried</i>} and37* {@link #position(long) <i>modified</i>}. The channel also provides access to38* the current <i>size</i> of the entity to which the channel is connected. The39* size increases when bytes are written beyond its current size; the size40* decreases when it is {@link #truncate <i>truncated</i>}.41*42* <p> The {@link #position(long) position} and {@link #truncate truncate} methods43* which do not otherwise have a value to return are specified to return the44* channel upon which they are invoked. This allows method invocations to be45* chained. Implementations of this interface should specialize the return type46* so that method invocations on the implementation class can be chained.47*48* @since 1.749* @see java.nio.file.Files#newByteChannel50*/5152public interface SeekableByteChannel53extends ByteChannel54{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, and59* then the position is updated with the number of bytes actually read.60* Otherwise this method behaves exactly as specified in the {@link61* ReadableByteChannel} interface.62*/63@Override64int read(ByteBuffer dst) throws IOException;6566/**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, unless70* the channel is connected to an entity such as a file that is opened with71* the {@link java.nio.file.StandardOpenOption#APPEND APPEND} option, in72* which case the position is first advanced to the end. The entity to which73* the channel is connected is grown, if necessary, to accommodate the74* written bytes, and then the position is updated with the number of bytes75* actually written. Otherwise this method behaves exactly as specified by76* the {@link WritableByteChannel} interface.77*/78@Override79int write(ByteBuffer src) throws IOException;8081/**82* Returns this channel's position.83*84* @return This channel's position,85* a non-negative integer counting the number of bytes86* from the beginning of the entity to the current position87*88* @throws ClosedChannelException89* If this channel is closed90* @throws IOException91* If some other I/O error occurs92*/93long position() throws IOException;9495/**96* Sets this channel's position.97*98* <p> Setting the position to a value that is greater than the current size99* is legal but does not change the size of the entity. A later attempt to100* read bytes at such a position will immediately return an end-of-file101* indication. A later attempt to write bytes at such a position will cause102* the entity to grow to accommodate the new bytes; the values of any bytes103* between the previous end-of-file and the newly-written bytes are104* unspecified.105*106* <p> Setting the channel's position is not recommended when connected to107* an entity, typically a file, that is opened with the {@link108* java.nio.file.StandardOpenOption#APPEND APPEND} option. When opened for109* append, the position is first advanced to the end before writing.110*111* @param newPosition112* The new position, a non-negative integer counting113* the number of bytes from the beginning of the entity114*115* @return This channel116*117* @throws ClosedChannelException118* If this channel is closed119* @throws IllegalArgumentException120* If the new position is negative121* @throws IOException122* If some other I/O error occurs123*/124SeekableByteChannel position(long newPosition) throws IOException;125126/**127* Returns the current size of entity to which this channel is connected.128*129* @return The current size, measured in bytes130*131* @throws ClosedChannelException132* If this channel is closed133* @throws IOException134* If some other I/O error occurs135*/136long size() throws IOException;137138/**139* Truncates the entity, to which this channel is connected, to the given140* size.141*142* <p> If the given size is less than the current size then the entity is143* truncated, discarding any bytes beyond the new end. If the given size is144* 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 size146* then it is set to that size.147*148* <p> An implementation of this interface may prohibit truncation when149* connected to an entity, typically a file, opened with the {@link150* java.nio.file.StandardOpenOption#APPEND APPEND} option.151*152* @param size153* The new size, a non-negative byte count154*155* @return This channel156*157* @throws NonWritableChannelException158* If this channel was not opened for writing159* @throws ClosedChannelException160* If this channel is closed161* @throws IllegalArgumentException162* If the new size is negative163* @throws IOException164* If some other I/O error occurs165*/166SeekableByteChannel truncate(long size) throws IOException;167}168169170