2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.io.OutputStreamWriter;
23 import java.io.Writer;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Arrays;
28 /* ------------------------------------------------------------ */
29 /** Byte Array ISO 8859 writer.
30 * This class combines the features of a OutputStreamWriter for
31 * ISO8859 encoding with that of a ByteArrayOutputStream. It avoids
32 * many inefficiencies associated with these standard library classes.
33 * It has been optimized for standard ASCII characters.
37 public class ByteArrayISO8859Writer extends Writer
41 private ByteArrayOutputStream2 _bout=null;
42 private OutputStreamWriter _writer=null;
43 private boolean _fixed=false;
45 /* ------------------------------------------------------------ */
48 public ByteArrayISO8859Writer()
53 /* ------------------------------------------------------------ */
55 * @param capacity Buffer capacity
57 public ByteArrayISO8859Writer(int capacity)
59 _buf=new byte[capacity];
62 /* ------------------------------------------------------------ */
63 public ByteArrayISO8859Writer(byte[] buf)
69 /* ------------------------------------------------------------ */
70 public Object getLock()
75 /* ------------------------------------------------------------ */
81 /* ------------------------------------------------------------ */
87 /* ------------------------------------------------------------ */
88 public int spareCapacity()
90 return _buf.length-_size;
93 /* ------------------------------------------------------------ */
94 public void setLength(int l)
99 /* ------------------------------------------------------------ */
100 public byte[] getBuf()
105 /* ------------------------------------------------------------ */
106 public void writeTo(OutputStream out)
109 out.write(_buf,0,_size);
112 /* ------------------------------------------------------------ */
113 public void write(char c)
116 ensureSpareCapacity(1);
118 _buf[_size++]=(byte)c;
122 writeEncoded(ca,0,1);
126 /* ------------------------------------------------------------ */
128 public void write(char[] ca)
131 ensureSpareCapacity(ca.length);
132 for (int i=0;i<ca.length;i++)
136 _buf[_size++]=(byte)c;
139 writeEncoded(ca,i,ca.length-i);
145 /* ------------------------------------------------------------ */
147 public void write(char[] ca,int offset, int length)
150 ensureSpareCapacity(length);
151 for (int i=0;i<length;i++)
155 _buf[_size++]=(byte)c;
158 writeEncoded(ca,offset+i,length-i);
164 /* ------------------------------------------------------------ */
166 public void write(String s)
175 int length=s.length();
176 ensureSpareCapacity(length);
177 for (int i=0;i<length;i++)
181 _buf[_size++]=(byte)c;
184 writeEncoded(s.toCharArray(),i,length-i);
190 /* ------------------------------------------------------------ */
192 public void write(String s,int offset, int length)
195 ensureSpareCapacity(length);
196 for (int i=0;i<length;i++)
198 char c=s.charAt(offset+i);
200 _buf[_size++]=(byte)c;
203 writeEncoded(s.toCharArray(),offset+i,length-i);
209 /* ------------------------------------------------------------ */
210 private void writeEncoded(char[] ca,int offset, int length)
215 _bout = new ByteArrayOutputStream2(2*length);
216 _writer = new OutputStreamWriter(_bout,StandardCharsets.ISO_8859_1);
220 _writer.write(ca,offset,length);
222 ensureSpareCapacity(_bout.getCount());
223 System.arraycopy(_bout.getBuf(),0,_buf,_size,_bout.getCount());
224 _size+=_bout.getCount();
227 /* ------------------------------------------------------------ */
232 /* ------------------------------------------------------------ */
233 public void resetWriter()
238 /* ------------------------------------------------------------ */
243 /* ------------------------------------------------------------ */
244 public void destroy()
249 /* ------------------------------------------------------------ */
250 public void ensureSpareCapacity(int n)
253 if (_size+n>_buf.length)
256 throw new IOException("Buffer overflow: "+_buf.length);
257 _buf=Arrays.copyOf(_buf,(_buf.length+n)*4/3);
261 /* ------------------------------------------------------------ */
262 public byte[] getByteArray()
264 return Arrays.copyOf(_buf,_size);