1 package org.apache.lucene.analysis.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * A StringBuilder that allows one to access the array.
23 public class OpenStringBuilder implements Appendable, CharSequence {
27 public OpenStringBuilder() {
31 public OpenStringBuilder(int size) {
35 public OpenStringBuilder(char[] arr, int len) {
39 public void setLength(int len) { this.len = len; }
41 public void set(char[] arr, int end) {
46 public char[] getArray() { return buf; }
47 public int size() { return len; }
48 public int length() { return len; }
49 public int capacity() { return buf.length; }
51 public Appendable append(CharSequence csq) {
52 return append(csq, 0, csq.length());
55 public Appendable append(CharSequence csq, int start, int end) {
57 for (int i=start; i<end; i++) {
58 unsafeWrite(csq.charAt(i));
63 public Appendable append(char c) {
68 public char charAt(int index) {
72 public void setCharAt(int index, char ch) {
76 public CharSequence subSequence(int start, int end) {
77 throw new UnsupportedOperationException(); // todo
80 public void unsafeWrite(char b) {
84 public void unsafeWrite(int b) { unsafeWrite((char)b); }
86 public void unsafeWrite(char b[], int off, int len) {
87 System.arraycopy(b, off, buf, this.len, len);
91 protected void resize(int len) {
92 char newbuf[] = new char[Math.max(buf.length << 1, len)];
93 System.arraycopy(buf, 0, newbuf, 0, size());
97 public void reserve(int num) {
98 if (len + num > buf.length) resize(len + num);
101 public void write(char b) {
102 if (len >= buf.length) {
108 public void write(int b) { write((char)b); }
110 public final void write(char[] b) {
114 public void write(char b[], int off, int len) {
116 unsafeWrite(b, off, len);
119 public final void write(OpenStringBuilder arr) {
120 write(arr.buf, 0, len);
123 public void write(String s) {
125 s.getChars(0,s.length(),buf, len);
129 public void flush() {
132 public final void reset() {
136 public char[] toCharArray() {
137 char newbuf[] = new char[size()];
138 System.arraycopy(buf, 0, newbuf, 0, size());
142 public String toString() {
143 return new String(buf, 0, size());