Path: blob/master/src/java.base/share/classes/java/nio/StringCharBuffer.java
41152 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio;2627import java.util.Objects;2829// ## If the sequence is a string, use reflection to share its array3031class StringCharBuffer // package-private32extends CharBuffer33{34CharSequence str;3536StringCharBuffer(CharSequence s, int start, int end) { // package-private37super(-1, start, end, s.length(), null);38int n = s.length();39Objects.checkFromToIndex(start, end, n);40str = s;41this.isReadOnly = true;42}4344public CharBuffer slice() {45int pos = this.position();46int lim = this.limit();47int rem = (pos <= lim ? lim - pos : 0);48return new StringCharBuffer(str,49-1,500,51rem,52rem,53offset + pos);54}5556@Override57public CharBuffer slice(int index, int length) {58Objects.checkFromIndexSize(index, length, limit());59return new StringCharBuffer(str,60-1,610,62length,63length,64offset + index);65}6667private StringCharBuffer(CharSequence s,68int mark,69int pos,70int limit,71int cap,72int offset) {73super(mark, pos, limit, cap, null, offset, null);74str = s;75this.isReadOnly = true;76}7778public CharBuffer duplicate() {79return new StringCharBuffer(str, markValue(),80position(), limit(), capacity(), offset);81}8283public CharBuffer asReadOnlyBuffer() {84return duplicate();85}8687public final char get() {88return str.charAt(nextGetIndex() + offset);89}9091public final char get(int index) {92return str.charAt(checkIndex(index) + offset);93}9495char getUnchecked(int index) {96return str.charAt(index + offset);97}9899// ## Override bulk get methods for better performance100101public final CharBuffer put(char c) {102throw new ReadOnlyBufferException();103}104105public final CharBuffer put(int index, char c) {106throw new ReadOnlyBufferException();107}108109public final CharBuffer compact() {110throw new ReadOnlyBufferException();111}112113public final boolean isReadOnly() {114return true;115}116117final String toString(int start, int end) {118return str.subSequence(start + offset, end + offset).toString();119}120121public final CharBuffer subSequence(int start, int end) {122try {123int pos = position();124return new StringCharBuffer(str,125-1,126pos + checkIndex(start, pos),127pos + checkIndex(end, pos),128capacity(),129offset);130} catch (IllegalArgumentException x) {131throw new IndexOutOfBoundsException();132}133}134135public boolean isDirect() {136return false;137}138139public ByteOrder order() {140return ByteOrder.nativeOrder();141}142143ByteOrder charRegionOrder() {144return null;145}146147boolean isAddressable() {148return false;149}150151public boolean equals(Object ob) {152if (this == ob)153return true;154if (!(ob instanceof CharBuffer that))155return false;156int thisPos = this.position();157int thisRem = this.limit() - thisPos;158int thatPos = that.position();159int thatRem = that.limit() - thatPos;160if (thisRem < 0 || thisRem != thatRem)161return false;162return BufferMismatch.mismatch(this, thisPos,163that, thatPos,164thisRem) < 0;165}166167public int compareTo(CharBuffer that) {168int thisPos = this.position();169int thisRem = this.limit() - thisPos;170int thatPos = that.position();171int thatRem = that.limit() - thatPos;172int length = Math.min(thisRem, thatRem);173if (length < 0)174return -1;175int i = BufferMismatch.mismatch(this, thisPos,176that, thatPos,177length);178if (i >= 0) {179return Character.compare(this.get(thisPos + i), that.get(thatPos + i));180}181return thisRem - thatRem;182}183}184185186