src/jdk.packager/share/classes/jdk/packager/internal/IOUtils.java
author herrick
Mon, 27 Aug 2018 16:01:38 -0400
branchJDK-8200758-branch
changeset 56869 41e17fe9fbeb
parent 56821 565d54ca1f41
child 56982 e094d5483bd6
permissions -rw-r--r--
8208522: removed unreferenced class and minor formatting of java source Reviewed-by: almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     1
/*
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     2
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     4
 *
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    10
 *
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    15
 * accompanied this code).
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    16
 *
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    20
 *
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    23
 * questions.
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    24
 */
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    25
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    26
package jdk.packager.internal;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    27
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    28
import java.io.*;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    29
import java.net.URL;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    30
import java.util.Arrays;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    31
import java.nio.channels.FileChannel;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    32
import java.nio.file.FileVisitResult;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    33
import java.nio.file.Files;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    34
import java.nio.file.Path;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    35
import java.nio.file.SimpleFileVisitor;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    36
import java.nio.file.attribute.BasicFileAttributes;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    37
import java.util.ArrayList;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    38
import java.util.List;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    39
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    40
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    41
public class IOUtils {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    42
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    43
    public static void deleteRecursive(File path) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    44
        if (!path.exists()) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    45
            return;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    46
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    47
        Path directory = path.toPath();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    48
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    49
            @Override
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    50
            public FileVisitResult visitFile(Path file,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    51
                            BasicFileAttributes attr) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    52
                if (Platform.getPlatform() == Platform.WINDOWS) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    53
                    Files.setAttribute(file, "dos:readonly", false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    54
                }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    55
                Files.delete(file);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    56
                return FileVisitResult.CONTINUE;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    57
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    58
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    59
            @Override
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    60
            public FileVisitResult preVisitDirectory(Path dir,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    61
                            BasicFileAttributes attr) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    62
                if (Platform.getPlatform() == Platform.WINDOWS) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    63
                    Files.setAttribute(dir, "dos:readonly", false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    64
                }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    65
                return FileVisitResult.CONTINUE;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    66
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    67
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    68
            @Override
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    69
            public FileVisitResult postVisitDirectory(Path dir, IOException e)
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    70
                            throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    71
                Files.delete(dir);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    72
                return FileVisitResult.CONTINUE;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    73
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    74
        });
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    75
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    76
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    77
    public static void copyRecursive(Path src, Path dest) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    78
        Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    79
            @Override
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    80
            public FileVisitResult preVisitDirectory(final Path dir,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    81
                    final BasicFileAttributes attrs) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    82
                Files.createDirectories(dest.resolve(src.relativize(dir)));
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    83
                return FileVisitResult.CONTINUE;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    84
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    85
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    86
            @Override
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    87
            public FileVisitResult visitFile(final Path file,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    88
                    final BasicFileAttributes attrs) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    89
                Files.copy(file, dest.resolve(src.relativize(file)));
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    90
                return FileVisitResult.CONTINUE;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    91
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    92
        });
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    93
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    94
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    95
    public static void copyFromURL(URL location, File file) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    96
        copyFromURL(location, file, false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    97
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
    98
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
    99
    public static void copyFromURL(URL location, File file, boolean append)
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   100
            throws IOException {
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   101
        if (location == null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   102
            throw new IOException("Missing input resource!");
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   103
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   104
        if (file.exists() && !append) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   105
           file.delete();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   106
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   107
        InputStream in = location.openStream();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   108
        FileOutputStream out = new FileOutputStream(file, append);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   109
        byte[] buffer = new byte[1024];
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   110
        int len;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   111
        while ((len = in.read(buffer)) != -1) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   112
            out.write(buffer, 0, len);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   113
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   114
        out.close();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   115
        in.close();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   116
        file.setReadOnly();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   117
        file.setReadable(true, false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   118
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   119
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   120
    public static void copyFile(File sourceFile, File destFile)
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   121
            throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   122
        destFile.getParentFile().mkdirs();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   123
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   124
        //recreate the file as existing copy may have weird permissions
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   125
        destFile.delete();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   126
        destFile.createNewFile();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   127
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   128
        FileChannel source = null;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   129
        FileChannel destination = null;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   130
        source = new FileInputStream(sourceFile).getChannel();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   131
        destination = new FileOutputStream(destFile).getChannel();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   132
        if (destination != null && source != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   133
            destination.transferFrom(source, 0, source.size());
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   134
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   135
        if (source != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   136
            source.close();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   137
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   138
        if (destination != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   139
            destination.close();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   140
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   141
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   142
        //preserve executable bit!
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   143
        if (sourceFile.canExecute()) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   144
            destFile.setExecutable(true, false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   145
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   146
        if (!sourceFile.canWrite()) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   147
            destFile.setReadOnly();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   148
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   149
        destFile.setReadable(true, false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   150
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   151
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   152
    public static long getFolderSize(File folder) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   153
        long foldersize = 0;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   154
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   155
        File[] children = folder.listFiles();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   156
        if (children != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   157
            for (File f : children) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   158
                if (f.isDirectory()) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   159
                    foldersize += getFolderSize(f);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   160
                } else {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   161
                    foldersize += f.length();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   162
                }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   163
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   164
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   165
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   166
        return foldersize;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   167
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   168
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   169
    //run "launcher paramfile" in the directory where paramfile is kept
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   170
    public static void run(String launcher, File paramFile, boolean verbose)
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   171
            throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   172
        if (paramFile != null && paramFile.exists()) {
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   173
            ProcessBuilder pb =
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   174
                    new ProcessBuilder(launcher, paramFile.getName());
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   175
            pb = pb.directory(paramFile.getParentFile());
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   176
            exec(pb, verbose);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   177
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   178
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   179
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   180
    public static void exec(ProcessBuilder pb, boolean verbose)
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   181
            throws IOException {
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   182
        exec(pb, verbose, false);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   183
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   184
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   185
    public static void exec(ProcessBuilder pb, boolean verbose,
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   186
            boolean testForPresenseOnly) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   187
        exec(pb, verbose, testForPresenseOnly, null);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   188
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   189
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   190
    public static void exec(ProcessBuilder pb, boolean verbose,
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   191
            boolean testForPresenseOnly, PrintStream consumer)
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   192
            throws IOException {
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   193
        pb.redirectErrorStream(true);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   194
        String prefix = Arguments.echoMode() ? "\nECHO-MODE: " : "";
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   195
        Log.verbose(prefix + "Running "
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   196
                + Arrays.toString(pb.command().toArray(new String[0]))
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   197
                + (pb.directory() != null ? (" in " + pb.directory()) : ""));
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   198
        Process p = pb.start();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   199
        InputStreamReader isr = new InputStreamReader(p.getInputStream());
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   200
        BufferedReader br = new BufferedReader(isr);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   201
        String lineRead;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   202
        while ((lineRead = br.readLine()) != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   203
            if (consumer != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   204
                consumer.print(lineRead + '\n');
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   205
            } else if (verbose) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   206
               Log.info(lineRead);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   207
            } else {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   208
               Log.debug(lineRead);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   209
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   210
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   211
        try {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   212
            int ret = p.waitFor();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   213
            if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   214
                throw new IOException("Exec failed with code "
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   215
                        + ret + " command ["
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   216
                        + Arrays.toString(pb.command().toArray(new String[0]))
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   217
                        + " in " + (pb.directory() != null ?
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   218
                                pb.directory().getAbsolutePath() :
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   219
                                "unspecified directory"));
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   220
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   221
        } catch (InterruptedException ex) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   222
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   223
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   224
        if (Arguments.echoMode()) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   225
             Log.verbose("\n");
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   226
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   227
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   228
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   229
    @SuppressWarnings("unchecked")
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   230
    private static Process startProcess(Object... args) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   231
        final ArrayList<String> argsList = new ArrayList<>();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   232
        for (Object a : args) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   233
            if (a instanceof List) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   234
                argsList.addAll((List)a);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   235
            } else if (a instanceof String) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   236
                argsList.add((String)a);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   237
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   238
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   239
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   240
        return Runtime.getRuntime().exec(
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   241
                argsList.toArray(new String[argsList.size()]));
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   242
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   243
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   244
    private static void logErrorStream(Process p) {
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   245
        final BufferedReader err =
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   246
                new BufferedReader(new InputStreamReader(p.getErrorStream()));
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   247
        Thread t = new Thread(() -> {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   248
            try {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   249
                String line;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   250
                while ((line = err.readLine()) != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   251
                    Log.error(line);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   252
                }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   253
            } catch (IOException ioe) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   254
                Log.verbose(ioe);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   255
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   256
        });
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   257
        t.setDaemon(true);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   258
        t.start();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   259
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   260
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   261
    public static int getProcessOutput(List<String> result, Object... args)
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   262
            throws IOException, InterruptedException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   263
        final Process p = startProcess(args);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   264
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   265
        List<String> list = new ArrayList<>();
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   266
        final BufferedReader in =
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   267
                new BufferedReader(new InputStreamReader(p.getInputStream()));
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   268
        Thread t = new Thread(() -> {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   269
            try {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   270
                String line;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   271
                while ((line = in.readLine()) != null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   272
                    list.add(line);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   273
                }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   274
            } catch (IOException ioe) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   275
                jdk.packager.internal.Log.verbose(ioe);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   276
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   277
        });
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   278
        t.setDaemon(true);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   279
        t.start();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   280
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   281
        logErrorStream(p);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   282
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   283
        int ret = p.waitFor();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   284
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   285
        result.clear();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   286
        result.addAll(list);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   287
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   288
        return ret;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   289
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   290
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   291
    // no good test if we are running pre-JRE7
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   292
    // use heuristic approach
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   293
    // "false positive" is better than wrong answer
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   294
    public static boolean isNotSymbolicLink(File file) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   295
        //no symlinks on windows
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   296
        if (Platform.getPlatform() == Platform.WINDOWS) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   297
            return true;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   298
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   299
        try {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   300
            if (file == null || file.getParent() == null) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   301
                return false;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   302
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   303
            File file_canonical = new File(
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   304
                    file.getParentFile().getCanonicalFile(), file.getName());
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   305
            if (file_canonical.getCanonicalFile().equals(
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   306
                       file_canonical.getAbsoluteFile())) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   307
                return true;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   308
            }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   309
        } catch (IOException ioe) {}
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   310
        return false;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   311
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   312
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   313
    public static byte[] readFully(File f) throws IOException {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   314
        InputStream inp = new FileInputStream(f);
56869
41e17fe9fbeb 8208522: removed unreferenced class and minor formatting of java source
herrick
parents: 56821
diff changeset
   315
        // read fully into memory
56821
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   316
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   317
        byte[] buffer = new byte[1024];
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   318
        int length;
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   319
        while ((length = inp.read(buffer)) != -1) {
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   320
            baos.write(buffer, 0, length);
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   321
        }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   322
        baos.close();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   323
        return baos.toByteArray();
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   324
    }
565d54ca1f41 8206005: Move prototype jpackager from closed sandbox to open sandbox
herrick
parents:
diff changeset
   325
}