1 7 8 10 package java.nio; 11 12 13 14 15 16 239 240 public abstract class ByteBuffer 241 extends Buffer 242 implements Comparable <ByteBuffer > 243 { 244 245 final byte[] hb; final int offset; 251 boolean isReadOnly; 253 ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset) 258 { 259 super(mark, pos, lim, cap); 260 this.hb = hb; 261 this.offset = offset; 262 } 263 264 ByteBuffer(int mark, int pos, int lim, int cap) { this(mark, pos, lim, cap, null, 0); 268 } 269 270 271 272 287 public static ByteBuffer allocateDirect(int capacity) { 288 return new DirectByteBuffer (capacity); 289 } 290 291 292 293 309 public static ByteBuffer allocate(int capacity) { 310 if (capacity < 0) 311 throw new IllegalArgumentException (); 312 return new HeapByteBuffer (capacity, capacity); 313 } 314 315 346 public static ByteBuffer wrap(byte[] array, 347 int offset, int length) 348 { 349 try { 350 return new HeapByteBuffer (array, offset, length); 351 } catch (IllegalArgumentException x) { 352 throw new IndexOutOfBoundsException (); 353 } 354 } 355 356 372 public static ByteBuffer wrap(byte[] array) { 373 return wrap(array, 0, array.length); 374 } 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 485 public abstract ByteBuffer slice(); 486 487 502 public abstract ByteBuffer duplicate(); 503 504 522 public abstract ByteBuffer asReadOnlyBuffer(); 523 524 525 527 536 public abstract byte get(); 537 538 555 public abstract ByteBuffer put(byte b); 556 557 570 public abstract byte get(int index); 571 572 593 public abstract ByteBuffer put(int index, byte b); 594 595 596 598 647 public ByteBuffer get(byte[] dst, int offset, int length) { 648 checkBounds(offset, length, dst.length); 649 if (length > remaining()) 650 throw new BufferUnderflowException (); 651 int end = offset + length; 652 for (int i = offset; i < end; i++) 653 dst[i] = get(); 654 return this; 655 } 656 657 673 public ByteBuffer get(byte[] dst) { 674 return get(dst, 0, dst.length); 675 } 676 677 678 680 721 public ByteBuffer put(ByteBuffer src) { 722 if (src == this) 723 throw new IllegalArgumentException (); 724 int n = src.remaining(); 725 if (n > remaining()) 726 throw new BufferOverflowException (); 727 for (int i = 0; i < n; i++) 728 put(src.get()); 729 return this; 730 } 731 732 782 public ByteBuffer put(byte[] src, int offset, int length) { 783 checkBounds(offset, length, src.length); 784 if (length > remaining()) 785 throw new BufferOverflowException (); 786 int end = offset + length; 787 for (int i = offset; i < end; i++) 788 this.put(src[i]); 789 return this; 790 } 791 792 811 public final ByteBuffer put(byte[] src) { 812 return put(src, 0, src.length); 813 } 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 903 914 public final boolean hasArray() { 915 return (hb != null) && !isReadOnly; 916 } 917 918 937 public final byte[] array() { 938 if (hb == null) 939 throw new UnsupportedOperationException (); 940 if (isReadOnly) 941 throw new ReadOnlyBufferException (); 942 return hb; 943 } 944 945 965 public final int arrayOffset() { 966 if (hb == null) 967 throw new UnsupportedOperationException (); 968 if (isReadOnly) 969 throw new ReadOnlyBufferException (); 970 return offset; 971 } 972 973 1014 public abstract ByteBuffer compact(); 1015 1016 1021 public abstract boolean isDirect(); 1022 1023 1024 1025 1030 public String toString() { 1031 StringBuffer sb = new StringBuffer (); 1032 sb.append(getClass().getName()); 1033 sb.append("[pos="); 1034 sb.append(position()); 1035 sb.append(" lim="); 1036 sb.append(limit()); 1037 sb.append(" cap="); 1038 sb.append(capacity()); 1039 sb.append("]"); 1040 return sb.toString(); 1041 } 1042 1043 1044 1045 1046 1047 1048 1061 public int hashCode() { 1062 int h = 1; 1063 int p = position(); 1064 for (int i = limit() - 1; i >= p; i--) 1065 h = 31 * h + (int)get(i); 1066 return h; 1067 } 1068 1069 1094 public boolean equals(Object ob) { 1095 if (!(ob instanceof ByteBuffer )) 1096 return false; 1097 ByteBuffer that = (ByteBuffer )ob; 1098 if (this.remaining() != that.remaining()) 1099 return false; 1100 int p = this.position(); 1101 for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) { 1102 byte v1 = this.get(i); 1103 byte v2 = that.get(j); 1104 if (v1 != v2) { 1105 if ((v1 != v1) && (v2 != v2)) continue; 1107 return false; 1108 } 1109 } 1110 return true; 1111 } 1112 1113 1125 public int compareTo(ByteBuffer that) { 1126 int n = this.position() + Math.min(this.remaining(), that.remaining()); 1127 for (int i = this.position(), j = that.position(); i < n; i++, j++) { 1128 byte v1 = this.get(i); 1129 byte v2 = that.get(j); 1130 if (v1 == v2) 1131 continue; 1132 if ((v1 != v1) && (v2 != v2)) continue; 1134 if (v1 < v2) 1135 return -1; 1136 return +1; 1137 } 1138 return this.remaining() - that.remaining(); 1139 } 1140 1141 1142 1143 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 boolean bigEndian = true; 1362 boolean nativeByteOrder = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN); 1364 1365 1375 public final ByteOrder order() { 1376 return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; 1377 } 1378 1379 1389 public final ByteBuffer order(ByteOrder bo) { 1390 bigEndian = (bo == ByteOrder.BIG_ENDIAN); 1391 nativeByteOrder = 1392 (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN)); 1393 return this; 1394 } 1395 1396 abstract byte _get(int i); abstract void _put(int i, byte b); 1401 1402 1415 public abstract char getChar(); 1416 1417 1437 public abstract ByteBuffer putChar(char value); 1438 1439 1455 public abstract char getChar(int index); 1456 1457 1480 public abstract ByteBuffer putChar(int index, char value); 1481 1482 1498 public abstract CharBuffer asCharBuffer(); 1499 1500 1501 1514 public abstract short getShort(); 1515 1516 1536 public abstract ByteBuffer putShort(short value); 1537 1538 1554 public abstract short getShort(int index); 1555 1556 1579 public abstract ByteBuffer putShort(int index, short value); 1580 1581 1597 public abstract ShortBuffer asShortBuffer(); 1598 1599 1600 1613 public abstract int getInt(); 1614 1615 1635 public abstract ByteBuffer putInt(int value); 1636 1637 1653 public abstract int getInt(int index); 1654 1655 1678 public abstract ByteBuffer putInt(int index, int value); 1679 1680 1696 public abstract IntBuffer asIntBuffer(); 1697 1698 1699 1712 public abstract long getLong(); 1713 1714 1734 public abstract ByteBuffer putLong(long value); 1735 1736 1752 public abstract long getLong(int index); 1753 1754 1777 public abstract ByteBuffer putLong(int index, long value); 1778 1779 1795 public abstract LongBuffer asLongBuffer(); 1796 1797 1798 1811 public abstract float getFloat(); 1812 1813 1833 public abstract ByteBuffer putFloat(float value); 1834 1835 1851 public abstract float getFloat(int index); 1852 1853 1876 public abstract ByteBuffer putFloat(int index, float value); 1877 1878 1894 public abstract FloatBuffer asFloatBuffer(); 1895 1896 1897 1910 public abstract double getDouble(); 1911 1912 1932 public abstract ByteBuffer putDouble(double value); 1933 1934 1950 public abstract double getDouble(int index); 1951 1952 1975 public abstract ByteBuffer putDouble(int index, double value); 1976 1977 1993 public abstract DoubleBuffer asDoubleBuffer(); 1994 1995} 1996 | Popular Tags |