< prev index next >

src/java.base/share/classes/java/nio/StringCharBuffer.java

Print this page
rev 57242 : imported patch 8234049.patch


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import java.util.Objects;
  29 
  30 // ## If the sequence is a string, use reflection to share its array
  31 
  32 class StringCharBuffer                                  // package-private
  33     extends CharBuffer
  34 {
  35     CharSequence str;
  36 
  37     StringCharBuffer(CharSequence s, int start, int end) { // package-private
  38         super(-1, start, end, s.length());
  39         int n = s.length();
  40         Objects.checkFromToIndex(start, end, n);
  41         str = s;
  42         this.isReadOnly = true;
  43     }
  44 
  45     public CharBuffer slice() {
  46         return new StringCharBuffer(str,
  47                                     -1,
  48                                     0,
  49                                     this.remaining(),
  50                                     this.remaining(),
  51                                     offset + this.position());
  52     }
  53 
  54     @Override
  55     public CharBuffer slice(int index, int length) {
  56         Objects.checkFromIndexSize(index, length, limit());
  57         return new StringCharBuffer(str,
  58                                     -1,
  59                                     0,
  60                                     length,
  61                                     length,
  62                                     offset + index);
  63     }
  64 
  65     private StringCharBuffer(CharSequence s,
  66                              int mark,
  67                              int pos,
  68                              int limit,
  69                              int cap,
  70                              int offset) {
  71         super(mark, pos, limit, cap, null, offset);
  72         str = s;
  73         this.isReadOnly = true;
  74     }
  75 
  76     public CharBuffer duplicate() {
  77         return new StringCharBuffer(str, markValue(),
  78                                     position(), limit(), capacity(), offset);
  79     }
  80 
  81     public CharBuffer asReadOnlyBuffer() {
  82         return duplicate();
  83     }
  84 
  85     public final char get() {
  86         return str.charAt(nextGetIndex() + offset);
  87     }
  88 
  89     public final char get(int index) {
  90         return str.charAt(checkIndex(index) + offset);
  91     }




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import java.util.Objects;
  29 
  30 // ## If the sequence is a string, use reflection to share its array
  31 
  32 class StringCharBuffer                                  // package-private
  33     extends CharBuffer
  34 {
  35     CharSequence str;
  36 
  37     StringCharBuffer(CharSequence s, int start, int end) { // package-private
  38         super(-1, start, end, s.length(), null);
  39         int n = s.length();
  40         Objects.checkFromToIndex(start, end, n);
  41         str = s;
  42         this.isReadOnly = true;
  43     }
  44 
  45     public CharBuffer slice() {
  46         return new StringCharBuffer(str,
  47                                     -1,
  48                                     0,
  49                                     this.remaining(),
  50                                     this.remaining(),
  51                                     offset + this.position());
  52     }
  53 
  54     @Override
  55     public CharBuffer slice(int index, int length) {
  56         Objects.checkFromIndexSize(index, length, limit());
  57         return new StringCharBuffer(str,
  58                                     -1,
  59                                     0,
  60                                     length,
  61                                     length,
  62                                     offset + index);
  63     }
  64 
  65     private StringCharBuffer(CharSequence s,
  66                              int mark,
  67                              int pos,
  68                              int limit,
  69                              int cap,
  70                              int offset) {
  71         super(mark, pos, limit, cap, null, offset, null);
  72         str = s;
  73         this.isReadOnly = true;
  74     }
  75 
  76     public CharBuffer duplicate() {
  77         return new StringCharBuffer(str, markValue(),
  78                                     position(), limit(), capacity(), offset);
  79     }
  80 
  81     public CharBuffer asReadOnlyBuffer() {
  82         return duplicate();
  83     }
  84 
  85     public final char get() {
  86         return str.charAt(nextGetIndex() + offset);
  87     }
  88 
  89     public final char get(int index) {
  90         return str.charAt(checkIndex(index) + offset);
  91     }


< prev index next >