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