178 if (result != null && jv != null && ! jv.isTrustedManifestEntry(name)) {
179 throw new SecurityException("Untrusted manifest entry: " + name);
180 }
181 return result;
182 }
183
184 /**
185 * Clears the main Attributes as well as the entries in this Manifest.
186 */
187 public void clear() {
188 attr.clear();
189 entries.clear();
190 }
191
192 /**
193 * Writes the Manifest to the specified OutputStream.
194 * Attributes.Name.MANIFEST_VERSION must be set in
195 * MainAttributes prior to invoking this method.
196 *
197 * @param out the output stream
198 * @exception IOException if an I/O error has occurred
199 * @see #getMainAttributes
200 */
201 public void write(OutputStream out) throws IOException {
202 DataOutputStream dos = new DataOutputStream(out);
203 // Write out the main attributes for the manifest
204 attr.writeMain(dos);
205 // Now write out the per-entry attributes
206 StringBuilder buffer = entries.isEmpty() ? null : new StringBuilder(72);
207 for (Map.Entry<String, Attributes> e : entries.entrySet()) {
208 buffer.setLength(0);
209 buffer.append("Name: ");
210 buffer.append(e.getKey());
211 println72(dos, buffer.toString());
212 e.getValue().write(dos);
213 }
214 dos.flush();
215 }
216
217 /**
218 * Adds line breaks to enforce a maximum of 72 bytes per line.
259 */
260 static void println(OutputStream out) throws IOException {
261 out.write('\r');
262 out.write('\n');
263 }
264
265 static String getErrorPosition(String filename, final int lineNumber) {
266 if (filename == null ||
267 !SecurityProperties.INCLUDE_JAR_NAME_IN_EXCEPTIONS) {
268 return "line " + lineNumber;
269 }
270 return "manifest of " + filename + ":" + lineNumber;
271 }
272
273 /**
274 * Reads the Manifest from the specified InputStream. The entry
275 * names and attributes read will be merged in with the current
276 * manifest entries.
277 *
278 * @param is the input stream
279 * @exception IOException if an I/O error has occurred
280 */
281 public void read(InputStream is) throws IOException {
282 read(is, null);
283 }
284
285 private void read(InputStream is, String jarFilename) throws IOException {
286 // Buffered input stream for reading manifest data
287 FastInputStream fis = new FastInputStream(is);
288 // Line buffer
289 byte[] lbuf = new byte[512];
290 // Read the main attributes for the manifest
291 int lineNumber = attr.read(fis, lbuf, jarFilename, 0);
292 // Total number of entries, attributes read
293 int ecount = 0, acount = 0;
294 // Average size of entry attributes
295 int asize = 2;
296 // Now parse the manifest entries
297 int len;
298 String name = null;
299 boolean skipEmptyLines = true;
|
178 if (result != null && jv != null && ! jv.isTrustedManifestEntry(name)) {
179 throw new SecurityException("Untrusted manifest entry: " + name);
180 }
181 return result;
182 }
183
184 /**
185 * Clears the main Attributes as well as the entries in this Manifest.
186 */
187 public void clear() {
188 attr.clear();
189 entries.clear();
190 }
191
192 /**
193 * Writes the Manifest to the specified OutputStream.
194 * Attributes.Name.MANIFEST_VERSION must be set in
195 * MainAttributes prior to invoking this method.
196 *
197 * @param out the output stream
198 * @throws IOException if an I/O error has occurred
199 * @see #getMainAttributes
200 */
201 public void write(OutputStream out) throws IOException {
202 DataOutputStream dos = new DataOutputStream(out);
203 // Write out the main attributes for the manifest
204 attr.writeMain(dos);
205 // Now write out the per-entry attributes
206 StringBuilder buffer = entries.isEmpty() ? null : new StringBuilder(72);
207 for (Map.Entry<String, Attributes> e : entries.entrySet()) {
208 buffer.setLength(0);
209 buffer.append("Name: ");
210 buffer.append(e.getKey());
211 println72(dos, buffer.toString());
212 e.getValue().write(dos);
213 }
214 dos.flush();
215 }
216
217 /**
218 * Adds line breaks to enforce a maximum of 72 bytes per line.
259 */
260 static void println(OutputStream out) throws IOException {
261 out.write('\r');
262 out.write('\n');
263 }
264
265 static String getErrorPosition(String filename, final int lineNumber) {
266 if (filename == null ||
267 !SecurityProperties.INCLUDE_JAR_NAME_IN_EXCEPTIONS) {
268 return "line " + lineNumber;
269 }
270 return "manifest of " + filename + ":" + lineNumber;
271 }
272
273 /**
274 * Reads the Manifest from the specified InputStream. The entry
275 * names and attributes read will be merged in with the current
276 * manifest entries.
277 *
278 * @param is the input stream
279 * @throws IOException if an I/O error has occurred
280 */
281 public void read(InputStream is) throws IOException {
282 read(is, null);
283 }
284
285 private void read(InputStream is, String jarFilename) throws IOException {
286 // Buffered input stream for reading manifest data
287 FastInputStream fis = new FastInputStream(is);
288 // Line buffer
289 byte[] lbuf = new byte[512];
290 // Read the main attributes for the manifest
291 int lineNumber = attr.read(fis, lbuf, jarFilename, 0);
292 // Total number of entries, attributes read
293 int ecount = 0, acount = 0;
294 // Average size of entry attributes
295 int asize = 2;
296 // Now parse the manifest entries
297 int len;
298 String name = null;
299 boolean skipEmptyLines = true;
|