| 1 7 8 10 package java.nio; 11 12 13 import java.io.IOException ; 14 15 16 239 240 public abstract class CharBuffer 241 extends Buffer  242 implements Comparable <CharBuffer >, Appendable , CharSequence , Readable  243 { 244 245 final char[] hb; final int offset; 251 boolean isReadOnly; 253 CharBuffer(int mark, int pos, int lim, int cap, char[] hb, int offset) 258 { 259 super(mark, pos, lim, cap); 260 this.hb = hb; 261 this.offset = offset; 262 } 263 264 CharBuffer(int mark, int pos, int lim, int cap) { this(mark, pos, lim, cap, null, 0); 268 } 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 309 public static CharBuffer allocate(int capacity) { 310 if (capacity < 0) 311 throw new IllegalArgumentException (); 312 return new HeapCharBuffer (capacity, capacity); 313 } 314 315 346 public static CharBuffer wrap(char[] array, 347 int offset, int length) 348 { 349 try { 350 return new HeapCharBuffer (array, offset, length); 351 } catch (IllegalArgumentException x) { 352 throw new IndexOutOfBoundsException (); 353 } 354 } 355 356 372 public static CharBuffer wrap(char[] array) { 373 return wrap(array, 0, array.length); 374 } 375 376 377 378 391 public int read(CharBuffer target) throws IOException { 392 int targetRemaining = target.remaining(); 394 int remaining = remaining(); 395 if (remaining == 0) 396 return -1; 397 int n = Math.min(remaining, targetRemaining); 398 int limit = limit(); 399 if (targetRemaining < remaining) 401 limit(position() + n); 402 try { 403 if (n > 0) 404 target.put(this); 405 } finally { 406 limit(limit); } 408 return n; 409 } 410 411 440 public static CharBuffer wrap(CharSequence csq, int start, int end) { 441 try { 442 return new StringCharBuffer (csq, start, end); 443 } catch (IllegalArgumentException x) { 444 throw new IndexOutOfBoundsException (); 445 } 446 } 447 448 462 public static CharBuffer wrap(CharSequence csq) { 463 return wrap(csq, 0, csq.length()); 464 } 465 466 467 468 485 public abstract CharBuffer slice(); 486 487 502 public abstract CharBuffer duplicate(); 503 504 522 public abstract CharBuffer asReadOnlyBuffer(); 523 524 525 527 536 public abstract char get(); 537 538 555 public abstract CharBuffer put(char c); 556 557 570 public abstract char get(int index); 571 572 593 public abstract CharBuffer put(int index, char c); 594 595 596 598 647 public CharBuffer get(char[] 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 CharBuffer get(char[] dst) { 674 return get(dst, 0, dst.length); 675 } 676 677 678 680 721 public CharBuffer put(CharBuffer 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 CharBuffer put(char[] 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 CharBuffer put(char[] src) { 812 return put(src, 0, src.length); 813 } 814 815 816 817 869 public CharBuffer put(String src, int start, int end) { 870 checkBounds(start, end - start, src.length()); 871 for (int i = start; i < end; i++) 872 this.put(src.charAt(i)); 873 return this; 874 } 875 876 894 public final CharBuffer put(String src) { 895 return put(src, 0, src.length()); 896 } 897 898 899 900 901 903 914 public final boolean hasArray() { 915 return (hb != null) && !isReadOnly; 916 } 917 918 937 public final char[] 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 CharBuffer compact(); 1015 1016 1021 public abstract boolean isDirect(); 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 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 CharBuffer )) 1096 return false; 1097 CharBuffer that = (CharBuffer )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 char v1 = this.get(i); 1103 char 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(CharBuffer 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 char v1 = this.get(i); 1129 char 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 1157 public String toString() { 1158 return toString(position(), limit()); 1159 } 1160 1161 abstract String toString(int start, int end); 1163 1164 |