1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.nio; 19 20/** 21 * This class wraps a char sequence to be a char buffer. 22 * <p> 23 * Implementation notice: 24 * <ul> 25 * <li>Char sequence based buffer is always readonly.</li> 26 * </ul> 27 * </p> 28 * 29 */ 30final class CharSequenceAdapter extends CharBuffer { 31 32 static CharSequenceAdapter copy(CharSequenceAdapter other) { 33 CharSequenceAdapter buf = new CharSequenceAdapter(other.sequence); 34 buf.limit = other.limit; 35 buf.position = other.position; 36 buf.mark = other.mark; 37 return buf; 38 } 39 40 final CharSequence sequence; 41 42 CharSequenceAdapter(CharSequence chseq) { 43 super(chseq.length()); 44 sequence = chseq; 45 } 46 47 @Override 48 public CharBuffer asReadOnlyBuffer() { 49 return duplicate(); 50 } 51 52 @Override 53 public CharBuffer compact() { 54 throw new ReadOnlyBufferException(); 55 } 56 57 @Override 58 public CharBuffer duplicate() { 59 return copy(this); 60 } 61 62 @Override 63 public char get() { 64 if (position == limit) { 65 throw new BufferUnderflowException(); 66 } 67 return sequence.charAt(position++); 68 } 69 70 @Override 71 public char get(int index) { 72 if (index < 0 || index >= limit) { 73 throw new IndexOutOfBoundsException(); 74 } 75 return sequence.charAt(index); 76 } 77 78 @Override 79 public final CharBuffer get(char[] dest, int off, int len) { 80 int length = dest.length; 81 if ((off < 0) || (len < 0) || (long) off + (long) len > length) { 82 throw new IndexOutOfBoundsException(); 83 } 84 if (len > remaining()) { 85 throw new BufferUnderflowException(); 86 } 87 int newPosition = position + len; 88 sequence.toString().getChars(position, newPosition, dest, off); 89 position = newPosition; 90 return this; 91 } 92 93 @Override 94 public boolean isDirect() { 95 return false; 96 } 97 98 @Override 99 public boolean isReadOnly() { 100 return true; 101 } 102 103 @Override 104 public ByteOrder order() { 105 return ByteOrder.nativeOrder(); 106 } 107 108 @Override 109 protected char[] protectedArray() { 110 throw new UnsupportedOperationException(); 111 } 112 113 @Override 114 protected int protectedArrayOffset() { 115 throw new UnsupportedOperationException(); 116 } 117 118 @Override 119 protected boolean protectedHasArray() { 120 return false; 121 } 122 123 @Override 124 public CharBuffer put(char c) { 125 throw new ReadOnlyBufferException(); 126 } 127 128 @Override 129 public CharBuffer put(int index, char c) { 130 throw new ReadOnlyBufferException(); 131 } 132 133 @Override 134 public final CharBuffer put(char[] src, int off, int len) { 135 if ((off < 0) || (len < 0) || (long) off + (long) len > src.length) { 136 throw new IndexOutOfBoundsException(); 137 } 138 139 if (len > remaining()) { 140 throw new BufferOverflowException(); 141 } 142 143 throw new ReadOnlyBufferException(); 144 } 145 146 @Override 147 public CharBuffer put(String src, int start, int end) { 148 if ((start < 0) || (end < 0) 149 || (long) start + (long) end > src.length()) { 150 throw new IndexOutOfBoundsException(); 151 } 152 throw new ReadOnlyBufferException(); 153 } 154 155 @Override 156 public CharBuffer slice() { 157 return new CharSequenceAdapter(sequence.subSequence(position, limit)); 158 } 159 160 @Override 161 public CharSequence subSequence(int start, int end) { 162 if (end < start || start < 0 || end > remaining()) { 163 throw new IndexOutOfBoundsException(); 164 } 165 166 CharSequenceAdapter result = copy(this); 167 result.position = position + start; 168 result.limit = position + end; 169 return result; 170 } 171} 172