1. /*
  2. * @(#)StringCharBuffer.java 1.17 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio;
  8. // ## If the sequence is a string, use reflection to share its array
  9. class StringCharBuffer // package-private
  10. extends CharBuffer
  11. {
  12. CharSequence str;
  13. StringCharBuffer(CharSequence s, int start, int end) { // package-private
  14. super(-1, start, end, s.length());
  15. int n = s.length();
  16. if ((start < 0) || (start > n) || (end < start) || (end > n))
  17. throw new IndexOutOfBoundsException();
  18. str = s;
  19. }
  20. public CharBuffer slice() {
  21. return new StringCharBuffer(str, position(), limit());
  22. }
  23. private StringCharBuffer(CharSequence s, int mark,
  24. int pos, int limit, int cap)
  25. {
  26. super(mark, pos, limit, cap);
  27. str = s;
  28. }
  29. public CharBuffer duplicate() {
  30. return new StringCharBuffer(str, markValue(),
  31. position(), limit(), capacity());
  32. }
  33. public CharBuffer asReadOnlyBuffer() {
  34. return duplicate();
  35. }
  36. public final char get() {
  37. return str.charAt(nextGetIndex());
  38. }
  39. public final char get(int index) {
  40. return str.charAt(checkIndex(index));
  41. }
  42. // ## Override bulk get methods for better performance
  43. public final CharBuffer put(char c) {
  44. throw new ReadOnlyBufferException();
  45. }
  46. public final CharBuffer put(int index, char c) {
  47. throw new ReadOnlyBufferException();
  48. }
  49. public final CharBuffer compact() {
  50. throw new ReadOnlyBufferException();
  51. }
  52. public final boolean isReadOnly() {
  53. return true;
  54. }
  55. final String toString(int start, int end) {
  56. return str.toString().substring(start, end);
  57. }
  58. public final CharSequence subSequence(int start, int end) {
  59. try {
  60. int pos = position();
  61. return new StringCharBuffer(str,
  62. pos + checkIndex(start, pos),
  63. pos + checkIndex(end, pos));
  64. } catch (IllegalArgumentException x) {
  65. throw new IndexOutOfBoundsException();
  66. }
  67. }
  68. public boolean isDirect() {
  69. return false;
  70. }
  71. public ByteOrder order() {
  72. return ByteOrder.nativeOrder();
  73. }
  74. }