Skip to content

Commit 62974a0

Browse files
author
Brian Norman
committed
Unsigned numbers in Okio
Add support for the experimental unsigned numbers introduced in Kotlin 1.3. There are new inline extension functions on BufferedSource and BufferedSink which allow reading and writing with unsigned numbers.
1 parent 0078fca commit 62974a0

File tree

2 files changed

+651
-0
lines changed

2 files changed

+651
-0
lines changed
Lines changed: 371 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,371 @@
1+
/*
2+
* Copyright (C) 2018 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:JvmName("-Unsigned")
18+
@file:Suppress("NOTHING_TO_INLINE") // Syntactic sugar.
19+
20+
package okio
21+
22+
import java.io.IOException
23+
24+
/** Writes a unsigned byte to this sink. */
25+
@ExperimentalUnsignedTypes
26+
@Throws(IOException::class)
27+
inline fun <S : BufferedSink> S.writeUByte(b: UByte): S {
28+
writeByte(b.toByte().toInt())
29+
return this
30+
}
31+
32+
/**
33+
* Writes a big-endian, unsigned short to this sink using two bytes.
34+
* ```
35+
* val buffer = Buffer()
36+
* buffer.writeUShort(65534u.toUShort())
37+
* buffer.writeUShort(15u.toUShort())
38+
*
39+
* assertEquals(4, buffer.size)
40+
* assertEquals(0xff.toByte(), buffer.readByte())
41+
* assertEquals(0xfe.toByte(), buffer.readByte())
42+
* assertEquals(0x00.toByte(), buffer.readByte())
43+
* assertEquals(0x0f.toByte(), buffer.readByte())
44+
* assertEquals(0, buffer.size)
45+
* ```
46+
*/
47+
@ExperimentalUnsignedTypes
48+
@Throws(IOException::class)
49+
inline fun <S : BufferedSink> S.writeUShort(b: UShort): S {
50+
writeShort(b.toShort().toInt())
51+
return this
52+
}
53+
54+
/**
55+
* Writes a little-endian, unsigned short to this sink using two bytes.
56+
* ```
57+
* val buffer = Buffer()
58+
* buffer.writeUShortLe(65534u.toUShort())
59+
* buffer.writeUShortLe(15u.toUShort())
60+
*
61+
* assertEquals(4, buffer.size)
62+
* assertEquals(0xfe.toByte(), buffer.readByte())
63+
* assertEquals(0xff.toByte(), buffer.readByte())
64+
* assertEquals(0x0f.toByte(), buffer.readByte())
65+
* assertEquals(0x00.toByte(), buffer.readByte())
66+
* assertEquals(0, buffer.size)
67+
* ```
68+
*/
69+
@ExperimentalUnsignedTypes
70+
@Throws(IOException::class)
71+
inline fun <S : BufferedSink> S.writeUShortLe(b: UShort): S {
72+
writeShortLe(b.toShort().toInt())
73+
return this
74+
}
75+
76+
/**
77+
* Writes a big-endian, unsigned int to this sink using four bytes.
78+
* ```
79+
* val buffer = Buffer()
80+
* buffer.writeUInt(4294967294u)
81+
* buffer.writeUInt(15u)
82+
*
83+
* assertEquals(8, buffer.size)
84+
* assertEquals(0xff.toByte(), buffer.readByte())
85+
* assertEquals(0xff.toByte(), buffer.readByte())
86+
* assertEquals(0xff.toByte(), buffer.readByte())
87+
* assertEquals(0xfe.toByte(), buffer.readByte())
88+
* assertEquals(0x00.toByte(), buffer.readByte())
89+
* assertEquals(0x00.toByte(), buffer.readByte())
90+
* assertEquals(0x00.toByte(), buffer.readByte())
91+
* assertEquals(0x0f.toByte(), buffer.readByte())
92+
* assertEquals(0, buffer.size)
93+
* ```
94+
*/
95+
@ExperimentalUnsignedTypes
96+
@Throws(IOException::class)
97+
inline fun <S : BufferedSink> S.writeUInt(b: UInt): S {
98+
writeInt(b.toInt())
99+
return this
100+
}
101+
102+
/**
103+
* Writes a little-endian, unsigned int to this sink using four bytes.
104+
* ```
105+
* val buffer = Buffer()
106+
* buffer.writeUIntLe(4294967294u)
107+
* buffer.writeUIntLe(15u)
108+
*
109+
* assertEquals(8, buffer.size)
110+
* assertEquals(0xfe.toByte(), buffer.readByte())
111+
* assertEquals(0xff.toByte(), buffer.readByte())
112+
* assertEquals(0xff.toByte(), buffer.readByte())
113+
* assertEquals(0xff.toByte(), buffer.readByte())
114+
* assertEquals(0x0f.toByte(), buffer.readByte())
115+
* assertEquals(0x00.toByte(), buffer.readByte())
116+
* assertEquals(0x00.toByte(), buffer.readByte())
117+
* assertEquals(0x00.toByte(), buffer.readByte())
118+
* assertEquals(0, buffer.size)
119+
* ```
120+
*/
121+
@ExperimentalUnsignedTypes
122+
@Throws(IOException::class)
123+
inline fun <S : BufferedSink> S.writeUIntLe(b: UInt): S {
124+
writeIntLe(b.toInt())
125+
return this
126+
}
127+
128+
/**
129+
* Writes a big-endian, unsigned long to this sink using eight bytes.
130+
* ```
131+
* val buffer = Buffer()
132+
* buffer.writeULong(18446744073709551614uL)
133+
* buffer.writeULong(15u)
134+
*
135+
* assertEquals(16, buffer.size)
136+
* assertEquals(0xff.toByte(), buffer.readByte())
137+
* assertEquals(0xff.toByte(), buffer.readByte())
138+
* assertEquals(0xff.toByte(), buffer.readByte())
139+
* assertEquals(0xff.toByte(), buffer.readByte())
140+
* assertEquals(0xff.toByte(), buffer.readByte())
141+
* assertEquals(0xff.toByte(), buffer.readByte())
142+
* assertEquals(0xff.toByte(), buffer.readByte())
143+
* assertEquals(0xfe.toByte(), buffer.readByte())
144+
* assertEquals(0x00.toByte(), buffer.readByte())
145+
* assertEquals(0x00.toByte(), buffer.readByte())
146+
* assertEquals(0x00.toByte(), buffer.readByte())
147+
* assertEquals(0x00.toByte(), buffer.readByte())
148+
* assertEquals(0x00.toByte(), buffer.readByte())
149+
* assertEquals(0x00.toByte(), buffer.readByte())
150+
* assertEquals(0x00.toByte(), buffer.readByte())
151+
* assertEquals(0x0f.toByte(), buffer.readByte())
152+
* assertEquals(0, buffer.size)
153+
* ```
154+
*/
155+
@ExperimentalUnsignedTypes
156+
@Throws(IOException::class)
157+
inline fun <S : BufferedSink> S.writeULong(b: ULong): S {
158+
writeLong(b.toLong())
159+
return this
160+
}
161+
162+
/**
163+
* Writes a little-endian, unsigned long to this sink using eight bytes.
164+
* ```
165+
* val buffer = Buffer()
166+
* buffer.writeULongLe(18446744073709551614uL)
167+
* buffer.writeULongLe(15uL)
168+
*
169+
* assertEquals(16, buffer.size)
170+
* assertEquals(0xfe.toByte(), buffer.readByte())
171+
* assertEquals(0xff.toByte(), buffer.readByte())
172+
* assertEquals(0xff.toByte(), buffer.readByte())
173+
* assertEquals(0xff.toByte(), buffer.readByte())
174+
* assertEquals(0xff.toByte(), buffer.readByte())
175+
* assertEquals(0xff.toByte(), buffer.readByte())
176+
* assertEquals(0xff.toByte(), buffer.readByte())
177+
* assertEquals(0xff.toByte(), buffer.readByte())
178+
* assertEquals(0x0f.toByte(), buffer.readByte())
179+
* assertEquals(0x00.toByte(), buffer.readByte())
180+
* assertEquals(0x00.toByte(), buffer.readByte())
181+
* assertEquals(0x00.toByte(), buffer.readByte())
182+
* assertEquals(0x00.toByte(), buffer.readByte())
183+
* assertEquals(0x00.toByte(), buffer.readByte())
184+
* assertEquals(0x00.toByte(), buffer.readByte())
185+
* assertEquals(0x00.toByte(), buffer.readByte())
186+
* assertEquals(0, buffer.size)
187+
* ```
188+
*/
189+
@ExperimentalUnsignedTypes
190+
@Throws(IOException::class)
191+
inline fun <S : BufferedSink> S.writeULongLe(b: ULong): S {
192+
writeLongLe(b.toLong())
193+
return this
194+
}
195+
196+
/** Removes an unsigned byte from this source and returns it. */
197+
@ExperimentalUnsignedTypes
198+
@Throws(IOException::class)
199+
inline fun BufferedSource.readUByte(): UByte {
200+
return readByte().toUByte()
201+
}
202+
203+
/**
204+
* Removes two bytes from this source and returns a big-endian, unsigned short.
205+
* ```
206+
* val buffer = Buffer()
207+
* .writeByte(0xff)
208+
* .writeByte(0xfe)
209+
* .writeByte(0x00)
210+
* .writeByte(0x0f)
211+
* assertEquals(4, buffer.size)
212+
*
213+
* assertEquals(65534u.toUShort(), buffer.readUShort())
214+
* assertEquals(2, buffer.size)
215+
*
216+
* assertEquals(15u.toUShort(), buffer.readUShort())
217+
* assertEquals(0, buffer.size)
218+
* ```
219+
*/
220+
@ExperimentalUnsignedTypes
221+
@Throws(IOException::class)
222+
inline fun BufferedSource.readUShort(): UShort {
223+
return readShort().toUShort()
224+
}
225+
226+
/**
227+
* Removes two bytes from this source and returns a little-endian, unsigned short.
228+
* ```
229+
* val buffer = Buffer()
230+
* .writeByte(0xfe)
231+
* .writeByte(0xff)
232+
* .writeByte(0x0f)
233+
* .writeByte(0x00)
234+
* assertEquals(4, buffer.size)
235+
*
236+
* assertEquals(65534u.toUShort(), buffer.readUShortLe())
237+
* assertEquals(2, buffer.size)
238+
*
239+
* assertEquals(15u.toUShort(), buffer.readUShortLe())
240+
* assertEquals(0, buffer.size)
241+
* ```
242+
*/
243+
@ExperimentalUnsignedTypes
244+
@Throws(IOException::class)
245+
inline fun BufferedSource.readUShortLe(): UShort {
246+
return readShortLe().toUShort()
247+
}
248+
249+
/**
250+
* Removes four bytes from this source and returns a big-endian, unsigned int.
251+
* ```
252+
* val buffer = Buffer()
253+
* .writeByte(0xff)
254+
* .writeByte(0xff)
255+
* .writeByte(0xff)
256+
* .writeByte(0xfe)
257+
* .writeByte(0x00)
258+
* .writeByte(0x00)
259+
* .writeByte(0x00)
260+
* .writeByte(0x0f)
261+
* assertEquals(4, buffer.size)
262+
*
263+
* assertEquals(4294967294u, buffer.readUInt())
264+
* assertEquals(2, buffer.size)
265+
*
266+
* assertEquals(15u, buffer.readUInt())
267+
* assertEquals(0, buffer.size)
268+
* ```
269+
*/
270+
@ExperimentalUnsignedTypes
271+
@Throws(IOException::class)
272+
inline fun BufferedSource.readUInt(): UInt {
273+
return readInt().toUInt()
274+
}
275+
276+
/**
277+
* Removes four bytes from this source and returns a little-endian, unsigned int.
278+
* ```
279+
* val buffer = Buffer()
280+
* .writeByte(0xfe)
281+
* .writeByte(0xff)
282+
* .writeByte(0xff)
283+
* .writeByte(0xff)
284+
* .writeByte(0x0f)
285+
* .writeByte(0x00)
286+
* .writeByte(0x00)
287+
* .writeByte(0x00)
288+
* assertEquals(8, buffer.size)
289+
*
290+
* assertEquals(4294967294u, buffer.readUIntLe())
291+
* assertEquals(4, buffer.size)
292+
*
293+
* assertEquals(15u, buffer.readUIntLe())
294+
* assertEquals(0, buffer.size)
295+
* ```
296+
*/
297+
@ExperimentalUnsignedTypes
298+
@Throws(IOException::class)
299+
inline fun BufferedSource.readUIntLe(): UInt {
300+
return readIntLe().toUInt()
301+
}
302+
303+
/**
304+
* Removes eight bytes from this source and returns a big-endian, unsigned long.
305+
* ```
306+
* val buffer = Buffer()
307+
* .writeByte(0xff)
308+
* .writeByte(0xff)
309+
* .writeByte(0xff)
310+
* .writeByte(0xff)
311+
* .writeByte(0xff)
312+
* .writeByte(0xff)
313+
* .writeByte(0xff)
314+
* .writeByte(0xfe)
315+
* .writeByte(0x00)
316+
* .writeByte(0x00)
317+
* .writeByte(0x00)
318+
* .writeByte(0x00)
319+
* .writeByte(0x00)
320+
* .writeByte(0x00)
321+
* .writeByte(0x00)
322+
* .writeByte(0x0f)
323+
* assertEquals(16, buffer.size)
324+
*
325+
* assertEquals(18446744073709551614uL, buffer.readULong())
326+
* assertEquals(8, buffer.size)
327+
*
328+
* assertEquals(15u, buffer.readULong())
329+
* assertEquals(0, buffer.size)
330+
* ```
331+
*/
332+
@ExperimentalUnsignedTypes
333+
@Throws(IOException::class)
334+
inline fun BufferedSource.readULong(): ULong {
335+
return readLong().toULong()
336+
}
337+
338+
/**
339+
* Removes eight bytes from this source and returns a little-endian, unsigned long.
340+
* ```
341+
* val buffer = Buffer()
342+
* .writeByte(0xfe)
343+
* .writeByte(0xff)
344+
* .writeByte(0xff)
345+
* .writeByte(0xff)
346+
* .writeByte(0xff)
347+
* .writeByte(0xff)
348+
* .writeByte(0xff)
349+
* .writeByte(0xff)
350+
* .writeByte(0x0f)
351+
* .writeByte(0x00)
352+
* .writeByte(0x00)
353+
* .writeByte(0x00)
354+
* .writeByte(0x00)
355+
* .writeByte(0x00)
356+
* .writeByte(0x00)
357+
* .writeByte(0x00)
358+
* assertEquals(16, buffer.size)
359+
*
360+
* assertEquals(18446744073709551614uL, buffer.readULongLe())
361+
* assertEquals(8, buffer.size)
362+
*
363+
* assertEquals(15u, buffer.readULongLe())
364+
* assertEquals(0, buffer.size)
365+
* ```
366+
*/
367+
@ExperimentalUnsignedTypes
368+
@Throws(IOException::class)
369+
inline fun BufferedSource.readULongLe(): ULong {
370+
return readLongLe().toULong()
371+
}

0 commit comments

Comments
 (0)