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