001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.format.parser.chars;
023:
024: import java.io.CharArrayReader;
025: import java.io.IOException;
026: import java.io.Reader;
027: import java.io.Writer;
028:
029: /**
030: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
031: * @version $Revision: 8784 $
032: */
033: public class MutableChars implements Chars {
034:
035: protected int index;
036: protected char[] chars;
037: protected Writer writer;
038:
039: public MutableChars(int length) {
040: this (new char[length]);
041: }
042:
043: public MutableChars() {
044: this (20);
045: }
046:
047: public MutableChars(char[] chars) {
048: this .index = 0;
049: this .chars = chars;
050: this .writer = new MyWriter();
051: }
052:
053: public MutableChars(String s) {
054: this (s.toCharArray());
055: }
056:
057: private void increase() {
058: char[] temp = new char[1 + chars.length * 2];
059: System.arraycopy(chars, 0, temp, 0, chars.length);
060: chars = temp;
061: }
062:
063: public MutableChars append(String s) {
064: int mark = index;
065: index += s.length();
066: while (index > chars.length - 1) {
067: increase();
068: }
069: s.getChars(0, s.length(), chars, mark);
070: return this ;
071: }
072:
073: public MutableChars append(char cbuf[], int off, int len) {
074: int mark = index;
075: index += len;
076: while (index > chars.length - 1) {
077: increase();
078: }
079: System.arraycopy(cbuf, off, chars, mark, len);
080: return this ;
081: }
082:
083: public MutableChars append(char c) {
084: if (index > chars.length - 1) // ~ to index >= length
085: {
086: increase();
087: }
088: chars[index++] = c;
089: return this ;
090: }
091:
092: public void reset() {
093: index = 0;
094: }
095:
096: public void appendTo(Writer writer) throws IOException {
097: writer.write(chars, 0, index);
098: }
099:
100: public String toString() {
101: return new String(chars, 0, index);
102: }
103:
104: public Reader getReader() {
105: return new CharArrayReader(chars, 0, index);
106: }
107:
108: public int length() {
109: return index;
110: }
111:
112: public Writer getWriter() {
113: return writer;
114: }
115:
116: public char[] chars() {
117: return chars;
118: }
119:
120: public class MyWriter extends Writer {
121: public void write(char cbuf[], int off, int len)
122: throws IOException {
123: MutableChars.this .append(cbuf, off, len);
124: }
125:
126: public void flush() throws IOException {
127: }
128:
129: public void close() throws IOException {
130: }
131: }
132: }
|