1 /*
   2  * Copyright (c) 2000, 2018, 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         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     private StringCharBuffer(CharSequence s,
  55                              int mark,
  56                              int pos,
  57                              int limit,
  58                              int cap,
  59                              int offset) {
  60         super(mark, pos, limit, cap, null, offset);
  61         str = s;
  62         this.isReadOnly = true;
  63     }
  64 
  65     public CharBuffer duplicate() {
  66         return new StringCharBuffer(str, markValue(),
  67                                     position(), limit(), capacity(), offset);
  68     }
  69 
  70     public CharBuffer asReadOnlyBuffer() {
  71         return duplicate();
  72     }
  73 
  74     public final char get() {
  75         return str.charAt(nextGetIndex() + offset);
  76     }
  77 
  78     public final char get(int index) {
  79         return str.charAt(checkIndex(index) + offset);
  80     }
  81 
  82     char getUnchecked(int index) {
  83         return str.charAt(index + offset);
  84     }
  85 
  86     // ## Override bulk get methods for better performance
  87 
  88     public final CharBuffer put(char c) {
  89         throw new ReadOnlyBufferException();
  90     }
  91 
  92     public final CharBuffer put(int index, char c) {
  93         throw new ReadOnlyBufferException();
  94     }
  95 
  96     public final CharBuffer compact() {
  97         throw new ReadOnlyBufferException();
  98     }
  99 
 100     public final boolean isReadOnly() {
 101         return true;
 102     }
 103 
 104     final String toString(int start, int end) {
 105         return str.subSequence(start + offset, end + offset).toString();
 106     }
 107 
 108     public final CharBuffer subSequence(int start, int end) {
 109         try {
 110             int pos = position();
 111             return new StringCharBuffer(str,
 112                                         -1,
 113                                         pos + checkIndex(start, pos),
 114                                         pos + checkIndex(end, pos),
 115                                         capacity(),
 116                                         offset);
 117         } catch (IllegalArgumentException x) {
 118             throw new IndexOutOfBoundsException();
 119         }
 120     }
 121 
 122     public boolean isDirect() {
 123         return false;
 124     }
 125 
 126     public ByteOrder order() {
 127         return ByteOrder.nativeOrder();
 128     }
 129 
 130     ByteOrder charRegionOrder() {
 131         return null;
 132     }
 133 
 134     public boolean equals(Object ob) {
 135         if (this == ob)
 136             return true;
 137         if (!(ob instanceof CharBuffer))
 138             return false;
 139         CharBuffer that = (CharBuffer)ob;
 140         if (this.remaining() != that.remaining())
 141             return false;
 142         return BufferMismatch.mismatch(this, this.position(),
 143                                        that, that.position(),
 144                                        this.remaining()) < 0;
 145     }
 146 
 147     public int compareTo(CharBuffer that) {
 148         int i = BufferMismatch.mismatch(this, this.position(),
 149                                         that, that.position(),
 150                                         Math.min(this.remaining(), that.remaining()));
 151         if (i >= 0) {
 152             return Character.compare(this.get(this.position() + i), that.get(that.position() + i));
 153         }
 154         return this.remaining() - that.remaining();
 155     }
 156 }