-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathInputOperations.kt
230 lines (197 loc) · 6.01 KB
/
InputOperations.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
package kotlinx.io
import kotlinx.io.buffer.*
import kotlin.math.*
/**
* Copies the [Input] content to [destination].
* The [Input] is not closed at the end of the copy.
*
* @return transferred bytes count.
*/
public fun Input.copyTo(destination: Output): Int {
var count = 0
// This method isn't using `eof()` to avoid a copy in `prefetch()`.
while (true) {
val chunkSize = readAvailableTo(destination)
if (chunkSize == 0) {
break
}
count += chunkSize
}
return count
}
/**
* Copies [size] bytes from the [Input] to the given [destination].
* The [Input] is not closed at the end of the copy.
*
* @throws EOFException if Input cannot provide enough bytes.
* @return transferred bytes count.
*/
public fun Input.copyTo(destination: Output, size: Int): Int {
checkSize(size)
// This method isn't using `eof()` to avoid a copy in `prefetch()`
var remaining = size
while (remaining > 0) {
val chunkSize = destination.writeBuffer { buffer, startIndex, endIndex ->
val length = min(remaining, endIndex - startIndex)
readAvailableTo(buffer, startIndex, startIndex + length)
}
if (chunkSize == 0) {
throw EOFException("cannot read more bytes from closed Input[$this]. Expected ${size - remaining} of $size")
}
remaining -= chunkSize
}
return size
}
/**
* Reads [ByteArray] of fixed [size] from [Input].
* The [Input] is not closed at the end of the reading.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readByteArray(size: Int): ByteArray {
checkSize(size)
val result = ByteArray(size)
var position = size
while (position < size) {
val count = readBufferRange { buffer, startOffset, endOffset ->
val length = min(endOffset - startOffset, size - position)
buffer.copyTo(result, startOffset, length, position)
length
}
if (count == 0) {
throw EOFException("cannot read more bytes from closed Input[$this]. Expected ${size - position} of $size")
}
position += count
}
return result
}
/**
* Reads entire [Input] to [ByteArray].
* The [Input] is not closed at the end of the copy.
*/
public fun Input.readByteArray(): ByteArray {
val result = ByteArrayOutput()
while (true) {
if (readAvailableTo(result) == 0) {
break
}
}
return result.toByteArray()
}
/**
* Reads [length] bytes from [Input] to [array] from [startIndex].
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readByteArray(array: ByteArray, startIndex: Int = 0, length: Int = array.size - startIndex) {
checkArrayStartAndLength(array, startIndex, length)
var remaining = length
var consumed = 0
while (remaining > 0) {
val count = readBufferRange { buffer, bufferStart, bufferEnd ->
val size = minOf(bufferEnd - bufferStart, remaining)
buffer.loadByteArray(bufferStart, array, startIndex + consumed, size)
consumed += size
remaining -= size
size
}
if (count == 0) {
throw EOFException("No more bytes available.")
}
}
}
/**
* Reads [length] bytes from [Input] to [array] from [startIndex].
*
* @throws EOFException if Input cannot provide enough bytes.
*/
@ExperimentalUnsignedTypes
public fun Input.readByteArray(array: UByteArray, startIndex: Int = 0, length: Int = array.size - startIndex) {
readByteArray(array.asByteArray(), startIndex, length)
}
/**
* Discards exact [count] of bytes.
*
* @throws [EOFException] if this input ends before discard finished.
* @return discarded bytes count
*/
public fun Input.discardExact(count: Int): Int {
checkCount(count)
val skipped = discard(count)
if (skipped < count) {
unexpectedEOF("Fail to skip $count bytes, remaining ${count - skipped}.")
}
return count
}
/**
* Reads a [Byte] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readByte(): Byte = readPrimitive(
1
) { buffer, offset -> buffer.loadByteAt(offset).toLong() }.toByte()
/**
* Reads a single [Short] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readShort(): Short = readPrimitive(
2
) { buffer, offset -> buffer.loadShortAt(offset).toLong() }.toShort()
/**
* Reads a single [Int] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readInt(): Int = readPrimitive(
4
) { buffer, offset -> buffer.loadIntAt(offset).toLong() }.toInt()
/**
* Reads a single [Long] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readLong(): Long = readPrimitive(
8
) { buffer, offset -> buffer.loadLongAt(offset) }
/**
* Reads an [UByte] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
@ExperimentalUnsignedTypes
public fun Input.readUByte(): UByte = readByte().toUByte()
/**
* Reads a [ULong] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
@ExperimentalUnsignedTypes
public fun Input.readULong(): ULong = readLong().toULong()
/**
* Reads an [UInt] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
@ExperimentalUnsignedTypes
public fun Input.readUInt(): UInt = readInt().toUInt()
/**
* Reads an [UShort] from this Input.
*
* @throws EOFException if Input cannot provide enough bytes.
*/
@ExperimentalUnsignedTypes
public fun Input.readUShort(): UShort = readShort().toUShort()
/**
* Reads a [Double] from the current [Input].
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readDouble(): Double = Double.fromBits(readLong())
/**
* Reads a [Float] from the current [Input].
*
* @throws EOFException if Input cannot provide enough bytes.
*/
public fun Input.readFloat(): Float = Float.fromBits(readInt())