From a4be70fbf09b8f00cccb697fd031e9bdd277c89a Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Sun, 16 Feb 2003 02:03:06 +0000 Subject: [PATCH] Read/Write manifests in UTF-8 PR: 17075 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274074 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Jar.java | 28 +++++++++++-------- .../tools/ant/taskdefs/ManifestTask.java | 24 ++++++++++------ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 6d785048e..664185680 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -58,7 +58,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -204,7 +204,7 @@ public class Jar extends Zip { * or the name of a jar added through a fileset. If its the name of an added * jar, the task expects the manifest to be in the jar at META-INF/MANIFEST.MF. * - * @param manifestFile + * @param manifestFile the manifest file to use. */ public void setManifest(File manifestFile) { if (!manifestFile.exists()) { @@ -218,18 +218,20 @@ public class Jar extends Zip { private Manifest getManifest(File manifestFile) { Manifest newManifest = null; - Reader r = null; + FileInputStream fis = null; + InputStreamReader isr = null; try { - r = new FileReader(manifestFile); - newManifest = getManifest(r); + fis = new FileInputStream(manifestFile); + isr = new InputStreamReader(fis, "UTF-8"); + newManifest = getManifest(isr); } catch (IOException e) { throw new BuildException("Unable to read manifest file: " + manifestFile + " (" + e.getMessage() + ")", e); } finally { - if (r != null) { + if (isr != null) { try { - r.close(); + isr.close(); } catch (IOException e) { // do nothing } @@ -254,8 +256,9 @@ public class Jar extends Zip { while (enum.hasMoreElements()) { ZipEntry ze = (ZipEntry) enum.nextElement(); if (ze.getName().equalsIgnoreCase(MANIFEST_NAME)) { - return getManifest(new InputStreamReader(zf - .getInputStream(ze))); + InputStreamReader isr = + new InputStreamReader(zf.getInputStream(ze), "UTF-8"); + return getManifest(isr); } } return null; @@ -467,13 +470,13 @@ public class Jar extends Zip { } } - private void filesetManifest(File file, InputStream is) { + private void filesetManifest(File file, InputStream is) throws IOException { if (manifestFile != null && manifestFile.equals(file)) { // If this is the same name specified in 'manifest', this // is the manifest to use log("Found manifest " + file, Project.MSG_VERBOSE); if (is != null) { - manifest = getManifest(new InputStreamReader(is)); + manifest = getManifest(new InputStreamReader(is, "UTF-8")); } else { manifest = getManifest(file); } @@ -486,7 +489,8 @@ public class Jar extends Zip { try { Manifest newManifest = null; if (is != null) { - newManifest = getManifest(new InputStreamReader(is)); + newManifest + = getManifest(new InputStreamReader(is, "UTF-8")); } else { newManifest = getManifest(file); } diff --git a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java index 36e5198b6..06332838c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -55,8 +55,10 @@ package org.apache.tools.ant.taskdefs; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; import java.io.IOException; import java.io.PrintWriter; import org.apache.tools.ant.BuildException; @@ -169,10 +171,12 @@ public class ManifestTask extends Task { BuildException error = null; if (manifestFile.exists()) { - FileReader f = null; + FileInputStream fis = null; + InputStreamReader isr = null; try { - f = new FileReader(manifestFile); - current = new Manifest(f); + fis = new FileInputStream(manifestFile); + isr = new InputStreamReader(fis, "UTF-8"); + current = new Manifest(isr); } catch (ManifestException m) { error = new BuildException("Existing manifest " + manifestFile + " is invalid", m, getLocation()); @@ -180,9 +184,9 @@ public class ManifestTask extends Task { error = new BuildException("Failed to read " + manifestFile, e, getLocation()); } finally { - if (f != null) { + if (isr != null) { try { - f.close(); + isr.close(); } catch (IOException e) {} } } @@ -210,7 +214,9 @@ public class ManifestTask extends Task { PrintWriter w = null; try { - w = new PrintWriter(new FileWriter(manifestFile)); + FileOutputStream fos = new FileOutputStream(manifestFile); + OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); + w = new PrintWriter(osw); toWrite.write(w); } catch (IOException e) { throw new BuildException("Failed to write " + manifestFile,