1 /* 2 * Copyright (c) 2007, 2011, 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 * @see java.nio.file.Files#newByteChannel 51 */ 52 53 public interface SeekableByteChannel 54 extends ByteChannel 55 { 56 /** 57 * Reads a sequence of bytes from this channel into the given buffer. 58 * 59 * <p> Bytes are read starting at this channel's current position, and 60 * then the position is updated with the number of bytes actually read. 61 * Otherwise this method behaves exactly as specified in the {@link 62 * ReadableByteChannel} interface. 63 */ 64 @Override read(ByteBuffer dst)65 int read(ByteBuffer dst) throws IOException; 66 67 /** 68 * Writes a sequence of bytes to this channel from the given buffer. 69 * 70 * <p> Bytes are written starting at this channel's current position. 71 * The entity to which 72 * the channel is connected is grown, if necessary, to accommodate the 73 * written bytes, and then the position is updated with the number of bytes 74 * actually written. Otherwise this method behaves exactly as specified by 75 * the {@link WritableByteChannel} interface. 76 */ 77 @Override write(ByteBuffer src)78 int write(ByteBuffer src) throws IOException; 79 80 /** 81 * Returns this channel's position. 82 * 83 * @return This channel's position, 84 * a non-negative integer counting the number of bytes 85 * from the beginning of the entity to the current position 86 * 87 * @throws ClosedChannelException 88 * If this channel is closed 89 * @throws IOException 90 * If some other I/O error occurs 91 */ position()92 long position() throws IOException; 93 94 /** 95 * Sets this channel's position. 96 * 97 * <p> Setting the position to a value that is greater than the current size 98 * is legal but does not change the size of the entity. A later attempt to 99 * read bytes at such a position will immediately return an end-of-file 100 * indication. A later attempt to write bytes at such a position will cause 101 * the entity to grow to accommodate the new bytes; the values of any bytes 102 * between the previous end-of-file and the newly-written bytes are 103 * unspecified. 104 * 105 * @param newPosition 106 * The new position, a non-negative integer counting 107 * the number of bytes from the beginning of the entity 108 * 109 * @return This channel 110 * 111 * @throws ClosedChannelException 112 * If this channel is closed 113 * @throws IllegalArgumentException 114 * If the new position is negative 115 * @throws IOException 116 * If some other I/O error occurs 117 */ position(long newPosition)118 SeekableByteChannel position(long newPosition) throws IOException; 119 120 /** 121 * Returns the current size of entity to which this channel is connected. 122 * 123 * @return The current size, measured in bytes 124 * 125 * @throws ClosedChannelException 126 * If this channel is closed 127 * @throws IOException 128 * If some other I/O error occurs 129 */ size()130 long size() throws IOException; 131 132 /** 133 * Truncates the entity, to which this channel is connected, to the given 134 * size. 135 * 136 * <p> If the given size is less than the current size then the entity is 137 * truncated, discarding any bytes beyond the new end. If the given size is 138 * greater than or equal to the current size then the entity is not modified. 139 * In either case, if the current position is greater than the given size 140 * then it is set to that size. 141 * 142 * @param size 143 * The new size, a non-negative byte count 144 * 145 * @return This channel 146 * 147 * @throws NonWritableChannelException 148 * If this channel was not opened for writing 149 * @throws ClosedChannelException 150 * If this channel is closed 151 * @throws IllegalArgumentException 152 * If the new size is negative 153 * @throws IOException 154 * If some other I/O error occurs 155 */ truncate(long size)156 SeekableByteChannel truncate(long size) throws IOException; 157 } 158