1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.nio; 19 20 import java.io.IOException; 21 22 /** A buffer of chars. 23 * <p> 24 * A char buffer can be created in either one of the following ways: 25 * </p> 26 * <ul> 27 * <li>{@link #allocate(int) Allocate} a new char array and create a buffer based on it;</li> 28 * <li>{@link #wrap(char[]) Wrap} an existing char array to create a new buffer;</li> 29 * <li>{@link #wrap(CharSequence) Wrap} an existing char sequence to create a new buffer;</li> 30 * <li>Use {@link java.nio.ByteBuffer#asCharBuffer() ByteBuffer.asCharBuffer} to create a char buffer based on a byte buffer.</li> 31 * </ul> 32 * 33 * @since Android 1.0 */ 34 public abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>, CharSequence, Appendable {// , Readable { 35 36 /** Creates a char buffer based on a newly allocated char array. 37 * 38 * @param capacity the capacity of the new buffer. 39 * @return the created char buffer. 40 * @throws IllegalArgumentException if {@code capacity} is less than zero. 41 * @since Android 1.0 */ allocate(int capacity)42 public static CharBuffer allocate (int capacity) { 43 if (capacity < 0) { 44 throw new IllegalArgumentException(); 45 } 46 return BufferFactory.newCharBuffer(capacity); 47 } 48 49 /** Creates a new char buffer by wrapping the given char array. 50 * <p> 51 * Calling this method has the same effect as {@code wrap(array, 0, array.length)}. 52 * </p> 53 * 54 * @param array the char array which the new buffer will be based on. 55 * @return the created char buffer. 56 * @since Android 1.0 */ wrap(char[] array)57 public static CharBuffer wrap (char[] array) { 58 return wrap(array, 0, array.length); 59 } 60 61 /** Creates a new char buffer by wrapping the given char array. 62 * <p> 63 * The new buffer's position will be {@code start}, limit will be {@code start + len}, capacity will be the length of the array. 64 * </p> 65 * 66 * @param array the char array which the new buffer will be based on. 67 * @param start the start index, must not be negative and not greater than {@code array.length}. 68 * @param len the length, must not be negative and not greater than {@code array.length - start}. 69 * @return the created char buffer. 70 * @exception IndexOutOfBoundsException if either {@code start} or {@code len} is invalid. 71 * @since Android 1.0 */ wrap(char[] array, int start, int len)72 public static CharBuffer wrap (char[] array, int start, int len) { 73 int length = array.length; 74 if ((start < 0) || (len < 0) || (long)start + (long)len > length) { 75 throw new IndexOutOfBoundsException(); 76 } 77 78 CharBuffer buf = BufferFactory.newCharBuffer(array); 79 buf.position = start; 80 buf.limit = start + len; 81 82 return buf; 83 } 84 85 /** Creates a new char buffer by wrapping the given char sequence. 86 * <p> 87 * Calling this method has the same effect as {@code wrap(chseq, 0, chseq.length())}. 88 * </p> 89 * 90 * @param chseq the char sequence which the new buffer will be based on. 91 * @return the created char buffer. 92 * @since Android 1.0 */ wrap(CharSequence chseq)93 public static CharBuffer wrap (CharSequence chseq) { 94 return BufferFactory.newCharBuffer(chseq); 95 } 96 97 /** Creates a new char buffer by wrapping the given char sequence. 98 * <p> 99 * The new buffer's position will be {@code start}, limit will be {@code end}, capacity will be the length of the char sequence. 100 * The new buffer is read-only. 101 * </p> 102 * 103 * @param chseq the char sequence which the new buffer will be based on. 104 * @param start the start index, must not be negative and not greater than {@code chseq.length()}. 105 * @param end the end index, must be no less than {@code start} and no greater than {@code chseq.length()}. 106 * @return the created char buffer. 107 * @exception IndexOutOfBoundsException if either {@code start} or {@code end} is invalid. 108 * @since Android 1.0 */ wrap(CharSequence chseq, int start, int end)109 public static CharBuffer wrap (CharSequence chseq, int start, int end) { 110 if (chseq == null) { 111 throw new NullPointerException(); 112 } 113 if (start < 0 || end < start || end > chseq.length()) { 114 throw new IndexOutOfBoundsException(); 115 } 116 117 CharBuffer result = BufferFactory.newCharBuffer(chseq); 118 result.position = start; 119 result.limit = end; 120 return result; 121 } 122 123 /** Constructs a {@code CharBuffer} with given capacity. 124 * 125 * @param capacity the capacity of the buffer. 126 * @since Android 1.0 */ CharBuffer(int capacity)127 CharBuffer (int capacity) { 128 super(capacity); 129 } 130 131 /** Returns the char array which this buffer is based on, if there is one. 132 * 133 * @return the char array which this buffer is based on. 134 * @exception ReadOnlyBufferException if this buffer is based on an array, but it is read-only. 135 * @exception UnsupportedOperationException if this buffer is not based on an array. 136 * @since Android 1.0 */ array()137 public final char[] array () { 138 return protectedArray(); 139 } 140 141 /** Returns the offset of the char array which this buffer is based on, if there is one. 142 * <p> 143 * The offset is the index of the array corresponds to the zero position of the buffer. 144 * </p> 145 * 146 * @return the offset of the char array which this buffer is based on. 147 * @exception ReadOnlyBufferException if this buffer is based on an array but it is read-only. 148 * @exception UnsupportedOperationException if this buffer is not based on an array. 149 * @since Android 1.0 */ arrayOffset()150 public final int arrayOffset () { 151 return protectedArrayOffset(); 152 } 153 154 /** Returns a read-only buffer that shares its content with this buffer. 155 * <p> 156 * The returned buffer is guaranteed to be a new instance, even if this buffer is read-only itself. The new buffer's position, 157 * limit, capacity and mark are the same as this buffer's. 158 * </p> 159 * <p> 160 * The new buffer shares its content with this buffer, which means this buffer's change of content will be visible to the new 161 * buffer. The two buffer's position, limit and mark are independent. 162 * </p> 163 * 164 * @return a read-only version of this buffer. 165 * @since Android 1.0 */ asReadOnlyBuffer()166 public abstract CharBuffer asReadOnlyBuffer (); 167 168 /** Returns the character located at the specified index in the buffer. The index value is referenced from the current buffer 169 * position. 170 * 171 * @param index the index referenced from the current buffer position. It must not be less than zero but less than the value 172 * obtained from a call to {@code remaining()}. 173 * @return the character located at the specified index (referenced from the current position) in the buffer. 174 * @exception IndexOutOfBoundsException if the index is invalid. 175 * @since Android 1.0 */ charAt(int index)176 public final char charAt (int index) { 177 if (index < 0 || index >= remaining()) { 178 throw new IndexOutOfBoundsException(); 179 } 180 return get(position + index); 181 } 182 183 /** Compacts this char buffer. 184 * <p> 185 * The remaining chars will be moved to the head of the buffer, starting from position zero. Then the position is set to 186 * {@code remaining()}; the limit is set to capacity; the mark is cleared. 187 * </p> 188 * 189 * @return this buffer. 190 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 191 * @since Android 1.0 */ compact()192 public abstract CharBuffer compact (); 193 194 /** Compare the remaining chars of this buffer to another char buffer's remaining chars. 195 * 196 * @param otherBuffer another char buffer. 197 * @return a negative value if this is less than {@code otherBuffer}; 0 if this equals to {@code otherBuffer}; a positive value 198 * if this is greater than {@code otherBuffer}. 199 * @exception ClassCastException if {@code otherBuffer} is not a char buffer. 200 * @since Android 1.0 */ compareTo(CharBuffer otherBuffer)201 public int compareTo (CharBuffer otherBuffer) { 202 int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining(); 203 int thisPos = position; 204 int otherPos = otherBuffer.position; 205 char thisByte, otherByte; 206 while (compareRemaining > 0) { 207 thisByte = get(thisPos); 208 otherByte = otherBuffer.get(otherPos); 209 if (thisByte != otherByte) { 210 return thisByte < otherByte ? -1 : 1; 211 } 212 thisPos++; 213 otherPos++; 214 compareRemaining--; 215 } 216 return remaining() - otherBuffer.remaining(); 217 } 218 219 /** Returns a duplicated buffer that shares its content with this buffer. 220 * <p> 221 * The duplicated buffer's initial position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's 222 * read-only property and byte order are the same as this buffer's, too. 223 * </p> 224 * <p> 225 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the 226 * other. The two buffer's position, limit and mark are independent. 227 * </p> 228 * 229 * @return a duplicated buffer that shares its content with this buffer. 230 * @since Android 1.0 */ duplicate()231 public abstract CharBuffer duplicate (); 232 233 /** Checks whether this char buffer is equal to another object. 234 * <p> 235 * If {@code other} is not a char buffer then {@code false} is returned. Two char buffers are equal if and only if their 236 * remaining chars are exactly the same. Position, limit, capacity and mark are not considered. 237 * </p> 238 * 239 * @param other the object to compare with this char buffer. 240 * @return {@code true} if this char buffer is equal to {@code other}, {@code false} otherwise. 241 * @since Android 1.0 */ equals(Object other)242 public boolean equals (Object other) { 243 if (!(other instanceof CharBuffer)) { 244 return false; 245 } 246 CharBuffer otherBuffer = (CharBuffer)other; 247 248 if (remaining() != otherBuffer.remaining()) { 249 return false; 250 } 251 252 int myPosition = position; 253 int otherPosition = otherBuffer.position; 254 boolean equalSoFar = true; 255 while (equalSoFar && (myPosition < limit)) { 256 equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++); 257 } 258 259 return equalSoFar; 260 } 261 262 /** Returns the char at the current position and increases the position by 1. 263 * 264 * @return the char at the current position. 265 * @exception BufferUnderflowException if the position is equal or greater than limit. 266 * @since Android 1.0 */ get()267 public abstract char get (); 268 269 /** Reads chars from the current position into the specified char array and increases the position by the number of chars read. 270 * <p> 271 * Calling this method has the same effect as {@code get(dest, 0, dest.length)}. 272 * </p> 273 * 274 * @param dest the destination char array. 275 * @return this buffer. 276 * @exception BufferUnderflowException if {@code dest.length} is greater than {@code remaining()}. 277 * @since Android 1.0 */ get(char[] dest)278 public CharBuffer get (char[] dest) { 279 return get(dest, 0, dest.length); 280 } 281 282 /** Reads chars from the current position into the specified char array, starting from the specified offset, and increases the 283 * position by the number of chars read. 284 * 285 * @param dest the target char array. 286 * @param off the offset of the char array, must not be negative and not greater than {@code dest.length}. 287 * @param len The number of chars to read, must be no less than zero and no greater than {@code dest.length - off}. 288 * @return this buffer. 289 * @exception IndexOutOfBoundsException if either {@code off} or {@code len} is invalid. 290 * @exception BufferUnderflowException if {@code len} is greater than {@code remaining()}. 291 * @since Android 1.0 */ get(char[] dest, int off, int len)292 public CharBuffer get (char[] dest, int off, int len) { 293 int length = dest.length; 294 if ((off < 0) || (len < 0) || (long)off + (long)len > length) { 295 throw new IndexOutOfBoundsException(); 296 } 297 298 if (len > remaining()) { 299 throw new BufferUnderflowException(); 300 } 301 for (int i = off; i < off + len; i++) { 302 dest[i] = get(); 303 } 304 return this; 305 } 306 307 /** Returns a char at the specified index; the position is not changed. 308 * 309 * @param index the index, must not be negative and less than limit. 310 * @return a char at the specified index. 311 * @exception IndexOutOfBoundsException if index is invalid. 312 * @since Android 1.0 */ get(int index)313 public abstract char get (int index); 314 315 /** Indicates whether this buffer is based on a char array and is read/write. 316 * 317 * @return {@code true} if this buffer is based on a byte array and provides read/write access, {@code false} otherwise. 318 * @since Android 1.0 */ hasArray()319 public final boolean hasArray () { 320 return protectedHasArray(); 321 } 322 323 /** Calculates this buffer's hash code from the remaining chars. The position, limit, capacity and mark don't affect the hash 324 * code. 325 * 326 * @return the hash code calculated from the remaining chars. 327 * @since Android 1.0 */ hashCode()328 public int hashCode () { 329 int myPosition = position; 330 int hash = 0; 331 while (myPosition < limit) { 332 hash = hash + get(myPosition++); 333 } 334 return hash; 335 } 336 337 /** Indicates whether this buffer is direct. A direct buffer will try its best to take advantage of native memory APIs and it 338 * may not stay in the Java heap, so it is not affected by garbage collection. 339 * <p> 340 * A char buffer is direct if it is based on a byte buffer and the byte buffer is direct. 341 * </p> 342 * 343 * @return {@code true} if this buffer is direct, {@code false} otherwise. 344 * @since Android 1.0 */ isDirect()345 public abstract boolean isDirect (); 346 347 /** Returns the number of remaining chars. 348 * 349 * @return the number of remaining chars. 350 * @since Android 1.0 */ length()351 public final int length () { 352 return remaining(); 353 } 354 355 /** Returns the byte order used by this buffer when converting chars from/to bytes. 356 * <p> 357 * If this buffer is not based on a byte buffer, then this always returns the platform's native byte order. 358 * </p> 359 * 360 * @return the byte order used by this buffer when converting chars from/to bytes. 361 * @since Android 1.0 */ order()362 public abstract ByteOrder order (); 363 364 /** Child class implements this method to realize {@code array()}. 365 * 366 * @see #array() */ protectedArray()367 abstract char[] protectedArray (); 368 369 /** Child class implements this method to realize {@code arrayOffset()}. 370 * 371 * @see #arrayOffset() */ protectedArrayOffset()372 abstract int protectedArrayOffset (); 373 374 /** Child class implements this method to realize {@code hasArray()}. 375 * 376 * @see #hasArray() */ protectedHasArray()377 abstract boolean protectedHasArray (); 378 379 /** Writes the given char to the current position and increases the position by 1. 380 * 381 * @param c the char to write. 382 * @return this buffer. 383 * @exception BufferOverflowException if position is equal or greater than limit. 384 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 385 * @since Android 1.0 */ put(char c)386 public abstract CharBuffer put (char c); 387 388 /** Writes chars from the given char array to the current position and increases the position by the number of chars written. 389 * <p> 390 * Calling this method has the same effect as {@code put(src, 0, src.length)}. 391 * </p> 392 * 393 * @param src the source char array. 394 * @return this buffer. 395 * @exception BufferOverflowException if {@code remaining()} is less than {@code src.length}. 396 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 397 * @since Android 1.0 */ put(char[] src)398 public final CharBuffer put (char[] src) { 399 return put(src, 0, src.length); 400 } 401 402 /** Writes chars from the given char array, starting from the specified offset, to the current position and increases the 403 * position by the number of chars written. 404 * 405 * @param src the source char array. 406 * @param off the offset of char array, must not be negative and not greater than {@code src.length}. 407 * @param len the number of chars to write, must be no less than zero and no greater than {@code src.length - off}. 408 * @return this buffer. 409 * @exception BufferOverflowException if {@code remaining()} is less than {@code len}. 410 * @exception IndexOutOfBoundsException if either {@code off} or {@code len} is invalid. 411 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 412 * @since Android 1.0 */ put(char[] src, int off, int len)413 public CharBuffer put (char[] src, int off, int len) { 414 int length = src.length; 415 if ((off < 0) || (len < 0) || (long)off + (long)len > length) { 416 throw new IndexOutOfBoundsException(); 417 } 418 419 if (len > remaining()) { 420 throw new BufferOverflowException(); 421 } 422 for (int i = off; i < off + len; i++) { 423 put(src[i]); 424 } 425 return this; 426 } 427 428 /** Writes all the remaining chars of the {@code src} char buffer to this buffer's current position, and increases both buffers' 429 * position by the number of chars copied. 430 * 431 * @param src the source char buffer. 432 * @return this buffer. 433 * @exception BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code remaining()}. 434 * @exception IllegalArgumentException if {@code src} is this buffer. 435 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 436 * @since Android 1.0 */ put(CharBuffer src)437 public CharBuffer put (CharBuffer src) { 438 if (src == this) { 439 throw new IllegalArgumentException(); 440 } 441 if (src.remaining() > remaining()) { 442 throw new BufferOverflowException(); 443 } 444 445 char[] contents = new char[src.remaining()]; 446 src.get(contents); 447 put(contents); 448 return this; 449 } 450 451 /** Writes a char to the specified index of this buffer; the position is not changed. 452 * 453 * @param index the index, must be no less than zero and less than the limit. 454 * @param c the char to write. 455 * @return this buffer. 456 * @exception IndexOutOfBoundsException if index is invalid. 457 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 458 * @since Android 1.0 */ put(int index, char c)459 public abstract CharBuffer put (int index, char c); 460 461 /** Writes all chars of the given string to the current position of this buffer, and increases the position by the length of 462 * string. 463 * <p> 464 * Calling this method has the same effect as {@code put(str, 0, str.length())}. 465 * </p> 466 * 467 * @param str the string to write. 468 * @return this buffer. 469 * @exception BufferOverflowException if {@code remaining()} is less than the length of string. 470 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 471 * @since Android 1.0 */ put(String str)472 public final CharBuffer put (String str) { 473 return put(str, 0, str.length()); 474 } 475 476 /** Writes chars of the given string to the current position of this buffer, and increases the position by the number of chars 477 * written. 478 * 479 * @param str the string to write. 480 * @param start the first char to write, must not be negative and not greater than {@code str.length()}. 481 * @param end the last char to write (excluding), must be less than {@code start} and not greater than {@code str.length()}. 482 * @return this buffer. 483 * @exception BufferOverflowException if {@code remaining()} is less than {@code end - start}. 484 * @exception IndexOutOfBoundsException if either {@code start} or {@code end} is invalid. 485 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 486 * @since Android 1.0 */ put(String str, int start, int end)487 public CharBuffer put (String str, int start, int end) { 488 int length = str.length(); 489 if (start < 0 || end < start || end > length) { 490 throw new IndexOutOfBoundsException(); 491 } 492 493 if (end - start > remaining()) { 494 throw new BufferOverflowException(); 495 } 496 for (int i = start; i < end; i++) { 497 put(str.charAt(i)); 498 } 499 return this; 500 } 501 502 /** Returns a sliced buffer that shares its content with this buffer. 503 * <p> 504 * The sliced buffer's capacity will be this buffer's {@code remaining()}, and its zero position will correspond to this 505 * buffer's current position. The new buffer's position will be 0, limit will be its capacity, and its mark is cleared. The new 506 * buffer's read-only property and byte order are same as this buffer. 507 * </p> 508 * <p> 509 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the 510 * other. The two buffer's position, limit and mark are independent. 511 * </p> 512 * 513 * @return a sliced buffer that shares its content with this buffer. 514 * @since Android 1.0 */ slice()515 public abstract CharBuffer slice (); 516 517 /** Returns a new char buffer representing a sub-sequence of this buffer's current remaining content. 518 * <p> 519 * The new buffer's position will be {@code position() + start}, limit will be {@code position() + end}, capacity will be the 520 * same as this buffer. The new buffer's read-only property and byte order are the same as this buffer. 521 * </p> 522 * <p> 523 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the 524 * other. The two buffer's position, limit and mark are independent. 525 * </p> 526 * 527 * @param start the start index of the sub-sequence, referenced from the current buffer position. Must not be less than zero 528 * and not greater than the value obtained from a call to {@code remaining()}. 529 * @param end the end index of the sub-sequence, referenced from the current buffer position. Must not be less than 530 * {@code start} and not be greater than the value obtained from a call to {@code remaining()}. 531 * @return a new char buffer represents a sub-sequence of this buffer's current remaining content. 532 * @exception IndexOutOfBoundsException if either {@code start} or {@code end} is invalid. 533 * @since Android 1.0 */ subSequence(int start, int end)534 public abstract CharSequence subSequence (int start, int end); 535 536 /** Returns a string representing the current remaining chars of this buffer. 537 * 538 * @return a string representing the current remaining chars of this buffer. 539 * @since Android 1.0 */ toString()540 public String toString () { 541 StringBuffer strbuf = new StringBuffer(); 542 for (int i = position; i < limit; i++) { 543 strbuf.append(get(i)); 544 } 545 return strbuf.toString(); 546 } 547 548 /** Writes the given char to the current position and increases the position by 1. 549 * 550 * @param c the char to write. 551 * @return this buffer. 552 * @exception BufferOverflowException if position is equal or greater than limit. 553 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 554 * @since Android 1.0 */ append(char c)555 public CharBuffer append (char c) { 556 return put(c); 557 } 558 559 /** Writes all chars of the given character sequence {@code csq} to the current position of this buffer, and increases the 560 * position by the length of the csq. 561 * <p> 562 * Calling this method has the same effect as {@code append(csq.toString())}. 563 * </p> 564 * If the {@code CharSequence} is {@code null} the string "null" will be written to the buffer. 565 * 566 * @param csq the {@code CharSequence} to write. 567 * @return this buffer. 568 * @exception BufferOverflowException if {@code remaining()} is less than the length of csq. 569 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 570 * @since Android 1.0 */ append(CharSequence csq)571 public CharBuffer append (CharSequence csq) { 572 if (csq != null) { 573 return put(csq.toString()); 574 } 575 return put("null"); //$NON-NLS-1$ 576 } 577 578 /** Writes chars of the given {@code CharSequence} to the current position of this buffer, and increases the position by the 579 * number of chars written. 580 * 581 * @param csq the {@code CharSequence} to write. 582 * @param start the first char to write, must not be negative and not greater than {@code csq.length()}. 583 * @param end the last char to write (excluding), must be less than {@code start} and not greater than {@code csq.length()}. 584 * @return this buffer. 585 * @exception BufferOverflowException if {@code remaining()} is less than {@code end - start}. 586 * @exception IndexOutOfBoundsException if either {@code start} or {@code end} is invalid. 587 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer. 588 * @since Android 1.0 */ append(CharSequence csq, int start, int end)589 public CharBuffer append (CharSequence csq, int start, int end) { 590 if (csq == null) { 591 csq = "null"; //$NON-NLS-1$ 592 } 593 CharSequence cs = csq.subSequence(start, end); 594 if (cs.length() > 0) { 595 return put(cs.toString()); 596 } 597 return this; 598 } 599 600 /** Reads characters from this buffer and puts them into {@code target}. The number of chars that are copied is either the 601 * number of remaining chars in this buffer or the number of remaining chars in {@code target}, whichever is smaller. 602 * 603 * @param target the target char buffer. 604 * @throws IllegalArgumentException if {@code target} is this buffer. 605 * @throws IOException if an I/O error occurs. 606 * @throws ReadOnlyBufferException if no changes may be made to the contents of {@code target}. 607 * @return the number of chars copied or -1 if there are no chars left to be read from this buffer. 608 * @since Android 1.0 */ read(CharBuffer target)609 public int read (CharBuffer target) throws IOException { 610 if (target == this) { 611 throw new IllegalArgumentException(); 612 } 613 if (remaining() == 0) { 614 return target.remaining() == 0 ? 0 : -1; 615 } 616 int result = Math.min(target.remaining(), remaining()); 617 char[] chars = new char[result]; 618 get(chars); 619 target.put(chars); 620 return result; 621 } 622 } 623