KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > CompiledXMLOutputStream


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 2000 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Cocoon" and "Apache Software Foundation" must not be used to
26     endorse or promote products derived from this software without prior
27     written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49
50  */

51
52 package org.ozoneDB.xml.util;
53
54 import java.io.OutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.UTFDataFormatException JavaDoc;
57 import java.io.Serializable JavaDoc;
58
59 import java.util.HashMap JavaDoc;
60
61 /**
62  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>, modifications
63  * for Ozone by <a HREF="mailto:conny@smb-tec.com">Conny Krappatsch</a>
64  */

65 final class CompiledXMLOutputStream extends OutputStream JavaDoc implements Serializable JavaDoc {
66     
67     private static final boolean COMPRESS = true;
68     
69     private final OutputStream JavaDoc out;
70     private final HashMap JavaDoc map = new HashMap JavaDoc();
71     private int indexCount = 0;
72     
73     
74     public CompiledXMLOutputStream( OutputStream JavaDoc out ) throws IOException JavaDoc{
75         this.out = out;
76     }
77     
78     
79     public final void writeEvent( int event ) throws IOException JavaDoc {
80         this.out.write( event );
81     }
82     
83     
84     public final void writeAttributes( int attributes ) throws IOException JavaDoc {
85         OutputStream JavaDoc out = this.out;
86         out.write( attributes >>> 8 & 0xFF );
87         out.write( attributes & 0xFF );
88 // out.write( attributes >>> 0 & 0xFF );
89
}
90     
91     
92     public final void writeString( String JavaDoc str ) throws IOException JavaDoc {
93         Integer JavaDoc index;
94         
95         if (CompiledXMLOutputStream.COMPRESS) {
96             index = (Integer JavaDoc)map.get( str );
97         } else {
98             index = null;
99         }
100         
101         if (index == null) {
102             int length = str.length();
103             if (CompiledXMLOutputStream.COMPRESS) {
104                 map.put(str, new Integer JavaDoc(indexCount++));
105                // System.out.println( "index=" + indexCount + ", str=" + str);
106
}
107             if (length > 2 << 15) {
108                 throw new IOException JavaDoc( "String cannot be bigger than 32K" );
109             }
110             this.writeChars( str.toCharArray(), 0, length );
111         } else {
112             int i = index.intValue();
113             this.out.write( i >>> 8 & 0xFF | 0x80 );
114             this.out.write( i & 0xFF );
115         }
116     }
117     
118     
119     public final void writeChars( char[] ch, int start, int length ) throws IOException JavaDoc {
120         int utflen = 0;
121         int c;
122         int count = 0;
123         int end = start + length;
124         
125         /* looks strange but is fast */
126         for (int i = start; i < end; i++) {
127             c = ch[i];
128             utflen += (c >= 0x0001 && c <= 0x007F) ? 1 : (c > 0x07FF) ? 3 : 2;
129         }
130         
131         if (utflen > 65535) {
132             throw new UTFDataFormatException JavaDoc();
133         }
134         
135         byte[] bytearr = new byte[utflen + 2];
136         bytearr[count++] = (byte)(utflen >>> 8 & 0xFF);
137         bytearr[count++] = (byte)(utflen >>> 0 & 0xFF);
138         for (int i = start; i < end; i++) {
139             c = ch[i];
140             if (c >= 0x0001 && c <= 0x007F) {
141                 bytearr[count++] = (byte)c;
142             } else if (c > 0x07FF) {
143                 bytearr[count++] = (byte)(0xE0 | c >> 12 & 0x0F);
144                 bytearr[count++] = (byte)(0x80 | c >> 6 & 0x3F);
145                 bytearr[count++] = (byte)(0x80 | c >> 0 & 0x3F);
146             } else {
147                 bytearr[count++] = (byte)(0xC0 | c >> 6 & 0x1F);
148                 bytearr[count++] = (byte)(0x80 | c >> 0 & 0x3F);
149             }
150         }
151         
152         this.out.write(bytearr, 0, count);
153     }
154     
155     
156     public final void write(int c) throws IOException JavaDoc {
157         this.out.write(c);
158     }
159 }
160
Popular Tags