Merged with mainline at revision 128810.
[official-gcc.git] / libjava / classpath / java / nio / CharSequenceBuffer.java
blob26aad1c38ac7b26664076c2b6b83ef425c7bcf72
1 /* CharBuffer.java --
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.nio;
41 /**
42 * A CharBuffer that wraps a {@link CharSequence}.
44 final class CharSequenceBuffer
45 extends CharBuffer
48 /**
49 * The wrapped char sequence.
51 private CharSequence charSequence;
53 /**
54 * Creates a new CharSequenceBuffer.
56 * @param charSeq the CharSequence to wrap
57 * @param capacity the capacity
58 * @param limit the limit
59 * @param position the position
60 * @param mark the mark
61 * @param offs the offset
63 CharSequenceBuffer(CharSequence charSeq, int capacity, int limit,
64 int position, int mark, int offs)
66 super(capacity, limit, position, mark);
67 charSequence = charSeq;
68 array_offset = offs;
71 /**
72 * Creates a new instance of CharSequenceBuffer, wrapping the specified
73 * {@link CharSequence}.
75 * @param charSeq the char sequence to wrap
76 * @param start the start index in the char sequence
77 * @param end the end index in the char sequence
79 CharSequenceBuffer(CharSequence charSeq, int start, int end)
81 this(charSeq, charSeq.length(), end, start, -1, 0);
84 /**
85 * Returns a read-only view of this buffer.
87 public CharBuffer asReadOnlyBuffer()
89 return duplicate();
92 /**
93 * This buffer class is not writable by definition and thus throws
94 * a ReadOnlyBufferException here.
96 public CharBuffer compact()
98 throw new ReadOnlyBufferException();
102 * Returns a duplicate of this buffer.
104 * @return a duplicate of this buffer
106 public CharBuffer duplicate()
108 return new CharSequenceBuffer(charSequence, cap, limit, pos, mark, 0);
112 * Returns the character at the current position.
114 * @return the character at the current position
116 public char get()
118 if (pos >= limit)
119 throw new BufferUnderflowException();
121 return charSequence.charAt(array_offset + pos++);
125 * Returns the character at the specified position.
127 * @return the character at the specified position
129 public char get(int index)
131 if (index < 0 || index >= limit)
132 throw new IndexOutOfBoundsException();
134 return charSequence.charAt(array_offset + index);
138 * Cannot be direct, return <code>false</code> here.
140 * @return false
142 public boolean isDirect()
144 return false;
148 * Returns the byte order of this buffer. This is always the native byte
149 * order.
151 * @return the byte order of this buffer
153 public ByteOrder order()
155 return ByteOrder.nativeOrder();
159 * This buffer class is not writable by definition and thus throws
160 * a ReadOnlyBufferException here.
162 public CharBuffer put(char b)
164 throw new ReadOnlyBufferException();
168 * This buffer class is not writable by definition and thus throws
169 * a ReadOnlyBufferException here.
171 public CharBuffer put(int index, char b)
173 throw new ReadOnlyBufferException();
177 * Returns a slice of this buffer, exposing the current position and limit.
179 public CharBuffer slice()
181 int newCapacity = limit - pos;
182 return new CharSequenceBuffer(charSequence, newCapacity, newCapacity, 0,
183 -1, pos);
187 * Returns a sub sequence from the specified start index and with the
188 * specified length.
190 * @param start the start index
191 * @param length the length of the sub sequence
193 public CharSequence subSequence(int start, int length)
195 int begin = array_offset + start + pos;
196 return charSequence.subSequence(begin, begin + length);
200 * This kind of CharBuffer is read-only, so we return <code>true</code>
201 * here.
203 public boolean isReadOnly()
205 return true;