Skip to content

Commit 5e0317e

Browse files
author
Brian Norman
committed
Unsigned numbers in Okio
Work in progress, needs a better upgrade to Kotlin 1.3
1 parent 0078fca commit 5e0317e

File tree

2 files changed

+660
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)