1 /*
   2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  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 
  29 // ## If the sequence is a string, use reflection to share its array
  30 
  31 class StringCharBuffer                                  // package-private
  32     extends CharBuffer
  33 {
  34     CharSequence str;
  35 
  36     StringCharBuffer(CharSequence s, int start, int end) { // package-private
  37         super(-1, start, end, s.length());
  38         int n = s.length();
  39         if ((start < 0) || (start > n) || (end < start) || (end > n))
  40             throw new IndexOutOfBoundsException();
  41         str = s;
  42     }
  43 
  44     public CharBuffer slice() {
  45         return new StringCharBuffer(str,
  46                                     -1,
  47                                     0,
  48                                     this.remaining(),
  49                                     this.remaining(),
  50                                     offset + this.position());
  51     }
  52 
  53     private StringCharBuffer(CharSequence s,
  54                              int mark,
  55                              int pos,
  56                              int limit,
  57                              int cap,
  58                              int offset) {
  59         super(mark, pos, limit, cap, null, offset);
  60         str = s;
  61     }
  62 
  63     public CharBuffer duplicate() {
  64         return new StringCharBuffer(str, markValue(),
  65                                     position(), limit(), capacity(), offset);
  66     }
  67 
  68     public CharBuffer asReadOnlyBuffer() {
  69         return duplicate();
  70     }
  71 
  72     public final char get() {
  73         return str.charAt(nextGetIndex() + offset);
  74     }
  75 
  76     public final char get(int index) {
  77         return str.charAt(checkIndex(index) + offset);
  78     }
  79 
  80     // ## Override bulk get methods for better performance
  81 
  82     public final CharBuffer put(char c) {
  83         throw new ReadOnlyBufferException();
  84     }
  85 
  86     public final CharBuffer put(int index, char c) {
  87         throw new ReadOnlyBufferException();
  88     }
  89 
  90     public final CharBuffer compact() {
  91         throw new ReadOnlyBufferException();
  92     }
  93 
  94     public final boolean isReadOnly() {
  95         return true;
  96     }
  97 
  98     final String toString(int start, int end) {
  99         return str.toString().substring(start + offset, end + offset);
 100     }
 101 
 102     public final CharBuffer subSequence(int start, int end) {
 103         try {
 104             int pos = position();
 105             return new StringCharBuffer(str,
 106                                         -1,
 107                                         pos + checkIndex(start, pos),
 108                                         pos + checkIndex(end, pos),
 109                                         capacity(),
 110                                         offset);
 111         } catch (IllegalArgumentException x) {
 112             throw new IndexOutOfBoundsException();
 113         }
 114     }
 115 
 116     public boolean isDirect() {
 117         return false;
 118     }
 119 
 120     public ByteOrder order() {
 121         return ByteOrder.nativeOrder();
 122     }
 123 
 124 }