-
Notifications
You must be signed in to change notification settings - Fork 668
/
SeekableByteChannel.java
165 lines (155 loc) · 7.43 KB
/
SeekableByteChannel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio.channels;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* A byte channel that maintains a current <i>position</i> and allows the
* position to be changed.
*
* <p> A seekable byte channel is connected to an entity, typically a file,
* that contains a variable-length sequence of bytes that can be read and
* written. The current position can be {@link #position() <i>queried</i>} and
* {@link #position(long) <i>modified</i>}. The channel also provides access to
* the current <i>size</i> of the entity to which the channel is connected. The
* size increases when bytes are written beyond its current size; the size
* decreases when it is {@link #truncate <i>truncated</i>}.
*
* <p> The {@link #position(long) position} and {@link #truncate truncate} methods
* which do not otherwise have a value to return are specified to return the
* channel upon which they are invoked. This allows method invocations to be
* chained. Implementations of this interface should specialize the return type
* so that method invocations on the implementation class can be chained.
*
* @see java.nio.file.Files#newByteChannel
* @since 1.7
*/
// 支持随机读/写的字节通道(常用于file通道)
public interface SeekableByteChannel extends ByteChannel {
/**
* Reads a sequence of bytes from this channel into the given buffer.
*
* <p> Bytes are read starting at this channel's current position, and
* then the position is updated with the number of bytes actually read.
* Otherwise this method behaves exactly as specified in the {@link
* ReadableByteChannel} interface.
*/
/*
* 从当前通道(关联的文件)中起始处读取,读到的内容存入dst后,返回读到的字节数量
* 该方法是一次性地,即已经读完的流不可以重复读取
*/
@Override
int read(ByteBuffer dst) throws IOException;
/**
* Writes a sequence of bytes to this channel from the given buffer.
*
* <p> Bytes are written starting at this channel's current position, unless
* the channel is connected to an entity such as a file that is opened with
* the {@link java.nio.file.StandardOpenOption#APPEND APPEND} option, in
* which case the position is first advanced to the end. The entity to which
* the channel is connected is grown, if necessary, to accommodate the
* written bytes, and then the position is updated with the number of bytes
* actually written. Otherwise this method behaves exactly as specified by
* the {@link WritableByteChannel} interface.
*/
/*
* 从缓冲区src读取,读到的内容向当前通道(关联的文件)中追加写入后,返回写入的字节数量
* 待写入内容从fd中上次position==-1时写完的末尾追加内容(不支持随机写入)
*/
@Override
int write(ByteBuffer src) throws IOException;
/**
* Returns this channel's position.
*
* @return This channel's position, a non-negative integer counting the number of bytes
* from the beginning of the entity to the current position
*
* @throws ClosedChannelException If this channel is closed
* @throws IOException If some other I/O error occurs
*/
// 返回此通道(文件)的游标位置
long position() throws IOException;
/**
* Sets this channel's position.
*
* Setting the position to a value that is greater than the current size
* is legal but does not change the size of the entity. 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 entity to grow to accommodate the new bytes; the values of any bytes
* between the previous end-of-file and the newly-written bytes are
* unspecified.
*
* Setting the channel's position is not recommended when connected to
* an entity, typically a file, that is opened with the {@link
* java.nio.file.StandardOpenOption#APPEND APPEND} option. When opened for
* append, the position is first advanced to the end before writing.
*
* @param newPosition The new position, a non-negative integer counting
* the number of bytes from the beginning of the entity
*
* @return This 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
*/
// 设置此通道(文件)的游标位置
SeekableByteChannel position(long newPosition) throws IOException;
/**
* Returns the current size of entity to which this channel is connected.
*
* @return The current size, measured in bytes
*
* @throws ClosedChannelException If this channel is closed
* @throws IOException If some other I/O error occurs
*/
// 返回此通道(文件)的字节数量
long size() throws IOException;
/**
* Truncates the entity, to which this channel is connected, to the given size.
*
* If the given size is less than the current size then the entity is
* truncated, discarding any bytes beyond the new end. If the given size is
* greater than or equal to the current size then the entity is not modified.
* In either case, if the current position is greater than the given size
* then it is set to that size.
*
* An implementation of this interface may prohibit truncation when
* connected to an entity, typically a file, opened with the {@link
* java.nio.file.StandardOpenOption#APPEND APPEND} option.
*
* @param size The new size, a non-negative byte count
*
* @return This 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
*/
// 用指定的新尺寸size截断通道(文件)
SeekableByteChannel truncate(long size) throws IOException;
}