KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > ByteArrayISO8859Writer


1 // ========================================================================
2
// $Id: ByteArrayISO8859Writer.java,v 1.14 2004/10/23 09:03:22 gregwilkins Exp $
3
// Copyright 2001-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.io.OutputStreamWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21
22
23 /* ------------------------------------------------------------ */
24 /** Byte Array ISO 8859 writer.
25  * This class combines the features of a OutputStreamWriter for
26  * ISO8859 encoding with that of a ByteArrayOutputStream. It avoids
27  * many inefficiencies associated with these standard library classes.
28  * It has been optimized for standard ASCII characters.
29  *
30  * @version $Revision: 1.14 $
31  * @author Greg Wilkins (gregw)
32  */

33 public class ByteArrayISO8859Writer extends Writer JavaDoc
34 {
35     private byte[] _buf;
36     private int _size;
37     private ByteArrayOutputStream2 _bout=null;
38     private OutputStreamWriter JavaDoc _writer=null;
39     private boolean _fixed=false;
40
41     /* ------------------------------------------------------------ */
42     /** Constructor.
43      */

44     public ByteArrayISO8859Writer()
45     {
46         _buf=ByteArrayPool.getByteArrayAtLeast(2048);
47     }
48     
49     /* ------------------------------------------------------------ */
50     /** Constructor.
51      * @param capacity Buffer capacity
52      */

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 JavaDoc 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 JavaDoc out)
103         throws IOException JavaDoc
104     {
105         out.write(_buf,0,_size);
106     }
107
108     /* ------------------------------------------------------------ */
109     public void write(char c)
110         throws IOException JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc s)
160         throws IOException JavaDoc
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 JavaDoc s,int offset, int length)
181         throws IOException JavaDoc
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 JavaDoc
200     {
201         if (_bout==null)
202         {
203             _bout = new ByteArrayOutputStream2(2*length);
204             _writer = new OutputStreamWriter JavaDoc(_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 JavaDoc
239     {
240         if (_size+n>_buf.length)
241         {
242             if (_fixed)
243                 throw new IOException JavaDoc("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