1 16 package org.mortbay.util; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.Writer ; 21 22 23 24 33 public class ByteArrayISO8859Writer extends Writer 34 { 35 private byte[] _buf; 36 private int _size; 37 private ByteArrayOutputStream2 _bout=null; 38 private OutputStreamWriter _writer=null; 39 private boolean _fixed=false; 40 41 42 44 public ByteArrayISO8859Writer() 45 { 46 _buf=ByteArrayPool.getByteArrayAtLeast(2048); 47 } 48 49 50 53 public ByteArrayISO8859Writer(int capacity) 54 { 55 _buf=ByteArrayPool.getByteArray(capacity); 56 } 57 58 59 public ByteArrayISO8859Writer(byte[] buf) 60 { 61 _buf=buf; 62 _fixed=true; 63 } 64 65 66 public Object getLock() 67 { 68 return lock; 69 } 70 71 72 public int size() 73 { 74 return _size; 75 } 76 77 78 public int capacity() 79 { 80 return _buf.length; 81 } 82 83 84 public int spareCapacity() 85 { 86 return _buf.length-_size; 87 } 88 89 90 public void setLength(int l) 91 { 92 _size=l; 93 } 94 95 96 public byte[] getBuf() 97 { 98 return _buf; 99 } 100 101 102 public void writeTo(OutputStream out) 103 throws IOException 104 { 105 out.write(_buf,0,_size); 106 } 107 108 109 public void write(char c) 110 throws IOException 111 { 112 ensureSpareCapacity(1); 113 if (c>=0&&c<=0x7f) 114 _buf[_size++]=(byte)c; 115 else 116 { 117 char[] ca ={c}; 118 writeEncoded(ca,0,1); 119 } 120 } 121 122 123 public void write(char[] ca) 124 throws IOException 125 { 126 ensureSpareCapacity(ca.length); 127 for (int i=0;i<ca.length;i++) 128 { 129 char c=ca[i]; 130 if (c>=0&&c<=0x7f) 131 _buf[_size++]=(byte)c; 132 else 133 { 134 writeEncoded(ca,i,ca.length-i); 135 break; 136 } 137 } 138 } 139 140 141 public void write(char[] ca,int offset, int length) 142 throws IOException 143 { 144 ensureSpareCapacity(length); 145 for (int i=0;i<length;i++) 146 { 147 char c=ca[offset+i]; 148 if (c>=0&&c<=0x7f) 149 _buf[_size++]=(byte)c; 150 else 151 { 152 writeEncoded(ca,offset+i,length-i); 153 break; 154 } 155 } 156 } 157 158 159 public void write(String s) 160 throws IOException 161 { 162 int length=s.length(); 163 ensureSpareCapacity(length); 164 for (int i=0;i<length;i++) 165 { 166 char c=s.charAt(i); 167 if (c>=0x0&&c<=0x7f) 168 _buf[_size++]=(byte)c; 169 else 170 { 171 writeEncoded(s.toCharArray(),i,length-i); 172 break; 173 } 174 } 175 176 177 } 178 179 180 public void write(String s,int offset, int length) 181 throws IOException 182 { 183 ensureSpareCapacity(length); 184 for (int i=0;i<length;i++) 185 { 186 char c=s.charAt(offset+i); 187 if (c>=0&&c<=0x7f) 188 _buf[_size++]=(byte)c; 189 else 190 { 191 writeEncoded(s.toCharArray(),offset+i,length-i); 192 break; 193 } 194 } 195 } 196 197 198 private void writeEncoded(char[] ca,int offset, int length) 199 throws IOException 200 { 201 if (_bout==null) 202 { 203 _bout = new ByteArrayOutputStream2(2*length); 204 _writer = new OutputStreamWriter (_bout,StringUtil.__ISO_8859_1); 205 } 206 else 207 _bout.reset(); 208 _writer.write(ca,offset,length); 209 _writer.flush(); 210 ensureSpareCapacity(_bout.getCount()); 211 System.arraycopy(_bout.getBuf(),0,_buf,_size,_bout.getCount()); 212 _size+=_bout.getCount(); 213 } 214 215 216 public void flush() 217 {} 218 219 220 public void resetWriter() 221 { 222 _size=0; 223 } 224 225 226 public void close() 227 {} 228 229 230 public void destroy() 231 { 232 ByteArrayPool.returnByteArray(_buf); 233 _buf=null; 234 } 235 236 237 public void ensureSpareCapacity(int n) 238 throws IOException 239 { 240 if (_size+n>_buf.length) 241 { 242 if (_fixed) 243 throw new IOException ("Buffer overflow: "+_buf.length); 244 byte[] buf = new byte[(_buf.length+n)*4/3]; 245 System.arraycopy(_buf,0,buf,0,_size); 246 _buf=buf; 247 } 248 } 249 250 251 protected void finalize() 252 { 253 ByteArrayPool.returnByteArray(_buf); 254 } 255 256 257 public byte[] getByteArray() 258 { 259 byte[] data=new byte[_size]; 260 System.arraycopy(_buf,0,data,0,_size); 261 return data; 262 } 263 264 } 265 266 267 | Popular Tags |