1 /* 2 * Copyright (c) 2000, 2021, 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 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 StringCharBuffer(CharSequence s, int start, int end)37 StringCharBuffer(CharSequence s, int start, int end) { // package-private 38 super(-1, start, end, s.length()); 39 int n = s.length(); 40 if ((start < 0) || (start > n) || (end < start) || (end > n)) 41 throw new IndexOutOfBoundsException(); 42 str = s; 43 } 44 slice()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 slice(int index, int length)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 StringCharBuffer(CharSequence s, int mark, int pos, int limit, int cap, int offset)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 } 74 duplicate()75 public CharBuffer duplicate() { 76 return new StringCharBuffer(str, markValue(), 77 position(), limit(), capacity(), offset); 78 } 79 asReadOnlyBuffer()80 public CharBuffer asReadOnlyBuffer() { 81 return duplicate(); 82 } 83 get()84 public final char get() { 85 return str.charAt(nextGetIndex() + offset); 86 } 87 get(int index)88 public final char get(int index) { 89 return str.charAt(checkIndex(index) + offset); 90 } 91 getUnchecked(int index)92 char getUnchecked(int index) { 93 return str.charAt(index + offset); 94 } 95 96 // ## Override bulk get methods for better performance 97 put(char c)98 public final CharBuffer put(char c) { 99 throw new ReadOnlyBufferException(); 100 } 101 put(int index, char c)102 public final CharBuffer put(int index, char c) { 103 throw new ReadOnlyBufferException(); 104 } 105 compact()106 public final CharBuffer compact() { 107 throw new ReadOnlyBufferException(); 108 } 109 isReadOnly()110 public final boolean isReadOnly() { 111 return true; 112 } 113 toString(int start, int end)114 final String toString(int start, int end) { 115 return str.toString().substring(start + offset, end + offset); 116 } 117 subSequence(int start, int end)118 public final CharBuffer subSequence(int start, int end) { 119 try { 120 int pos = position(); 121 return new StringCharBuffer(str, 122 -1, 123 pos + checkIndex(start, pos), 124 pos + checkIndex(end, pos), 125 capacity(), 126 offset); 127 } catch (IllegalArgumentException x) { 128 throw new IndexOutOfBoundsException(); 129 } 130 } 131 isDirect()132 public boolean isDirect() { 133 return false; 134 } 135 order()136 public ByteOrder order() { 137 return ByteOrder.nativeOrder(); 138 } 139 charRegionOrder()140 ByteOrder charRegionOrder() { 141 return null; 142 } 143 isAddressable()144 boolean isAddressable() { 145 return false; 146 } 147 equals(Object ob)148 public boolean equals(Object ob) { 149 if (this == ob) 150 return true; 151 if (!(ob instanceof CharBuffer)) 152 return false; 153 CharBuffer that = (CharBuffer)ob; 154 int thisPos = this.position(); 155 int thisRem = this.limit() - thisPos; 156 int thatPos = that.position(); 157 int thatRem = that.limit() - thatPos; 158 if (thisRem < 0 || thisRem != thatRem) 159 return false; 160 return BufferMismatch.mismatch(this, thisPos, 161 that, thatPos, 162 thisRem) < 0; 163 } 164 compareTo(CharBuffer that)165 public int compareTo(CharBuffer that) { 166 int thisPos = this.position(); 167 int thisRem = this.limit() - thisPos; 168 int thatPos = that.position(); 169 int thatRem = that.limit() - thatPos; 170 int length = Math.min(thisRem, thatRem); 171 if (length < 0) 172 return -1; 173 int i = BufferMismatch.mismatch(this, thisPos, 174 that, thatPos, 175 length); 176 if (i >= 0) { 177 return Character.compare(this.get(thisPos + i), that.get(thatPos + i)); 178 } 179 return thisRem - thatRem; 180 } 181 } 182