1 4 package com.tc.bytes; 5 6 import com.tc.util.Assert; 7 8 13 abstract class AbstractTCByteBuffer implements TCByteBuffer { 14 15 public final TCByteBuffer get(int index, byte[] dst) { 16 return get(index, dst, 0, dst.length); 17 } 18 19 public final TCByteBuffer get(int index, byte[] dst, int offset, int length) { 20 final int origPosition = position(); 21 22 try { 23 position(index); 24 get(dst, offset, length); 25 } finally { 26 position(origPosition); 27 } 28 29 return this; 30 } 31 32 public final TCByteBuffer put(int index, byte[] src) { 33 return put(index, src, 0, src.length); 34 } 35 36 public final TCByteBuffer put(int index, byte[] src, int offset, int length) { 37 final int origPosition = position(); 38 39 try { 40 position(index); 41 put(src, offset, length); 42 } finally { 43 position(origPosition); 44 } 45 46 return this; 47 } 48 49 public final TCByteBuffer putUint(long i) { 50 if ((i > 0xFFFFFFFFL) || (i < 0L)) { 51 throw new IllegalArgumentException ("Unsigned integer value must be positive and <= (2^32)-1"); 53 } 54 55 put((byte) ((i >> 24) & 0x000000FF)); 56 put((byte) ((i >> 16) & 0x000000FF)); 57 put((byte) ((i >> 8) & 0x000000FF)); 58 put((byte) (i & 0x000000FF)); 59 60 return this; 61 } 62 63 public final TCByteBuffer putUint(int index, long i) { 64 final int origPosition = position(); 65 66 try { 67 position(index); 68 putUint(i); 69 } finally { 70 position(origPosition); 71 } 72 73 return this; 74 } 75 76 public final TCByteBuffer putUshort(int s) { 77 if ((s > 0x0000FFFF) || (s < 0)) { throw new IllegalArgumentException ( 78 "Unsigned integer value must be positive and <= (2^16)-1"); } 79 80 put((byte) ((s >> 8) & 0x000000FF)); 81 put((byte) (s & 0x000000FF)); 82 83 return this; 84 } 85 86 public final TCByteBuffer putUshort(int index, int s) { 87 final int origPosition = position(); 88 89 try { 90 position(index); 91 putUshort(s); 92 } finally { 93 position(origPosition); 94 } 95 96 return this; 97 } 98 99 public final long getUint() { 100 long rv = 0; 101 102 rv += ((long) (get() & 0xFF) << 24); 103 rv += ((get() & 0xFF) << 16); 104 rv += ((get() & 0xFF) << 8); 105 rv += ((get() & 0xFF)); 106 107 return rv; 108 } 109 110 public final long getUint(int index) { 111 final int origPosition = position(); 112 113 try { 114 position(index); 115 return getUint(); 116 } finally { 117 position(origPosition); 118 } 119 } 120 121 public final int getUshort() { 122 int rv = 0; 123 124 rv += ((get() & 0xFF) << 8); 125 rv += ((get() & 0xFF)); 126 127 Assert.eval((rv >= 0) && (rv <= 0xFFFF)); 128 129 return rv; 130 } 131 132 public final int getUshort(int index) { 133 final int origPosition = position(); 134 135 try { 136 position(index); 137 return getUshort(); 138 } finally { 139 position(origPosition); 140 } 141 } 142 143 public final short getUbyte() { 144 return (short) (get() & 0xFF); 145 } 146 147 public final short getUbyte(int index) { 148 final int origPosition = position(); 149 150 try { 151 position(index); 152 return getUbyte(); 153 } finally { 154 position(origPosition); 155 } 156 } 157 158 public final TCByteBuffer putUbyte(int index, short value) { 159 final int origPosition = position(); 160 161 try { 162 position(index); 163 putUbyte(value); 164 } finally { 165 position(origPosition); 166 } 167 168 return this; 169 } 170 171 public final TCByteBuffer putUbyte(short value) { 172 if ((value < 0) || (value > 0xFF)) { throw new IllegalArgumentException ( 173 "Unsigned byte value must in range 0-255 inclusive"); } 174 put((byte) (value & 0xFF)); 175 return this; 176 } 177 } | Popular Tags |