1 9 10 package org.jboss.portal.format.parser.chars; 11 12 import java.io.CharArrayReader ; 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.Writer ; 16 17 21 public class MutableChars implements Chars 22 { 23 24 protected int index; 25 protected char[] chars; 26 protected Writer writer; 27 28 public MutableChars(int length) 29 { 30 this(new char[length]); 31 } 32 33 public MutableChars() 34 { 35 this(20); 36 } 37 38 public MutableChars(char[] chars) 39 { 40 this.index = 0; 41 this.chars = chars; 42 this.writer = new MyWriter(); 43 } 44 45 public MutableChars(String s) 46 { 47 this(s.toCharArray()); 48 } 49 50 private void increase() 51 { 52 char[] temp = new char[1 + chars.length * 2]; 53 System.arraycopy(chars, 0, temp, 0, chars.length); 54 chars = temp; 55 } 56 57 public MutableChars append(String s) 58 { 59 int mark = index; 60 index += s.length(); 61 while (index > chars.length - 1) 62 { 63 increase(); 64 } 65 s.getChars(0, s.length(), chars, mark); 66 return this; 67 } 68 69 public MutableChars append(char cbuf[], int off, int len) 70 { 71 int mark = index; 72 index += len; 73 while (index > chars.length - 1) 74 { 75 increase(); 76 } 77 System.arraycopy(cbuf, off, chars, mark, len); 78 return this; 79 } 80 81 public MutableChars append(char c) 82 { 83 if (index > chars.length - 1) { 85 increase(); 86 } 87 chars[index++] = c; 88 return this; 89 } 90 91 public void reset() 92 { 93 index = 0; 94 } 95 96 public void appendTo(Writer writer) throws IOException 97 { 98 writer.write(chars, 0, index); 99 } 100 101 public String toString() 102 { 103 return new String (chars, 0, index); 104 } 105 106 public Reader getReader() 107 { 108 return new CharArrayReader (chars, 0, index); 109 } 110 111 public int length() 112 { 113 return index; 114 } 115 116 public Writer getWriter() 117 { 118 return writer; 119 } 120 121 public char[] chars() 122 { 123 return chars; 124 } 125 126 public class MyWriter extends Writer 127 { 128 public void write(char cbuf[], int off, int len) throws IOException 129 { 130 MutableChars.this.append(cbuf, off, len); 131 } 132 public void flush() throws IOException 133 { 134 } 135 public void close() throws IOException 136 { 137 } 138 } 139 } 140 | Popular Tags |