1 51 52 package org.ozoneDB.xml.util; 53 54 import java.io.InputStream ; 55 import java.io.IOException ; 56 import java.io.EOFException ; 57 import java.io.UTFDataFormatException ; 58 import java.io.Serializable ; 59 60 import java.util.ArrayList ; 61 62 66 67 68 final class CompiledXMLInputStream extends InputStream implements Serializable { 69 70 private final InputStream in; 71 private final ArrayList list = new ArrayList (); 72 73 74 public CompiledXMLInputStream( InputStream input ) throws IOException { 75 this.in = input; 76 } 77 78 79 public final int readEvent() throws IOException { 80 return this.in.read(); 81 } 82 83 84 public final int readAttributes() throws IOException { 85 InputStream in = this.in; 86 int ch1 = in.read(); 87 int ch2 = in.read(); 88 return (ch1 << 8) + (ch2 << 0); 89 } 90 91 92 public final String readString() throws IOException { 93 int length = this.readLength(); 94 int index = length & 0x00007FFF; 95 if (length >= 0x00008000) { 96 return (String )list.get( index ); 97 } else { 98 String str = new String ( this.readChars( index ) ); 99 list.add( str ); 100 return str; 102 } 103 } 104 105 106 public final char[] readChars() throws IOException { 107 return readChars( this.readLength() ); 108 } 109 110 111 public final int read() throws IOException { 112 return this.in.read(); 113 } 114 115 116 private char[] readChars( int len ) throws IOException { 117 char[] str = new char[len]; 118 byte[] bytearr = new byte[len]; 119 int c; 120 int char2; 121 int char3; 122 int count = 0; 123 int i = 0; 124 125 this.readBytes( bytearr ); 126 127 while (count < len) { 128 c = (int)bytearr[count] & 0xff; 129 switch (c >> 4) { 130 case 0: 131 case 1: 132 case 2: 133 case 3: 134 case 4: 135 case 5: 136 case 6: 137 case 7: 138 139 count++; 140 str[i++] = (char)c; 141 break; 142 case 12: 143 case 13: 144 145 count += 2; 146 char2 = (int)bytearr[count - 1]; 147 str[i++] = (char)((c & 0x1F) << 6 | char2 & 0x3F); 148 break; 149 case 14: 150 151 count += 3; 152 char2 = (int)bytearr[count - 2]; 153 char3 = (int)bytearr[count - 1]; 154 str[i++] = (char)((c & 0x0F) << 12 | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0); 155 break; 156 default: 157 158 throw new UTFDataFormatException (); 159 } 160 } 161 162 if (i < len) { 163 char[] newstr = new char[i]; 164 System.arraycopy(str, 0, newstr, 0, i ); 165 return newstr; 166 } 167 168 return str; 169 } 170 171 172 private void readBytes( byte[] b ) throws IOException { 173 InputStream in = this.in; 174 int n = 0; 175 int len = b.length; 176 while (n < len) { 177 int count = in.read( b, n, len - n ); 178 if (count < 0) { 179 throw new EOFException (); 180 } 181 n += count; 182 } 183 } 184 185 186 private int readLength() throws IOException { 187 InputStream in = this.in; 188 int ch1 = in.read(); 189 int ch2 = in.read(); 190 return (ch1 << 8) + (ch2 << 0); 191 } 192 } 193 | Popular Tags |