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