src/share/classes/sun/tools/jar/Manifest.java

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jar;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import java.security.*;
  31 
  32 import sun.net.www.MessageHeader;
  33 import sun.misc.BASE64Encoder;
  34 import sun.misc.BASE64Decoder;
  35 
  36 /**
  37  * This is OBSOLETE. DO NOT USE THIS. Use java.util.jar.Manifest
  38  * instead. It has to stay here because some apps (namely HJ and HJV)
  39  * call directly into it.
  40  *
  41  * @author David Brown
  42  * @author Benjamin Renaud
  43  */
  44 
  45 public class Manifest {
  46 
  47     /* list of headers that all pertain to a particular
  48      * file in the archive
  49      */
  50     private Vector<MessageHeader> entries = new Vector<>();
  51     private byte[] tmpbuf = new byte[512];
  52     /* a hashtable of entries, for fast lookup */
  53     private Hashtable<String, MessageHeader> tableEntries = new Hashtable<>();
  54 


 161             name = name.substring(1);
 162         return name;
 163     }
 164 
 165     public void addFile(File f) throws IOException {
 166         String stdName = localToStd(f.getPath());
 167         if (tableEntries.get(stdName) == null) {
 168             MessageHeader mh = new MessageHeader();
 169             mh.add("Name", stdName);
 170             addEntry(mh);
 171         }
 172     }
 173 
 174     public void doHashes(MessageHeader mh) throws IOException {
 175         // If unnamed or is a directory return immediately
 176         String name = mh.findValue("Name");
 177         if (name == null || name.endsWith("/")) {
 178             return;
 179         }
 180 
 181         BASE64Encoder enc = new BASE64Encoder();
 182 
 183         /* compute hashes, write over any other "Hash-Algorithms" (?) */
 184         for (int j = 0; j < hashes.length; ++j) {
 185             InputStream is = new FileInputStream(stdToLocal(name));
 186             try {
 187                 MessageDigest dig = MessageDigest.getInstance(hashes[j]);
 188 
 189                 int len;
 190                 while ((len = is.read(tmpbuf, 0, tmpbuf.length)) != -1) {
 191                     dig.update(tmpbuf, 0, len);
 192                 }
 193                 mh.set(hashes[j] + "-Digest", enc.encode(dig.digest()));
 194             } catch (NoSuchAlgorithmException e) {
 195                 throw new JarException("Digest algorithm " + hashes[j] +
 196                                        " not available.");
 197             } finally {
 198                 is.close();
 199             }
 200         }
 201     }
 202 
 203     /* Add a manifest file at current position in a stream
 204      */
 205     public void stream(OutputStream os) throws IOException {
 206 
 207         PrintStream ps;
 208         if (os instanceof PrintStream) {
 209             ps = (PrintStream) os;
 210         } else {
 211             ps = new PrintStream(os);
 212         }
 213 




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jar;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import java.security.*;
  31 
  32 import sun.net.www.MessageHeader;
  33 import java.util.Base64;

  34 
  35 /**
  36  * This is OBSOLETE. DO NOT USE THIS. Use java.util.jar.Manifest
  37  * instead. It has to stay here because some apps (namely HJ and HJV)
  38  * call directly into it.
  39  *
  40  * @author David Brown
  41  * @author Benjamin Renaud
  42  */
  43 
  44 public class Manifest {
  45 
  46     /* list of headers that all pertain to a particular
  47      * file in the archive
  48      */
  49     private Vector<MessageHeader> entries = new Vector<>();
  50     private byte[] tmpbuf = new byte[512];
  51     /* a hashtable of entries, for fast lookup */
  52     private Hashtable<String, MessageHeader> tableEntries = new Hashtable<>();
  53 


 160             name = name.substring(1);
 161         return name;
 162     }
 163 
 164     public void addFile(File f) throws IOException {
 165         String stdName = localToStd(f.getPath());
 166         if (tableEntries.get(stdName) == null) {
 167             MessageHeader mh = new MessageHeader();
 168             mh.add("Name", stdName);
 169             addEntry(mh);
 170         }
 171     }
 172 
 173     public void doHashes(MessageHeader mh) throws IOException {
 174         // If unnamed or is a directory return immediately
 175         String name = mh.findValue("Name");
 176         if (name == null || name.endsWith("/")) {
 177             return;
 178         }
 179 

 180 
 181         /* compute hashes, write over any other "Hash-Algorithms" (?) */
 182         for (int j = 0; j < hashes.length; ++j) {
 183             InputStream is = new FileInputStream(stdToLocal(name));
 184             try {
 185                 MessageDigest dig = MessageDigest.getInstance(hashes[j]);
 186 
 187                 int len;
 188                 while ((len = is.read(tmpbuf, 0, tmpbuf.length)) != -1) {
 189                     dig.update(tmpbuf, 0, len);
 190                 }
 191                 mh.set(hashes[j] + "-Digest", Base64.getMimeEncoder().encodeToString(dig.digest()));
 192             } catch (NoSuchAlgorithmException e) {
 193                 throw new JarException("Digest algorithm " + hashes[j] +
 194                                        " not available.");
 195             } finally {
 196                 is.close();
 197             }
 198         }
 199     }
 200 
 201     /* Add a manifest file at current position in a stream
 202      */
 203     public void stream(OutputStream os) throws IOException {
 204 
 205         PrintStream ps;
 206         if (os instanceof PrintStream) {
 207             ps = (PrintStream) os;
 208         } else {
 209             ps = new PrintStream(os);
 210         }
 211