1 51 52 package org.ozoneDB.xml.util; 53 54 import java.io.OutputStream ; 55 import java.io.IOException ; 56 import java.io.UTFDataFormatException ; 57 import java.io.Serializable ; 58 59 import java.util.HashMap ; 60 61 65 final class CompiledXMLOutputStream extends OutputStream implements Serializable { 66 67 private static final boolean COMPRESS = true; 68 69 private final OutputStream out; 70 private final HashMap map = new HashMap (); 71 private int indexCount = 0; 72 73 74 public CompiledXMLOutputStream( OutputStream out ) throws IOException { 75 this.out = out; 76 } 77 78 79 public final void writeEvent( int event ) throws IOException { 80 this.out.write( event ); 81 } 82 83 84 public final void writeAttributes( int attributes ) throws IOException { 85 OutputStream out = this.out; 86 out.write( attributes >>> 8 & 0xFF ); 87 out.write( attributes & 0xFF ); 88 } 90 91 92 public final void writeString( String str ) throws IOException { 93 Integer index; 94 95 if (CompiledXMLOutputStream.COMPRESS) { 96 index = (Integer )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 (indexCount++)); 105 } 107 if (length > 2 << 15) { 108 throw new IOException ( "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 { 120 int utflen = 0; 121 int c; 122 int count = 0; 123 int end = start + length; 124 125 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 (); 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 { 157 this.out.write(c); 158 } 159 } 160 | Popular Tags |