KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > java2 > ManifestHelper3


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube.java2;
28
29 import java.io.FilterInputStream JavaDoc;
30 import java.io.DataOutputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.zip.ZipFile JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 public class ManifestHelper3 {
38
39    private static final String JavaDoc MANIFEST_FILE = "META-INF/MANIFEST.MF";
40
41    // manifest main attributes
42
private Properties JavaDoc attributes = new Properties JavaDoc();
43
44    /**
45     * Constructs a new Manifest from the specified input stream.
46     *
47     * @param is the input stream containing manifest data
48     * @throws IOException if an I/O error has occured
49     */

50    public ManifestHelper3( ZipFile JavaDoc archive ) throws IOException JavaDoc {
51       InputStream JavaDoc is = archive.getInputStream( archive.getEntry( MANIFEST_FILE ) );
52       read( is );
53    }
54
55    /**
56     * Returns the main Attributes for the Manifest.
57     * @return the main Attributes for the Manifest
58     */

59    public Properties JavaDoc getMainAttributes() {
60       return attributes;
61    }
62
63    /**
64     * Reads the Manifest from the specified InputStream. The entry
65     * names and attributes read will be merged in with the current
66     * manifest entries.
67     *
68     * @param is the input stream
69     * @exception IOException if an I/O error has occurred
70     */

71    public void read( InputStream JavaDoc is ) throws IOException JavaDoc {
72       // Buffered input stream for reading manifest data
73
FastInputStream fis = new FastInputStream( is );
74        // Line buffer
75
byte[] lbuf = new byte[ 512 ];
76        // Read the main attributes for the manifest
77
readAttributes( fis, lbuf );
78
79       // Do not treat entries.
80
}
81
82    private void readAttributes( FastInputStream is, byte[] lbuf ) throws IOException JavaDoc {
83     String JavaDoc name = null, value = null;
84        int len;
85       while ( ( len = is.readLine( lbuf ) ) != -1 ) {
86          if ( lbuf[ --len ] != '\n' ) {
87               throw new IOException JavaDoc( "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             // continuation of previous line
98
if ( name == null ) {
99                  throw new IOException JavaDoc( "misplaced continuation line" );
100             }
101               value = value + new String JavaDoc( lbuf, 0, 1, len - 1 );
102          }
103          else {
104             while ( lbuf[ i++ ] != ':' ) {
105                if ( i >= len ) {
106                   throw new IOException JavaDoc("invalid header field");
107                }
108             }
109             if ( lbuf[ i++ ] != ' ' ) {
110                throw new IOException JavaDoc("invalid header field");
111             }
112             name = new String JavaDoc(lbuf, 0, 0, i - 2);
113             value = new String JavaDoc(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 JavaDoc e ) {
121             throw new IOException JavaDoc( "invalid header field name: " + name );
122          }
123       }
124    }
125
126
127    static class FastInputStream extends FilterInputStream JavaDoc {
128
129       private byte buf[];
130     private int count = 0;
131        private int pos = 0;
132
133     FastInputStream( InputStream JavaDoc in ) {
134          this( in, 8192 );
135     }
136
137        FastInputStream( InputStream JavaDoc in, int size ) {
138          super( in );
139          buf = new byte[ size ];
140        }
141
142     public int read() throws IOException JavaDoc {
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 JavaDoc {
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     /*
173         * Reads 'len' bytes from the input stream, or until an end-of-line
174      * is reached. Returns the number of bytes read.
175         */

176     public int readLine( byte[] b, int off, int len ) throws IOException JavaDoc {
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 JavaDoc {
208          if ( pos == count ) {
209            fill();
210          }
211          return buf[ pos ];
212        }
213
214     public int readLine( byte[] b ) throws IOException JavaDoc {
215          return readLine( b, 0, b.length );
216     }
217
218        public long skip( long n ) throws IOException JavaDoc {
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 JavaDoc {
234          return ( count - pos ) + in.available();
235        }
236
237     public void close() throws IOException JavaDoc {
238          if ( in != null ) {
239            in.close();
240             in = null;
241            buf = null;
242          }
243        }
244
245     private void fill() throws IOException JavaDoc {
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