| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public class ByteBuffer |
| | 8 | | { |
| 0 | 9 | | public static ByteOrder nativeOrder() => NativeOrder._o; |
| | 10 | |
|
| | 11 | | public static ByteBuffer allocate(int capacity) |
| | 12 | | { |
| 1 | 13 | | if (capacity < 0) |
| | 14 | | { |
| 0 | 15 | | throwOutOfRange("capacity", capacity, "capacity must be non-negative"); |
| | 16 | | } |
| 1 | 17 | | var ret = new ByteBuffer(); |
| 1 | 18 | | ret._position = 0; |
| 1 | 19 | | ret._limit = capacity; |
| 1 | 20 | | ret._capacity = capacity; |
| 1 | 21 | | ret._bytes = new byte[capacity]; |
| 1 | 22 | | ret._valBytes = new ValBytes(); |
| 1 | 23 | | return ret; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static ByteBuffer wrap(byte[] bytes) |
| | 27 | | { |
| 1 | 28 | | var ret = new ByteBuffer(); |
| 1 | 29 | | ret._position = 0; |
| 1 | 30 | | ret._limit = bytes.Length; |
| 1 | 31 | | ret._capacity = bytes.Length; |
| 1 | 32 | | ret._bytes = bytes; |
| 1 | 33 | | ret._valBytes = new ValBytes(); |
| 1 | 34 | | return ret; |
| | 35 | | } |
| | 36 | |
|
| 1 | 37 | | public ByteBuffer() => _order = ByteOrder.BigEndian; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Represents the endianness of the buffer. |
| | 41 | | /// </summary> |
| | 42 | | public enum ByteOrder |
| | 43 | | { |
| | 44 | | /// <summary> |
| | 45 | | /// Big endian byte order. |
| | 46 | | /// </summary> |
| | 47 | | BigEndian, |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Little endian byte order. |
| | 51 | | /// </summary> |
| | 52 | | LittleEndian |
| | 53 | | } |
| | 54 | |
|
| 1 | 55 | | public ByteOrder order() => _order; |
| | 56 | |
|
| | 57 | | public ByteBuffer order(ByteOrder bo) |
| | 58 | | { |
| 1 | 59 | | _order = bo; |
| 1 | 60 | | return this; |
| | 61 | | } |
| | 62 | |
|
| 1 | 63 | | public int position() => _position; |
| | 64 | |
|
| | 65 | | public ByteBuffer position(int pos) |
| | 66 | | { |
| 1 | 67 | | if (pos < 0) |
| | 68 | | { |
| 0 | 69 | | throwOutOfRange("pos", pos, "position must be non-negative"); |
| | 70 | | } |
| 1 | 71 | | if (pos > _limit) |
| | 72 | | { |
| 0 | 73 | | throwOutOfRange("pos", pos, "position must be less than limit"); |
| | 74 | | } |
| 1 | 75 | | _position = pos; |
| 1 | 76 | | return this; |
| | 77 | | } |
| | 78 | |
|
| 1 | 79 | | public int limit() => _limit; |
| | 80 | |
|
| | 81 | | public ByteBuffer limit(int newLimit) |
| | 82 | | { |
| 1 | 83 | | if (newLimit < 0) |
| | 84 | | { |
| 0 | 85 | | throwOutOfRange("newLimit", newLimit, "limit must be non-negative"); |
| | 86 | | } |
| 1 | 87 | | if (newLimit > _capacity) |
| | 88 | | { |
| 0 | 89 | | throwOutOfRange("newLimit", newLimit, "limit must be less than capacity"); |
| | 90 | | } |
| 1 | 91 | | _limit = newLimit; |
| 1 | 92 | | return this; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public void clear() |
| | 96 | | { |
| 0 | 97 | | _position = 0; |
| 0 | 98 | | _limit = _capacity; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public void flip() |
| | 102 | | { |
| 1 | 103 | | _limit = _position; |
| 1 | 104 | | _position = 0; |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | public void compact() |
| | 108 | | { |
| 1 | 109 | | if (_position < _limit) |
| | 110 | | { |
| 1 | 111 | | int n = _limit - _position; |
| 1 | 112 | | System.Buffer.BlockCopy(_bytes, _position, _bytes, 0, n); |
| 1 | 113 | | _position = n; |
| | 114 | | } |
| | 115 | | else |
| | 116 | | { |
| 0 | 117 | | _position = 0; |
| | 118 | | } |
| 1 | 119 | | _limit = _capacity; |
| 1 | 120 | | } |
| | 121 | |
|
| 1 | 122 | | public int remaining() => _limit - _position; |
| | 123 | |
|
| 1 | 124 | | public bool hasRemaining() => _position < _limit; |
| | 125 | |
|
| 1 | 126 | | public int capacity() => _capacity; |
| | 127 | |
|
| | 128 | | public byte[] toArray() |
| | 129 | | { |
| 0 | 130 | | int len = remaining(); |
| 0 | 131 | | byte[] rc = new byte[len]; |
| 0 | 132 | | System.Buffer.BlockCopy(_bytes, 0, rc, 0, len); |
| 0 | 133 | | return rc; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | public byte[] toArray(int startIndex, int length) |
| | 137 | | { |
| 0 | 138 | | if (startIndex < 0) |
| | 139 | | { |
| 0 | 140 | | throwOutOfRange("startIndex", startIndex, "startIndex must be non-negative"); |
| | 141 | | } |
| 0 | 142 | | if (startIndex >= _position) |
| | 143 | | { |
| 0 | 144 | | throwOutOfRange("startIndex", startIndex, "startIndex must be less than position"); |
| | 145 | | } |
| 0 | 146 | | if (length < 0) |
| | 147 | | { |
| 0 | 148 | | throwOutOfRange("length", length, "length must be non-negative"); |
| | 149 | | } |
| 0 | 150 | | if (startIndex + length > _position) |
| | 151 | | { |
| 0 | 152 | | throw new ArgumentException("startIndex + length must not exceed end mark of buffer"); |
| | 153 | | } |
| 0 | 154 | | byte[] rc = new byte[length]; |
| 0 | 155 | | System.Buffer.BlockCopy(_bytes, startIndex, rc, 0, length); |
| 0 | 156 | | return rc; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | public ByteBuffer put(ByteBuffer buf) |
| | 160 | | { |
| 1 | 161 | | int len = buf.remaining(); |
| 1 | 162 | | checkOverflow(len); |
| 1 | 163 | | System.Buffer.BlockCopy(buf._bytes, buf._position, _bytes, _position, len); |
| 1 | 164 | | _position += len; |
| 1 | 165 | | return this; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public byte get() |
| | 169 | | { |
| 1 | 170 | | checkUnderflow(1); |
| 1 | 171 | | return System.Buffer.GetByte(_bytes, _position++); |
| | 172 | | } |
| | 173 | |
|
| 1 | 174 | | public byte get(int pos) => System.Buffer.GetByte(_bytes, pos); |
| | 175 | |
|
| 1 | 176 | | public ByteBuffer get(byte[] b) => get(b, 0, System.Buffer.ByteLength(b)); |
| | 177 | |
|
| | 178 | | public ByteBuffer get(byte[] b, int offset, int length) |
| | 179 | | { |
| 1 | 180 | | if (offset < 0) |
| | 181 | | { |
| 0 | 182 | | throwOutOfRange("offset", offset, "offset must be non-negative"); |
| | 183 | | } |
| 1 | 184 | | if (offset + length > System.Buffer.ByteLength(b)) |
| | 185 | | { |
| 0 | 186 | | throwOutOfRange("length", length, "insufficient room beyond given offset in destination array"); |
| | 187 | | } |
| 1 | 188 | | checkUnderflow(length); |
| 1 | 189 | | System.Buffer.BlockCopy(_bytes, _position, b, offset, length); |
| 1 | 190 | | _position += length; |
| 1 | 191 | | return this; |
| | 192 | | } |
| | 193 | |
|
| | 194 | | public ByteBuffer put(byte b) |
| | 195 | | { |
| 1 | 196 | | checkOverflow(1); |
| 1 | 197 | | _bytes[_position++] = b; |
| 1 | 198 | | return this; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | public ByteBuffer put(int pos, byte b) |
| | 202 | | { |
| 1 | 203 | | _bytes[pos] = b; |
| 1 | 204 | | return this; |
| | 205 | | } |
| | 206 | |
|
| 1 | 207 | | public ByteBuffer put(byte[] b) => put(b, 0, System.Buffer.ByteLength(b)); |
| | 208 | |
|
| | 209 | | public ByteBuffer put(byte[] b, int offset, int length) |
| | 210 | | { |
| 1 | 211 | | if (offset < 0) |
| | 212 | | { |
| 0 | 213 | | throwOutOfRange("offset", offset, "offset must be non-negative"); |
| | 214 | | } |
| 1 | 215 | | if (offset + length > System.Buffer.ByteLength(b)) |
| | 216 | | { |
| 0 | 217 | | throwOutOfRange("length", length, "insufficient data beyond given offset in source array"); |
| | 218 | | } |
| 1 | 219 | | if (length > 0) |
| | 220 | | { |
| 1 | 221 | | checkOverflow(length); |
| 1 | 222 | | System.Buffer.BlockCopy(b, offset, _bytes, _position, length); |
| 1 | 223 | | _position += length; |
| | 224 | | } |
| 1 | 225 | | return this; |
| | 226 | | } |
| | 227 | |
|
| 0 | 228 | | public bool getBool() => get() == 1; |
| | 229 | |
|
| | 230 | | public void getBoolSeq(bool[] seq) |
| | 231 | | { |
| 1 | 232 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 233 | | checkUnderflow(len); |
| 1 | 234 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| 1 | 235 | | _position += len; |
| 1 | 236 | | } |
| | 237 | |
|
| 1 | 238 | | public ByteBuffer putBool(bool b) => put(b ? (byte)1 : (byte)0); |
| | 239 | |
|
| | 240 | | public ByteBuffer putBoolSeq(bool[] seq) |
| | 241 | | { |
| 1 | 242 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 243 | | checkOverflow(len); |
| 1 | 244 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| 1 | 245 | | _position += len; |
| 1 | 246 | | return this; |
| | 247 | | } |
| | 248 | |
|
| | 249 | | [StructLayout(LayoutKind.Explicit)] |
| | 250 | | private struct ValBytes |
| | 251 | | { |
| | 252 | | [FieldOffset(0)] |
| | 253 | | public short shortVal; |
| | 254 | |
|
| | 255 | | [FieldOffset(0)] |
| | 256 | | public int intVal; |
| | 257 | |
|
| | 258 | | [FieldOffset(0)] |
| | 259 | | public long longVal; |
| | 260 | |
|
| | 261 | | [FieldOffset(0)] |
| | 262 | | public float floatVal; |
| | 263 | |
|
| | 264 | | [FieldOffset(0)] |
| | 265 | | public double doubleVal; |
| | 266 | |
|
| | 267 | | [FieldOffset(0)] |
| | 268 | | public byte b0; |
| | 269 | | [FieldOffset(1)] |
| | 270 | | public byte b1; |
| | 271 | | [FieldOffset(2)] |
| | 272 | | public byte b2; |
| | 273 | | [FieldOffset(3)] |
| | 274 | | public byte b3; |
| | 275 | | [FieldOffset(4)] |
| | 276 | | public byte b4; |
| | 277 | | [FieldOffset(5)] |
| | 278 | | public byte b5; |
| | 279 | | [FieldOffset(6)] |
| | 280 | | public byte b6; |
| | 281 | | [FieldOffset(7)] |
| | 282 | | public byte b7; |
| | 283 | | } |
| | 284 | |
|
| | 285 | | public short getShort() |
| | 286 | | { |
| 1 | 287 | | short v = getShort(_position); |
| 1 | 288 | | _position += 2; |
| 1 | 289 | | return v; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | public short getShort(int pos) |
| | 293 | | { |
| 1 | 294 | | checkUnderflow(pos, 2); |
| 1 | 295 | | if (_order == NativeOrder._o) |
| | 296 | | { |
| 1 | 297 | | _valBytes.b0 = _bytes[pos]; |
| 1 | 298 | | _valBytes.b1 = _bytes[pos + 1]; |
| | 299 | | } |
| | 300 | | else |
| | 301 | | { |
| 1 | 302 | | _valBytes.b1 = _bytes[pos]; |
| 1 | 303 | | _valBytes.b0 = _bytes[pos + 1]; |
| | 304 | | } |
| 1 | 305 | | return _valBytes.shortVal; |
| | 306 | | } |
| | 307 | |
|
| | 308 | | public void getShortSeq(short[] seq) |
| | 309 | | { |
| 1 | 310 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 311 | | checkUnderflow(len); |
| 1 | 312 | | if (_order == NativeOrder._o) |
| | 313 | | { |
| 1 | 314 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| | 315 | | } |
| | 316 | | else |
| | 317 | | { |
| 0 | 318 | | for (int i = 0; i < seq.Length; ++i) |
| | 319 | | { |
| 0 | 320 | | int index = _position + (i * 2); |
| 0 | 321 | | _valBytes.b1 = _bytes[index]; |
| 0 | 322 | | _valBytes.b0 = _bytes[index + 1]; |
| 0 | 323 | | seq[i] = _valBytes.shortVal; |
| | 324 | | } |
| | 325 | | } |
| 1 | 326 | | _position += len; |
| 1 | 327 | | } |
| | 328 | |
|
| | 329 | | public ByteBuffer putShort(short val) |
| | 330 | | { |
| 1 | 331 | | checkOverflow(2); |
| 1 | 332 | | _valBytes.shortVal = val; |
| 1 | 333 | | if (_order == NativeOrder._o) |
| | 334 | | { |
| 1 | 335 | | _bytes[_position] = _valBytes.b0; |
| 1 | 336 | | _bytes[_position + 1] = _valBytes.b1; |
| | 337 | | } |
| | 338 | | else |
| | 339 | | { |
| 1 | 340 | | _bytes[_position + 1] = _valBytes.b0; |
| 1 | 341 | | _bytes[_position] = _valBytes.b1; |
| | 342 | | } |
| 1 | 343 | | _position += 2; |
| 1 | 344 | | return this; |
| | 345 | | } |
| | 346 | |
|
| | 347 | | public ByteBuffer putShortSeq(short[] seq) |
| | 348 | | { |
| 1 | 349 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 350 | | checkOverflow(len); |
| 1 | 351 | | if (_order == NativeOrder._o) |
| | 352 | | { |
| 1 | 353 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| | 354 | | } |
| | 355 | | else |
| | 356 | | { |
| 0 | 357 | | for (int i = 0; i < seq.Length; ++i) |
| | 358 | | { |
| 0 | 359 | | int index = _position + (i * 2); |
| 0 | 360 | | _valBytes.shortVal = seq[i]; |
| 0 | 361 | | _bytes[index + 1] = _valBytes.b0; |
| 0 | 362 | | _bytes[index] = _valBytes.b1; |
| | 363 | | } |
| | 364 | | } |
| 1 | 365 | | _position += len; |
| 1 | 366 | | return this; |
| | 367 | | } |
| | 368 | |
|
| | 369 | | public int getInt() |
| | 370 | | { |
| 1 | 371 | | checkUnderflow(4); |
| 1 | 372 | | if (_order == NativeOrder._o) |
| | 373 | | { |
| 1 | 374 | | _valBytes.b0 = _bytes[_position]; |
| 1 | 375 | | _valBytes.b1 = _bytes[_position + 1]; |
| 1 | 376 | | _valBytes.b2 = _bytes[_position + 2]; |
| 1 | 377 | | _valBytes.b3 = _bytes[_position + 3]; |
| | 378 | | } |
| | 379 | | else |
| | 380 | | { |
| 0 | 381 | | _valBytes.b3 = _bytes[_position]; |
| 0 | 382 | | _valBytes.b2 = _bytes[_position + 1]; |
| 0 | 383 | | _valBytes.b1 = _bytes[_position + 2]; |
| 0 | 384 | | _valBytes.b0 = _bytes[_position + 3]; |
| | 385 | | } |
| 1 | 386 | | _position += 4; |
| 1 | 387 | | return _valBytes.intVal; |
| | 388 | | } |
| | 389 | |
|
| | 390 | | public void getIntSeq(int[] seq) |
| | 391 | | { |
| 1 | 392 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 393 | | checkUnderflow(len); |
| 1 | 394 | | if (_order == NativeOrder._o) |
| | 395 | | { |
| 1 | 396 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| | 397 | | } |
| | 398 | | else |
| | 399 | | { |
| 0 | 400 | | for (int i = 0; i < seq.Length; ++i) |
| | 401 | | { |
| 0 | 402 | | int index = _position + (i * 4); |
| 0 | 403 | | _valBytes.b3 = _bytes[index]; |
| 0 | 404 | | _valBytes.b2 = _bytes[index + 1]; |
| 0 | 405 | | _valBytes.b1 = _bytes[index + 2]; |
| 0 | 406 | | _valBytes.b0 = _bytes[index + 3]; |
| 0 | 407 | | seq[i] = _valBytes.intVal; |
| | 408 | | } |
| | 409 | | } |
| 1 | 410 | | _position += len; |
| 1 | 411 | | } |
| | 412 | |
|
| | 413 | | public ByteBuffer putInt(int val) |
| | 414 | | { |
| 1 | 415 | | putInt(_position, val); |
| 1 | 416 | | _position += 4; |
| 1 | 417 | | return this; |
| | 418 | | } |
| | 419 | |
|
| | 420 | | public ByteBuffer putInt(int pos, int val) |
| | 421 | | { |
| 1 | 422 | | if (pos < 0) |
| | 423 | | { |
| 0 | 424 | | throwOutOfRange("pos", pos, "position must be non-negative"); |
| | 425 | | } |
| 1 | 426 | | if (pos + 4 > _limit) |
| | 427 | | { |
| 0 | 428 | | throwOutOfRange("pos", pos, "position must be less than limit - 4"); |
| | 429 | | } |
| 1 | 430 | | _valBytes.intVal = val; |
| 1 | 431 | | if (_order == NativeOrder._o) |
| | 432 | | { |
| 1 | 433 | | _bytes[pos] = _valBytes.b0; |
| 1 | 434 | | _bytes[pos + 1] = _valBytes.b1; |
| 1 | 435 | | _bytes[pos + 2] = _valBytes.b2; |
| 1 | 436 | | _bytes[pos + 3] = _valBytes.b3; |
| | 437 | | } |
| | 438 | | else |
| | 439 | | { |
| 0 | 440 | | _bytes[pos + 3] = _valBytes.b0; |
| 0 | 441 | | _bytes[pos + 2] = _valBytes.b1; |
| 0 | 442 | | _bytes[pos + 1] = _valBytes.b2; |
| 0 | 443 | | _bytes[pos] = _valBytes.b3; |
| | 444 | | } |
| 1 | 445 | | return this; |
| | 446 | | } |
| | 447 | |
|
| | 448 | | public ByteBuffer putIntSeq(int[] seq) |
| | 449 | | { |
| 1 | 450 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 451 | | checkOverflow(len); |
| 1 | 452 | | if (_order == NativeOrder._o) |
| | 453 | | { |
| 1 | 454 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| | 455 | | } |
| | 456 | | else |
| | 457 | | { |
| 0 | 458 | | for (int i = 0; i < seq.Length; ++i) |
| | 459 | | { |
| 0 | 460 | | int index = _position + (i * 4); |
| 0 | 461 | | _valBytes.intVal = seq[i]; |
| 0 | 462 | | _bytes[index + 3] = _valBytes.b0; |
| 0 | 463 | | _bytes[index + 2] = _valBytes.b1; |
| 0 | 464 | | _bytes[index + 1] = _valBytes.b2; |
| 0 | 465 | | _bytes[index] = _valBytes.b3; |
| | 466 | | } |
| | 467 | | } |
| 1 | 468 | | _position += len; |
| 1 | 469 | | return this; |
| | 470 | | } |
| | 471 | |
|
| | 472 | | public long getLong() |
| | 473 | | { |
| 1 | 474 | | long v = getLong(_position); |
| 1 | 475 | | _position += 8; |
| 1 | 476 | | return v; |
| | 477 | | } |
| | 478 | |
|
| | 479 | | public long getLong(int pos) |
| | 480 | | { |
| 1 | 481 | | checkUnderflow(pos, 8); |
| 1 | 482 | | if (_order == NativeOrder._o) |
| | 483 | | { |
| 1 | 484 | | _valBytes.b0 = _bytes[pos]; |
| 1 | 485 | | _valBytes.b1 = _bytes[pos + 1]; |
| 1 | 486 | | _valBytes.b2 = _bytes[pos + 2]; |
| 1 | 487 | | _valBytes.b3 = _bytes[pos + 3]; |
| 1 | 488 | | _valBytes.b4 = _bytes[pos + 4]; |
| 1 | 489 | | _valBytes.b5 = _bytes[pos + 5]; |
| 1 | 490 | | _valBytes.b6 = _bytes[pos + 6]; |
| 1 | 491 | | _valBytes.b7 = _bytes[pos + 7]; |
| | 492 | | } |
| | 493 | | else |
| | 494 | | { |
| 1 | 495 | | _valBytes.b7 = _bytes[pos]; |
| 1 | 496 | | _valBytes.b6 = _bytes[pos + 1]; |
| 1 | 497 | | _valBytes.b5 = _bytes[pos + 2]; |
| 1 | 498 | | _valBytes.b4 = _bytes[pos + 3]; |
| 1 | 499 | | _valBytes.b3 = _bytes[pos + 4]; |
| 1 | 500 | | _valBytes.b2 = _bytes[pos + 5]; |
| 1 | 501 | | _valBytes.b1 = _bytes[pos + 6]; |
| 1 | 502 | | _valBytes.b0 = _bytes[pos + 7]; |
| | 503 | | } |
| 1 | 504 | | return _valBytes.longVal; |
| | 505 | | } |
| | 506 | |
|
| | 507 | | public void getLongSeq(long[] seq) |
| | 508 | | { |
| 1 | 509 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 510 | | checkUnderflow(len); |
| 1 | 511 | | if (_order == NativeOrder._o) |
| | 512 | | { |
| 1 | 513 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| | 514 | | } |
| | 515 | | else |
| | 516 | | { |
| 0 | 517 | | for (int i = 0; i < seq.Length; ++i) |
| | 518 | | { |
| 0 | 519 | | int index = _position + (i * 8); |
| 0 | 520 | | _valBytes.b7 = _bytes[index]; |
| 0 | 521 | | _valBytes.b6 = _bytes[index + 1]; |
| 0 | 522 | | _valBytes.b5 = _bytes[index + 2]; |
| 0 | 523 | | _valBytes.b4 = _bytes[index + 3]; |
| 0 | 524 | | _valBytes.b3 = _bytes[index + 4]; |
| 0 | 525 | | _valBytes.b2 = _bytes[index + 5]; |
| 0 | 526 | | _valBytes.b1 = _bytes[index + 6]; |
| 0 | 527 | | _valBytes.b0 = _bytes[index + 7]; |
| 0 | 528 | | seq[i] = _valBytes.longVal; |
| | 529 | | } |
| | 530 | | } |
| 1 | 531 | | _position += len; |
| 1 | 532 | | } |
| | 533 | |
|
| | 534 | | public ByteBuffer putLong(long val) |
| | 535 | | { |
| 1 | 536 | | checkOverflow(8); |
| 1 | 537 | | _valBytes.longVal = val; |
| 1 | 538 | | if (_order == NativeOrder._o) |
| | 539 | | { |
| 1 | 540 | | _bytes[_position] = _valBytes.b0; |
| 1 | 541 | | _bytes[_position + 1] = _valBytes.b1; |
| 1 | 542 | | _bytes[_position + 2] = _valBytes.b2; |
| 1 | 543 | | _bytes[_position + 3] = _valBytes.b3; |
| 1 | 544 | | _bytes[_position + 4] = _valBytes.b4; |
| 1 | 545 | | _bytes[_position + 5] = _valBytes.b5; |
| 1 | 546 | | _bytes[_position + 6] = _valBytes.b6; |
| 1 | 547 | | _bytes[_position + 7] = _valBytes.b7; |
| | 548 | | } |
| | 549 | | else |
| | 550 | | { |
| 1 | 551 | | _bytes[_position + 7] = _valBytes.b0; |
| 1 | 552 | | _bytes[_position + 6] = _valBytes.b1; |
| 1 | 553 | | _bytes[_position + 5] = _valBytes.b2; |
| 1 | 554 | | _bytes[_position + 4] = _valBytes.b3; |
| 1 | 555 | | _bytes[_position + 3] = _valBytes.b4; |
| 1 | 556 | | _bytes[_position + 2] = _valBytes.b5; |
| 1 | 557 | | _bytes[_position + 1] = _valBytes.b6; |
| 1 | 558 | | _bytes[_position] = _valBytes.b7; |
| | 559 | | } |
| 1 | 560 | | _position += 8; |
| 1 | 561 | | return this; |
| | 562 | | } |
| | 563 | |
|
| | 564 | | public ByteBuffer putLongSeq(long[] seq) |
| | 565 | | { |
| 1 | 566 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 567 | | checkOverflow(len); |
| 1 | 568 | | if (_order == NativeOrder._o) |
| | 569 | | { |
| 1 | 570 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| | 571 | | } |
| | 572 | | else |
| | 573 | | { |
| 0 | 574 | | for (int i = 0; i < seq.Length; ++i) |
| | 575 | | { |
| 0 | 576 | | int index = _position + (i * 8); |
| 0 | 577 | | _valBytes.longVal = seq[i]; |
| 0 | 578 | | _bytes[index + 7] = _valBytes.b0; |
| 0 | 579 | | _bytes[index + 6] = _valBytes.b1; |
| 0 | 580 | | _bytes[index + 5] = _valBytes.b2; |
| 0 | 581 | | _bytes[index + 4] = _valBytes.b3; |
| 0 | 582 | | _bytes[index + 3] = _valBytes.b4; |
| 0 | 583 | | _bytes[index + 2] = _valBytes.b5; |
| 0 | 584 | | _bytes[index + 1] = _valBytes.b6; |
| 0 | 585 | | _bytes[index] = _valBytes.b7; |
| | 586 | | } |
| | 587 | | } |
| 1 | 588 | | _position += len; |
| 1 | 589 | | return this; |
| | 590 | | } |
| | 591 | |
|
| | 592 | | public float getFloat() |
| | 593 | | { |
| 1 | 594 | | checkUnderflow(4); |
| 1 | 595 | | if (_order == NativeOrder._o) |
| | 596 | | { |
| 1 | 597 | | _valBytes.b0 = _bytes[_position]; |
| 1 | 598 | | _valBytes.b1 = _bytes[_position + 1]; |
| 1 | 599 | | _valBytes.b2 = _bytes[_position + 2]; |
| 1 | 600 | | _valBytes.b3 = _bytes[_position + 3]; |
| | 601 | | } |
| | 602 | | else |
| | 603 | | { |
| 0 | 604 | | _valBytes.b3 = _bytes[_position]; |
| 0 | 605 | | _valBytes.b2 = _bytes[_position + 1]; |
| 0 | 606 | | _valBytes.b1 = _bytes[_position + 2]; |
| 0 | 607 | | _valBytes.b0 = _bytes[_position + 3]; |
| | 608 | | } |
| 1 | 609 | | _position += 4; |
| 1 | 610 | | return _valBytes.floatVal; |
| | 611 | | } |
| | 612 | |
|
| | 613 | | public void getFloatSeq(float[] seq) |
| | 614 | | { |
| 1 | 615 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 616 | | checkUnderflow(len); |
| 1 | 617 | | if (_order == NativeOrder._o) |
| | 618 | | { |
| 1 | 619 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| | 620 | | } |
| | 621 | | else |
| | 622 | | { |
| 0 | 623 | | for (int i = 0; i < seq.Length; ++i) |
| | 624 | | { |
| 0 | 625 | | int index = _position + (i * 4); |
| 0 | 626 | | _valBytes.b3 = _bytes[index]; |
| 0 | 627 | | _valBytes.b2 = _bytes[index + 1]; |
| 0 | 628 | | _valBytes.b1 = _bytes[index + 2]; |
| 0 | 629 | | _valBytes.b0 = _bytes[index + 3]; |
| 0 | 630 | | seq[i] = _valBytes.floatVal; |
| | 631 | | } |
| | 632 | | } |
| 1 | 633 | | _position += len; |
| 1 | 634 | | } |
| | 635 | |
|
| | 636 | | public ByteBuffer putFloat(float val) |
| | 637 | | { |
| 1 | 638 | | checkOverflow(4); |
| 1 | 639 | | _valBytes.floatVal = val; |
| 1 | 640 | | if (_order == NativeOrder._o) |
| | 641 | | { |
| 1 | 642 | | _bytes[_position] = _valBytes.b0; |
| 1 | 643 | | _bytes[_position + 1] = _valBytes.b1; |
| 1 | 644 | | _bytes[_position + 2] = _valBytes.b2; |
| 1 | 645 | | _bytes[_position + 3] = _valBytes.b3; |
| | 646 | | } |
| | 647 | | else |
| | 648 | | { |
| 0 | 649 | | _bytes[_position + 3] = _valBytes.b0; |
| 0 | 650 | | _bytes[_position + 2] = _valBytes.b1; |
| 0 | 651 | | _bytes[_position + 1] = _valBytes.b2; |
| 0 | 652 | | _bytes[_position] = _valBytes.b3; |
| | 653 | | } |
| 1 | 654 | | _position += 4; |
| 1 | 655 | | return this; |
| | 656 | | } |
| | 657 | |
|
| | 658 | | public ByteBuffer putFloatSeq(float[] seq) |
| | 659 | | { |
| 1 | 660 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 661 | | checkOverflow(len); |
| 1 | 662 | | if (_order == NativeOrder._o) |
| | 663 | | { |
| 1 | 664 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| | 665 | | } |
| | 666 | | else |
| | 667 | | { |
| 0 | 668 | | for (int i = 0; i < seq.Length; ++i) |
| | 669 | | { |
| 0 | 670 | | int index = _position + (i * 4); |
| 0 | 671 | | _valBytes.floatVal = seq[i]; |
| 0 | 672 | | _bytes[index + 3] = _valBytes.b0; |
| 0 | 673 | | _bytes[index + 2] = _valBytes.b1; |
| 0 | 674 | | _bytes[index + 1] = _valBytes.b2; |
| 0 | 675 | | _bytes[index] = _valBytes.b3; |
| | 676 | | } |
| | 677 | | } |
| 1 | 678 | | _position += len; |
| 1 | 679 | | return this; |
| | 680 | | } |
| | 681 | |
|
| | 682 | | public double getDouble() |
| | 683 | | { |
| 1 | 684 | | checkUnderflow(8); |
| 1 | 685 | | if (_order == NativeOrder._o) |
| | 686 | | { |
| 1 | 687 | | _valBytes.b0 = _bytes[_position]; |
| 1 | 688 | | _valBytes.b1 = _bytes[_position + 1]; |
| 1 | 689 | | _valBytes.b2 = _bytes[_position + 2]; |
| 1 | 690 | | _valBytes.b3 = _bytes[_position + 3]; |
| 1 | 691 | | _valBytes.b4 = _bytes[_position + 4]; |
| 1 | 692 | | _valBytes.b5 = _bytes[_position + 5]; |
| 1 | 693 | | _valBytes.b6 = _bytes[_position + 6]; |
| 1 | 694 | | _valBytes.b7 = _bytes[_position + 7]; |
| | 695 | | } |
| | 696 | | else |
| | 697 | | { |
| 0 | 698 | | _valBytes.b7 = _bytes[_position]; |
| 0 | 699 | | _valBytes.b6 = _bytes[_position + 1]; |
| 0 | 700 | | _valBytes.b5 = _bytes[_position + 2]; |
| 0 | 701 | | _valBytes.b4 = _bytes[_position + 3]; |
| 0 | 702 | | _valBytes.b3 = _bytes[_position + 4]; |
| 0 | 703 | | _valBytes.b2 = _bytes[_position + 5]; |
| 0 | 704 | | _valBytes.b1 = _bytes[_position + 6]; |
| 0 | 705 | | _valBytes.b0 = _bytes[_position + 7]; |
| | 706 | | } |
| 1 | 707 | | _position += 8; |
| 1 | 708 | | return _valBytes.doubleVal; |
| | 709 | | } |
| | 710 | |
|
| | 711 | | public void getDoubleSeq(double[] seq) |
| | 712 | | { |
| 1 | 713 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 714 | | checkUnderflow(len); |
| 1 | 715 | | if (_order == NativeOrder._o) |
| | 716 | | { |
| 1 | 717 | | System.Buffer.BlockCopy(_bytes, _position, seq, 0, len); |
| | 718 | | } |
| | 719 | | else |
| | 720 | | { |
| 0 | 721 | | for (int i = 0; i < seq.Length; ++i) |
| | 722 | | { |
| 0 | 723 | | int index = _position + (i * 8); |
| 0 | 724 | | _valBytes.b7 = _bytes[index]; |
| 0 | 725 | | _valBytes.b6 = _bytes[index + 1]; |
| 0 | 726 | | _valBytes.b5 = _bytes[index + 2]; |
| 0 | 727 | | _valBytes.b4 = _bytes[index + 3]; |
| 0 | 728 | | _valBytes.b3 = _bytes[index + 4]; |
| 0 | 729 | | _valBytes.b2 = _bytes[index + 5]; |
| 0 | 730 | | _valBytes.b1 = _bytes[index + 6]; |
| 0 | 731 | | _valBytes.b0 = _bytes[index + 7]; |
| 0 | 732 | | seq[i] = _valBytes.doubleVal; |
| | 733 | | } |
| | 734 | | } |
| 1 | 735 | | _position += len; |
| 1 | 736 | | } |
| | 737 | |
|
| | 738 | | public ByteBuffer putDouble(double val) |
| | 739 | | { |
| 1 | 740 | | checkOverflow(8); |
| 1 | 741 | | _valBytes.doubleVal = val; |
| 1 | 742 | | if (_order == NativeOrder._o) |
| | 743 | | { |
| 1 | 744 | | _bytes[_position] = _valBytes.b0; |
| 1 | 745 | | _bytes[_position + 1] = _valBytes.b1; |
| 1 | 746 | | _bytes[_position + 2] = _valBytes.b2; |
| 1 | 747 | | _bytes[_position + 3] = _valBytes.b3; |
| 1 | 748 | | _bytes[_position + 4] = _valBytes.b4; |
| 1 | 749 | | _bytes[_position + 5] = _valBytes.b5; |
| 1 | 750 | | _bytes[_position + 6] = _valBytes.b6; |
| 1 | 751 | | _bytes[_position + 7] = _valBytes.b7; |
| | 752 | | } |
| | 753 | | else |
| | 754 | | { |
| 0 | 755 | | _bytes[_position + 7] = _valBytes.b0; |
| 0 | 756 | | _bytes[_position + 6] = _valBytes.b1; |
| 0 | 757 | | _bytes[_position + 5] = _valBytes.b2; |
| 0 | 758 | | _bytes[_position + 4] = _valBytes.b3; |
| 0 | 759 | | _bytes[_position + 3] = _valBytes.b4; |
| 0 | 760 | | _bytes[_position + 2] = _valBytes.b5; |
| 0 | 761 | | _bytes[_position + 1] = _valBytes.b6; |
| 0 | 762 | | _bytes[_position] = _valBytes.b7; |
| | 763 | | } |
| 1 | 764 | | _position += 8; |
| 1 | 765 | | return this; |
| | 766 | | } |
| | 767 | |
|
| | 768 | | public ByteBuffer putDoubleSeq(double[] seq) |
| | 769 | | { |
| 1 | 770 | | int len = System.Buffer.ByteLength(seq); |
| 1 | 771 | | checkOverflow(len); |
| 1 | 772 | | if (_order == NativeOrder._o) |
| | 773 | | { |
| 1 | 774 | | System.Buffer.BlockCopy(seq, 0, _bytes, _position, len); |
| | 775 | | } |
| | 776 | | else |
| | 777 | | { |
| 0 | 778 | | for (int i = 0; i < seq.Length; ++i) |
| | 779 | | { |
| 0 | 780 | | int index = _position + (i * 8); |
| 0 | 781 | | _valBytes.doubleVal = seq[i]; |
| 0 | 782 | | _bytes[index + 7] = _valBytes.b0; |
| 0 | 783 | | _bytes[index + 6] = _valBytes.b1; |
| 0 | 784 | | _bytes[index + 5] = _valBytes.b2; |
| 0 | 785 | | _bytes[index + 4] = _valBytes.b3; |
| 0 | 786 | | _bytes[index + 3] = _valBytes.b4; |
| 0 | 787 | | _bytes[index + 2] = _valBytes.b5; |
| 0 | 788 | | _bytes[index + 1] = _valBytes.b6; |
| 0 | 789 | | _bytes[index] = _valBytes.b7; |
| | 790 | | } |
| | 791 | | } |
| 1 | 792 | | _position += len; |
| 1 | 793 | | return this; |
| | 794 | | } |
| | 795 | |
|
| 1 | 796 | | public byte[] rawBytes() => _bytes; |
| | 797 | |
|
| | 798 | | public byte[] rawBytes(int offset, int len) |
| | 799 | | { |
| 0 | 800 | | if (offset + len > _limit) |
| | 801 | | { |
| 0 | 802 | | throw new InvalidOperationException("buffer underflow"); |
| | 803 | | } |
| 0 | 804 | | byte[] rc = new byte[len]; |
| 0 | 805 | | Array.Copy(_bytes, offset, rc, 0, len); |
| 0 | 806 | | return rc; |
| | 807 | | } |
| | 808 | |
|
| | 809 | | private void checkUnderflow(int size) |
| | 810 | | { |
| 1 | 811 | | if (_position + size > _limit) |
| | 812 | | { |
| 1 | 813 | | throw new InvalidOperationException("buffer underflow"); |
| | 814 | | } |
| 1 | 815 | | } |
| | 816 | |
|
| | 817 | | private void checkUnderflow(int pos, int size) |
| | 818 | | { |
| 1 | 819 | | if (pos + size > _limit) |
| | 820 | | { |
| 0 | 821 | | throw new InvalidOperationException("buffer underflow"); |
| | 822 | | } |
| 1 | 823 | | } |
| | 824 | |
|
| | 825 | | private void checkOverflow(int size) |
| | 826 | | { |
| 1 | 827 | | if (_position + size > _limit) |
| | 828 | | { |
| 0 | 829 | | throw new InvalidOperationException("buffer overflow"); |
| | 830 | | } |
| 1 | 831 | | } |
| | 832 | |
|
| | 833 | | private int _position; |
| | 834 | | private int _limit; |
| | 835 | | private int _capacity; |
| | 836 | | private byte[] _bytes; |
| | 837 | | private ValBytes _valBytes; |
| | 838 | | private ByteOrder _order; |
| | 839 | |
|
| | 840 | | private class NativeOrder // Native Order |
| | 841 | | { |
| 1 | 842 | | static NativeOrder() => _o = BitConverter.IsLittleEndian ? ByteOrder.LittleEndian : ByteOrder.BigEndian; |
| | 843 | |
|
| | 844 | | internal static readonly ByteOrder _o; |
| | 845 | |
|
| 0 | 846 | | private NativeOrder() |
| | 847 | | { |
| 0 | 848 | | } |
| | 849 | | } |
| | 850 | |
|
| | 851 | | private static void throwOutOfRange(string param, object value, string message) => |
| 0 | 852 | | throw new ArgumentOutOfRangeException(param, value, message); |
| | 853 | | } |