jdk/src/share/classes/sun/misc/IOUtils.java
author alanb
Fri, 22 Feb 2013 14:04:06 +0000
changeset 16023 58ecc1b8327b
parent 5506 202f599c92aa
permissions -rw-r--r--
8008290: (profiles) Startup regression due to additional checking of JAR file manifests Reviewed-by: lancea, chegar, iris, mchung, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     4
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    10
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    15
 * accompanied this code).
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    16
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    23
 * questions.
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    24
 */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    25
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    26
/**
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    27
 * IOUtils: A collection of IO-related public static methods.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    28
 */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    29
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    30
package sun.misc;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    31
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    32
import java.io.EOFException;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    33
import java.io.IOException;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    34
import java.io.InputStream;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    35
import java.util.Arrays;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    36
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    37
public class IOUtils {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    38
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    39
    /**
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    40
     * Read up to <code>length</code> of bytes from <code>in</code>
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    41
     * until EOF is detected.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    42
     * @param in input stream, must not be null
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    43
     * @param length number of bytes to read, -1 or Integer.MAX_VALUE means
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    44
     *        read as much as possible
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    45
     * @param readAll if true, an EOFException will be thrown if not enough
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    46
     *        bytes are read. Ignored when length is -1 or Integer.MAX_VALUE
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    47
     * @return bytes read
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    48
     * @throws IOException Any IO error or a premature EOF is detected
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    49
     */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    50
    public static byte[] readFully(InputStream is, int length, boolean readAll)
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    51
            throws IOException {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    52
        byte[] output = {};
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    53
        if (length == -1) length = Integer.MAX_VALUE;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    54
        int pos = 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    55
        while (pos < length) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    56
            int bytesToRead;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    57
            if (pos >= output.length) { // Only expand when there's no room
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    58
                bytesToRead = Math.min(length - pos, output.length + 1024);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    59
                if (output.length < pos + bytesToRead) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    60
                    output = Arrays.copyOf(output, pos + bytesToRead);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    61
                }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    62
            } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    63
                bytesToRead = output.length - pos;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    64
            }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    65
            int cc = is.read(output, pos, bytesToRead);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    66
            if (cc < 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    67
                if (readAll && length != Integer.MAX_VALUE) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    68
                    throw new EOFException("Detect premature EOF");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    69
                } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    70
                    if (output.length != pos) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    71
                        output = Arrays.copyOf(output, pos);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    72
                    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    73
                    break;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    74
                }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    75
            }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    76
            pos += cc;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    77
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    78
        return output;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    79
    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    80
}