1 26 27 package com.opensugar.cube.java2; 28 29 import java.io.FilterInputStream ; 30 import java.io.DataOutputStream ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.io.IOException ; 34 import java.util.zip.ZipFile ; 35 import java.util.Properties ; 36 37 public class ManifestHelper3 { 38 39 private static final String MANIFEST_FILE = "META-INF/MANIFEST.MF"; 40 41 private Properties attributes = new Properties (); 43 44 50 public ManifestHelper3( ZipFile archive ) throws IOException { 51 InputStream is = archive.getInputStream( archive.getEntry( MANIFEST_FILE ) ); 52 read( is ); 53 } 54 55 59 public Properties getMainAttributes() { 60 return attributes; 61 } 62 63 71 public void read( InputStream is ) throws IOException { 72 FastInputStream fis = new FastInputStream( is ); 74 byte[] lbuf = new byte[ 512 ]; 76 readAttributes( fis, lbuf ); 78 79 } 81 82 private void readAttributes( FastInputStream is, byte[] lbuf ) throws IOException { 83 String name = null, value = null; 84 int len; 85 while ( ( len = is.readLine( lbuf ) ) != -1 ) { 86 if ( lbuf[ --len ] != '\n' ) { 87 throw new IOException ( "line too long" ); 88 } 89 if ( len > 0 && lbuf[ len-1 ] == '\r' ) { 90 --len; 91 } 92 if ( len == 0 ) { 93 break; 94 } 95 int i = 0; 96 if ( lbuf[0] == ' ' ) { 97 if ( name == null ) { 99 throw new IOException ( "misplaced continuation line" ); 100 } 101 value = value + new String ( lbuf, 0, 1, len - 1 ); 102 } 103 else { 104 while ( lbuf[ i++ ] != ':' ) { 105 if ( i >= len ) { 106 throw new IOException ("invalid header field"); 107 } 108 } 109 if ( lbuf[ i++ ] != ' ' ) { 110 throw new IOException ("invalid header field"); 111 } 112 name = new String (lbuf, 0, 0, i - 2); 113 value = new String (lbuf, 0, i, len - i); 114 } 115 116 try { 117 System.out.println( "manifest attr: " + name + ": _" + value + "_" ); 118 attributes.put( name, value ); 119 } 120 catch ( IllegalArgumentException e ) { 121 throw new IOException ( "invalid header field name: " + name ); 122 } 123 } 124 } 125 126 127 static class FastInputStream extends FilterInputStream { 128 129 private byte buf[]; 130 private int count = 0; 131 private int pos = 0; 132 133 FastInputStream( InputStream in ) { 134 this( in, 8192 ); 135 } 136 137 FastInputStream( InputStream in, int size ) { 138 super( in ); 139 buf = new byte[ size ]; 140 } 141 142 public int read() throws IOException { 143 if ( pos >= count ) { 144 fill(); 145 if ( pos >= count ) { 146 return -1; 147 } 148 } 149 return buf[ pos++ ] & 0xff; 150 } 151 152 public int read( byte[] b, int off, int len ) throws IOException { 153 int avail = count - pos; 154 if ( avail <= 0 ) { 155 if ( len >= buf.length ) { 156 return in.read( b, off, len ); 157 } 158 fill(); 159 avail = count - pos; 160 if ( avail <= 0 ) { 161 return -1; 162 } 163 } 164 if ( len > avail ) { 165 len = avail; 166 } 167 System.arraycopy( buf, pos, b, off, len ); 168 pos += len; 169 return len; 170 } 171 172 176 public int readLine( byte[] b, int off, int len ) throws IOException { 177 byte[] tbuf = this.buf; 178 int total = 0; 179 while ( total < len ) { 180 int avail = count - pos; 181 if ( avail <= 0 ) { 182 fill(); 183 avail = count - pos; 184 if ( avail <= 0 ) { 185 return -1; 186 } 187 } 188 int n = len - total; 189 if ( n > avail ) { 190 n = avail; 191 } 192 int tpos = pos; 193 int maxpos = tpos + n; 194 while ( tpos < maxpos && tbuf[ tpos++ ] != '\n' ) ; 195 n = tpos - pos; 196 System.arraycopy( tbuf, pos, b, off, n ); 197 off += n; 198 total += n; 199 pos = tpos; 200 if ( tbuf[ tpos-1 ] == '\n' ) { 201 break; 202 } 203 } 204 return total; 205 } 206 207 public byte peek() throws IOException { 208 if ( pos == count ) { 209 fill(); 210 } 211 return buf[ pos ]; 212 } 213 214 public int readLine( byte[] b ) throws IOException { 215 return readLine( b, 0, b.length ); 216 } 217 218 public long skip( long n ) throws IOException { 219 if ( n <= 0 ) { 220 return 0; 221 } 222 long avail = count - pos; 223 if ( avail <= 0 ) { 224 return in.skip( n ); 225 } 226 if ( n > avail ) { 227 n = avail; 228 } 229 pos += n; 230 return n; 231 } 232 233 public int available() throws IOException { 234 return ( count - pos ) + in.available(); 235 } 236 237 public void close() throws IOException { 238 if ( in != null ) { 239 in.close(); 240 in = null; 241 buf = null; 242 } 243 } 244 245 private void fill() throws IOException { 246 count = pos = 0; 247 int n = in.read( buf, 0, buf.length ); 248 if ( n > 0 ) { 249 count = n; 250 } 251 } 252 } 253 254 } 255 | Popular Tags |