forked from square/okio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandle.kt
443 lines (391 loc) · 13.6 KB
/
FileHandle.kt
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (C) 2021 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio
/**
* An open file for reading and writing; using either streaming and random access.
*
* Use [read] and [write] to perform one-off random-access reads and writes. Use [source], [sink],
* and [appendingSink] for streaming reads and writes.
*
* File handles must be closed when they are no longer needed. It is an error to read, write, or
* create streams after a file handle is closed. The operating system resources held by a file
* handle will be released once the file handle **and** all of its streams are closed.
*
* Although this class offers both reading and writing APIs, file handle instances may be
* read-only or write-only. For example, a handle to a file on a read-only file system will throw an
* exception if a write is attempted.
*
* File handles may be used by multiple threads concurrently. But the individual sources and sinks
* produced by a file handle are not safe for concurrent use.
*/
abstract class FileHandle(
/**
* True if this handle supports both reading and writing. If this is false all write operations
* including [write], [sink], [resize], and [flush] will all throw [IllegalStateException] if
* called.
*/
val readWrite: Boolean
) : Closeable {
/**
* True once the file handle is closed. Resources should be released with [protectedClose] once
* this is true and [openStreamCount] is 0.
*/
private var closed = false
/**
* Reference count of the number of open sources and sinks on this file handle. Resources should
* be released with [protectedClose] once this is 0 and [closed] is true.
*/
private var openStreamCount = 0
val lock: Lock = newLock()
/**
* Reads at least 1, and up to [byteCount] bytes from this starting at [fileOffset] and copies
* them to [array] at [arrayOffset]. Returns the number of bytes read, or -1 if [fileOffset]
* equals [size].
*/
@Throws(IOException::class)
fun read(
fileOffset: Long,
array: ByteArray,
arrayOffset: Int,
byteCount: Int
): Int {
lock.withLock {
check(!closed) { "closed" }
}
return protectedRead(fileOffset, array, arrayOffset, byteCount)
}
/**
* Reads at least 1, and up to [byteCount] bytes from this starting at [fileOffset] and appends
* them to [sink]. Returns the number of bytes read, or -1 if [fileOffset] equals [size].
*/
@Throws(IOException::class)
fun read(fileOffset: Long, sink: Buffer, byteCount: Long): Long {
lock.withLock {
check(!closed) { "closed" }
}
return readNoCloseCheck(fileOffset, sink, byteCount)
}
/**
* Returns the total number of bytes in the file. This will change if the file size changes.
*/
@Throws(IOException::class)
fun size(): Long {
lock.withLock {
check(!closed) { "closed" }
}
return protectedSize()
}
/**
* Changes the number of bytes in this file to [size]. This will remove bytes from the end if the
* new size is smaller. It will add `0` bytes to the end if it is larger.
*/
@Throws(IOException::class)
fun resize(size: Long) {
check(readWrite) { "file handle is read-only" }
lock.withLock {
check(!closed) { "closed" }
}
return protectedResize(size)
}
/** Reads [byteCount] bytes from [array] and writes them to this at [fileOffset]. */
fun write(
fileOffset: Long,
array: ByteArray,
arrayOffset: Int,
byteCount: Int
) {
check(readWrite) { "file handle is read-only" }
lock.withLock {
check(!closed) { "closed" }
}
return protectedWrite(fileOffset, array, arrayOffset, byteCount)
}
/** Removes [byteCount] bytes from [source] and writes them to this at [fileOffset]. */
@Throws(IOException::class)
fun write(fileOffset: Long, source: Buffer, byteCount: Long) {
check(readWrite) { "file handle is read-only" }
lock.withLock {
check(!closed) { "closed" }
}
writeNoCloseCheck(fileOffset, source, byteCount)
}
/** Pushes all buffered bytes to their final destination. */
@Throws(IOException::class)
fun flush() {
check(readWrite) { "file handle is read-only" }
lock.withLock {
check(!closed) { "closed" }
}
return protectedFlush()
}
/**
* Returns a source that reads from this starting at [fileOffset]. The returned source must be
* closed when it is no longer needed.
*/
@Throws(IOException::class)
fun source(fileOffset: Long = 0L): Source {
lock.withLock {
check(!closed) { "closed" }
openStreamCount++
}
return FileHandleSource(this, fileOffset)
}
/**
* Returns the position of [source] in the file. The argument [source] must be either a source
* produced by this file handle, or a [BufferedSource] that directly wraps such a source. If the
* parameter is a [BufferedSource], it adjusts for buffered bytes.
*/
@Throws(IOException::class)
fun position(source: Source): Long {
var source = source
var bufferSize = 0L
if (source is RealBufferedSource) {
bufferSize = source.buffer.size
source = source.source
}
require(source is FileHandleSource && source.fileHandle === this) {
"source was not created by this FileHandle"
}
check(!source.closed) { "closed" }
return source.position - bufferSize
}
/**
* Change the position of [source] in the file to [position]. The argument [source] must be either
* a source produced by this file handle, or a [BufferedSource] that directly wraps such a source.
* If the parameter is a [BufferedSource], it will skip or clear buffered bytes.
*/
@Throws(IOException::class)
fun reposition(source: Source, position: Long) {
if (source is RealBufferedSource) {
val fileHandleSource = source.source
require(fileHandleSource is FileHandleSource && fileHandleSource.fileHandle === this) {
"source was not created by this FileHandle"
}
check(!fileHandleSource.closed) { "closed" }
val bufferSize = source.buffer.size
val toSkip = position - (fileHandleSource.position - bufferSize)
if (toSkip in 0L until bufferSize) {
// The new position requires only a buffer change.
source.skip(toSkip)
} else {
// The new position doesn't share data with the current buffer.
source.buffer.clear()
fileHandleSource.position = position
}
} else {
require(source is FileHandleSource && source.fileHandle === this) {
"source was not created by this FileHandle"
}
check(!source.closed) { "closed" }
source.position = position
}
}
/**
* Returns a sink that writes to this starting at [fileOffset]. The returned sink must be closed
* when it is no longer needed.
*/
@Throws(IOException::class)
fun sink(fileOffset: Long = 0L): Sink {
check(readWrite) { "file handle is read-only" }
lock.withLock {
check(!closed) { "closed" }
openStreamCount++
}
return FileHandleSink(this, fileOffset)
}
/**
* Returns a sink that writes to this starting at the end. The returned sink must be closed when
* it is no longer needed.
*/
@Throws(IOException::class)
fun appendingSink(): Sink {
return sink(size())
}
/**
* Returns the position of [sink] in the file. The argument [sink] must be either a sink produced
* by this file handle, or a [BufferedSink] that directly wraps such a sink. If the parameter is a
* [BufferedSink], it adjusts for buffered bytes.
*/
@Throws(IOException::class)
fun position(sink: Sink): Long {
var sink = sink
var bufferSize = 0L
if (sink is RealBufferedSink) {
bufferSize = sink.buffer.size
sink = sink.sink
}
require(sink is FileHandleSink && sink.fileHandle === this) {
"sink was not created by this FileHandle"
}
check(!sink.closed) { "closed" }
return sink.position + bufferSize
}
/**
* Change the position of [sink] in the file to [position]. The argument [sink] must be either a
* sink produced by this file handle, or a [BufferedSink] that directly wraps such a sink. If the
* parameter is a [BufferedSink], it emits for buffered bytes.
*/
@Throws(IOException::class)
fun reposition(sink: Sink, position: Long) {
if (sink is RealBufferedSink) {
val fileHandleSink = sink.sink
require(fileHandleSink is FileHandleSink && fileHandleSink.fileHandle === this) {
"sink was not created by this FileHandle"
}
check(!fileHandleSink.closed) { "closed" }
sink.emit()
fileHandleSink.position = position
} else {
require(sink is FileHandleSink && sink.fileHandle === this) {
"sink was not created by this FileHandle"
}
check(!sink.closed) { "closed" }
sink.position = position
}
}
@Throws(IOException::class)
final override fun close() {
lock.withLock {
if (closed) return
closed = true
if (openStreamCount != 0) return
}
protectedClose()
}
/** Like [read] but not performing any close check. */
@Throws(IOException::class)
protected abstract fun protectedRead(
fileOffset: Long,
array: ByteArray,
arrayOffset: Int,
byteCount: Int
): Int
/** Like [write] but not performing any close check. */
@Throws(IOException::class)
protected abstract fun protectedWrite(
fileOffset: Long,
array: ByteArray,
arrayOffset: Int,
byteCount: Int
)
/** Like [flush] but not performing any close check. */
@Throws(IOException::class)
protected abstract fun protectedFlush()
/** Like [resize] but not performing any close check. */
@Throws(IOException::class)
protected abstract fun protectedResize(size: Long)
/** Like [size] but not performing any close check. */
@Throws(IOException::class)
protected abstract fun protectedSize(): Long
/**
* Subclasses should implement this to release resources held by this file handle. It is invoked
* once both the file handle is closed, and also all sources and sinks produced by it are also
* closed.
*/
@Throws(IOException::class)
protected abstract fun protectedClose()
private fun readNoCloseCheck(fileOffset: Long, sink: Buffer, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
var currentOffset = fileOffset
val targetOffset = fileOffset + byteCount
while (currentOffset < targetOffset) {
val tail = sink.writableSegment(1)
val readByteCount = protectedRead(
fileOffset = currentOffset,
array = tail.data,
arrayOffset = tail.limit,
byteCount = minOf(targetOffset - currentOffset, Segment.SIZE - tail.limit).toInt()
)
if (readByteCount == -1) {
if (tail.pos == tail.limit) {
// We allocated a tail segment, but didn't end up needing it. Recycle!
sink.head = tail.pop()
SegmentPool.recycle(tail)
}
if (fileOffset == currentOffset) return -1L // We wanted bytes but didn't return any.
break
}
tail.limit += readByteCount
currentOffset += readByteCount
sink.size += readByteCount
}
return currentOffset - fileOffset
}
private fun writeNoCloseCheck(fileOffset: Long, source: Buffer, byteCount: Long) {
checkOffsetAndCount(source.size, 0L, byteCount)
var currentOffset = fileOffset
val targetOffset = fileOffset + byteCount
while (currentOffset < targetOffset) {
val head = source.head!!
val toCopy = minOf(targetOffset - currentOffset, head.limit - head.pos).toInt()
protectedWrite(currentOffset, head.data, head.pos, toCopy)
head.pos += toCopy
currentOffset += toCopy
source.size -= toCopy
if (head.pos == head.limit) {
source.head = head.pop()
SegmentPool.recycle(head)
}
}
}
private class FileHandleSink(
val fileHandle: FileHandle,
var position: Long
) : Sink {
var closed = false
override fun write(source: Buffer, byteCount: Long) {
check(!closed) { "closed" }
fileHandle.writeNoCloseCheck(position, source, byteCount)
position += byteCount
}
override fun flush() {
check(!closed) { "closed" }
fileHandle.protectedFlush()
}
override fun timeout() = Timeout.NONE
override fun close() {
if (closed) return
closed = true
fileHandle.lock.withLock {
fileHandle.openStreamCount--
if (fileHandle.openStreamCount != 0 || !fileHandle.closed) return@close
}
fileHandle.protectedClose()
}
}
private class FileHandleSource(
val fileHandle: FileHandle,
var position: Long
) : Source {
var closed = false
override fun read(sink: Buffer, byteCount: Long): Long {
check(!closed) { "closed" }
val result = fileHandle.readNoCloseCheck(position, sink, byteCount)
if (result != -1L) position += result
return result
}
override fun timeout() = Timeout.NONE
override fun close() {
if (closed) return
closed = true
fileHandle.lock.withLock {
fileHandle.openStreamCount--
if (fileHandle.openStreamCount != 0 || !fileHandle.closed) return@close
}
fileHandle.protectedClose()
}
}
}