jdk/test/java/security/testlibrary/CertUtils.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertPathBuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertPathValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CollectionCertStoreParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.PKIXBuilderParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.PKIXCertPathBuilderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.PKIXCertPathValidatorResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.PKIXParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Static utility methods useful for testing certificate/certpath APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class CertUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private CertUtils() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Get a DER-encoded X.509 certificate from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param certFilePath path to file containing DER-encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @return X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static X509Certificate getCertFromFile(String certFilePath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                File certFile = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    certFilePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (!certFile.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    throw new IOException("File " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                          certFile.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                          " is not a readable file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                FileInputStream certFileInputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    new FileInputStream(certFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                cert = (X509Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    cf.generateCertificate(certFileInputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new IOException("Can't construct X509Certificate: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                      e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Get a DER-encoded X.509 CRL from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param crlFilePath path to file containing DER-encoded CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return X509CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static X509CRL getCRLFromFile(String crlFilePath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            X509CRL crl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                File crlFile = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    crlFilePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (!crlFile.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new IOException("File " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                          crlFile.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                          " is not a readable file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                FileInputStream crlFileInputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    new FileInputStream(crlFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                crl = (X509CRL) cf.generateCRL(crlFileInputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                throw new IOException("Can't construct X509CRL: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                      e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return crl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Read a bunch of certs from files and create a CertPath from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static CertPath buildPath(String [] fileNames) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return buildPath("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Read a bunch of certs from files and create a CertPath from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param relPath relative path containing certs (must end in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *    file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static CertPath buildPath(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        List<X509Certificate> list = new ArrayList<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            list.add(0, getCertFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return(cf.generateCertPath(list));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Read a bunch of certs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static CertStore createStore(String [] fileNames) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return createStore("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Read a bunch of certs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param relPath relative path containing certs (must end in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *    file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static CertStore createStore(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Set<X509Certificate> certs = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            certs.add(getCertFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            new CollectionCertStoreParameters(certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Read a bunch of CRLs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static CertStore createCRLStore(String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return createCRLStore("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Read a bunch of CRLs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param relPath relative path containing CRLs (must end in file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static CertStore createCRLStore(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Set<X509CRL> crls = new HashSet<X509CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            crls.add(getCRLFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            new CollectionCertStoreParameters(crls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Perform a PKIX path build. On failure, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param params PKIXBuilderParameters to use in validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static PKIXCertPathBuilderResult build(PKIXBuilderParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        CertPathBuilder builder =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            CertPathBuilder.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return (PKIXCertPathBuilderResult) builder.build(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Perform a PKIX validation. On failure, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param path CertPath to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param params PKIXParameters to use in validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static PKIXCertPathValidatorResult validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        (CertPath path, PKIXParameters params) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        CertPathValidator validator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            CertPathValidator.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return (PKIXCertPathValidatorResult) validator.validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Reads the entire input stream into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private static byte[] getTotalBytes(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
           byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}