Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/StringCharBuffer.java
38829 views
/*1* Copyright (c) 2000, 2013, 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;262728// ## If the sequence is a string, use reflection to share its array2930class StringCharBuffer // package-private31extends CharBuffer32{33CharSequence str;3435StringCharBuffer(CharSequence s, int start, int end) { // package-private36super(-1, start, end, s.length());37int n = s.length();38if ((start < 0) || (start > n) || (end < start) || (end > n))39throw new IndexOutOfBoundsException();40str = s;41}4243public CharBuffer slice() {44int pos = this.position();45int lim = this.limit();46int rem = (pos <= lim ? lim - pos : 0);47return new StringCharBuffer(str,48-1,490,50rem,51rem,52offset + pos);53}5455private StringCharBuffer(CharSequence s,56int mark,57int pos,58int limit,59int cap,60int offset) {61super(mark, pos, limit, cap, null, offset);62str = s;63}6465public CharBuffer duplicate() {66return new StringCharBuffer(str, markValue(),67position(), limit(), capacity(), offset);68}6970public CharBuffer asReadOnlyBuffer() {71return duplicate();72}7374public final char get() {75return str.charAt(nextGetIndex() + offset);76}7778public final char get(int index) {79return str.charAt(checkIndex(index) + offset);80}8182char getUnchecked(int index) {83return str.charAt(index + offset);84}8586// ## Override bulk get methods for better performance8788public final CharBuffer put(char c) {89throw new ReadOnlyBufferException();90}9192public final CharBuffer put(int index, char c) {93throw new ReadOnlyBufferException();94}9596public final CharBuffer compact() {97throw new ReadOnlyBufferException();98}99100public final boolean isReadOnly() {101return true;102}103104final String toString(int start, int end) {105return str.toString().substring(start + offset, end + offset);106}107108public final CharBuffer subSequence(int start, int end) {109try {110int pos = position();111return new StringCharBuffer(str,112-1,113pos + checkIndex(start, pos),114pos + checkIndex(end, pos),115capacity(),116offset);117} catch (IllegalArgumentException x) {118throw new IndexOutOfBoundsException();119}120}121122public boolean isDirect() {123return false;124}125126public ByteOrder order() {127return ByteOrder.nativeOrder();128}129130}131132133