1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 com.sun.javafx.tools.ant;
  27 
  28 import com.sun.javafx.tools.packager.CommonParams;
  29 import com.sun.javafx.tools.packager.DeployParams;
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.security.cert.CertPath;
  34 import java.security.cert.CertificateEncodingException;
  35 import java.util.Base64;
  36 import java.util.Enumeration;
  37 import java.util.Iterator;
  38 import java.util.jar.JarEntry;
  39 import java.util.jar.JarFile;
  40 import org.apache.tools.ant.types.FileSet;
  41 import org.apache.tools.ant.types.resources.FileResource;
  42 
  43 public final class Utils {
  44     private Utils() {
  45     }
  46 
  47     public static void addResources(CommonParams commonParams,
  48                                     FileSet fileset) {
  49         for (final Iterator i = fileset.iterator(); i.hasNext();) {
  50             FileResource fr = (FileResource) i.next();
  51             commonParams.addResource(fr.getBaseDir(), fr.getFile());
  52         }
  53     }
  54 
  55     public static void addResources(DeployParams deployParams,
  56                                     FileSet fileset,
  57                                     String type) {
  58         for (final Iterator i = fileset.iterator(); i.hasNext();) {
  59             FileResource fr = (FileResource) i.next();
  60             deployParams.addResource(fr.getBaseDir(), fr.getFile(), type);
  61         }
  62     }
  63 
  64     public static void addResources(DeployParams deployParams,
  65                                     com.sun.javafx.tools.ant.FileSet fileset) {
  66         for (final Iterator i = fileset.iterator(); i.hasNext();) {
  67             FileResource fr = (FileResource) i.next();
  68             deployParams.addResource(fr.getBaseDir(), fr.getFile(),
  69                     fileset.getMode(), fileset.getTypeAsString(),
  70                     fileset.getOs(), fileset.getArch());
  71         }
  72     }
  73 
  74     public static void readFully(InputStream is) throws IOException {
  75         byte buf[] = new byte[10000];
  76         while (is.read(buf) != -1) {}
  77     }
  78 
  79     public static void readAllFully(JarFile jf) throws IOException {
  80         Enumeration<JarEntry> entries = jf.entries();
  81         while (entries.hasMoreElements()) {
  82             JarEntry je = entries.nextElement();
  83             readFully(jf.getInputStream(je));
  84         }
  85     }
  86 
  87     public static byte[] getBytes(InputStream is) throws IOException {
  88         byte[] buffer = new byte[8192];
  89         ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
  90         int n;
  91         while ((n = is.read(buffer, 0, buffer.length)) != -1) {
  92             baos.write(buffer, 0, n);
  93         }
  94         return baos.toByteArray();
  95     }
  96 
  97     public static String getBase64Encoded(CertPath certPath)
  98             throws CertificateEncodingException
  99     {
 100         return Base64.getEncoder().encodeToString(certPath.getEncoded());
 101     }
 102 }