1 /* 2 * Copyright (c) 2000, 2013, 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 StringCharBuffer(CharSequence s, int start, int end)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 slice()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 StringCharBuffer(CharSequence s, int mark, int pos, int limit, int cap, int offset)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 duplicate()63 public CharBuffer duplicate() { 64 return new StringCharBuffer(str, markValue(), 65 position(), limit(), capacity(), offset); 66 } 67 asReadOnlyBuffer()68 public CharBuffer asReadOnlyBuffer() { 69 return duplicate(); 70 } 71 get()72 public final char get() { 73 return str.charAt(nextGetIndex() + offset); 74 } 75 get(int index)76 public final char get(int index) { 77 return str.charAt(checkIndex(index) + offset); 78 } 79 getUnchecked(int index)80 char getUnchecked(int index) { 81 return str.charAt(index + offset); 82 } 83 84 // ## Override bulk get methods for better performance 85 put(char c)86 public final CharBuffer put(char c) { 87 throw new ReadOnlyBufferException(); 88 } 89 put(int index, char c)90 public final CharBuffer put(int index, char c) { 91 throw new ReadOnlyBufferException(); 92 } 93 compact()94 public final CharBuffer compact() { 95 throw new ReadOnlyBufferException(); 96 } 97 isReadOnly()98 public final boolean isReadOnly() { 99 return true; 100 } 101 toString(int start, int end)102 final String toString(int start, int end) { 103 return str.toString().substring(start + offset, end + offset); 104 } 105 subSequence(int start, int end)106 public final CharBuffer subSequence(int start, int end) { 107 try { 108 int pos = position(); 109 return new StringCharBuffer(str, 110 -1, 111 pos + checkIndex(start, pos), 112 pos + checkIndex(end, pos), 113 capacity(), 114 offset); 115 } catch (IllegalArgumentException x) { 116 throw new IndexOutOfBoundsException(); 117 } 118 } 119 isDirect()120 public boolean isDirect() { 121 return false; 122 } 123 order()124 public ByteOrder order() { 125 return ByteOrder.nativeOrder(); 126 } 127 128 } 129