jdk/src/java.base/share/classes/sun/misc/Resource.java
author ihse
Fri, 12 Jun 2015 08:31:01 +0200
changeset 33957 39113ae98993
parent 30655 d83f50188ca9
permissions -rw-r--r--
8085822: JEP 223: New Version-String Scheme (initial integration) Reviewed-by: erikj, dcubed, dholmes, alanb Contributed-by: Magnus Ihse Bursie <magnus.ihse.bursie@oracle.com>, Alejandro E Murillo <alejandro.murillo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     2
 * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
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.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
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.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
    28
import java.io.EOFException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.ByteBuffer;
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
    36
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.nio.ByteBuffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class is used to represent a Resource that has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * from the class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public abstract class Resource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Returns the name of the Resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public abstract String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Returns the URL of the Resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public abstract URL getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Returns the CodeSource URL for the Resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public abstract URL getCodeSourceURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Returns an InputStream for reading the Resource data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public abstract InputStream getInputStream() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Returns the length of the Resource data, or -1 if unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public abstract int getContentLength() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private InputStream cis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* Cache result in case getBytes is called after getByteBuffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private synchronized InputStream cachedInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (cis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            cis = getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return cis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the Resource data as an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public byte[] getBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Get stream before content length so that a FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // can propagate upwards without being caught too early
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        InputStream in = cachedInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // This code has been uglified to protect against interrupts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // Even if a thread has been interrupted when loading resources,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // the IO should not abort, so must carefully retry, failing only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // if the retry leads to some other IO exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        boolean isInterrupted = Thread.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                len = getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch (InterruptedIOException iioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                Thread.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                isInterrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   109
            b = new byte[0];
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   110
            if (len == -1) len = Integer.MAX_VALUE;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   111
            int pos = 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   112
            while (pos < len) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   113
                int bytesToRead;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   114
                if (pos >= b.length) { // Only expand when there's no room
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   115
                    bytesToRead = Math.min(len - pos, b.length + 1024);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   116
                    if (b.length < pos + bytesToRead) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   117
                        b = Arrays.copyOf(b, pos + bytesToRead);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   119
                } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   120
                    bytesToRead = b.length - pos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   122
                int cc = 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   123
                try {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   124
                    cc = in.read(b, pos, bytesToRead);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   125
                } catch (InterruptedIOException iioe) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   126
                    Thread.interrupted();
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   127
                    isInterrupted = true;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   128
                }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   129
                if (cc < 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   130
                    if (len != Integer.MAX_VALUE) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   131
                        throw new EOFException("Detect premature EOF");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   132
                    } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   133
                        if (b.length != pos) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   134
                            b = Arrays.copyOf(b, pos);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   135
                        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   136
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   139
                pos += cc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } catch (InterruptedIOException iioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                isInterrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (IOException ignore) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (isInterrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the Resource data as a ByteBuffer, but only if the input stream
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   157
     * was implemented on top of a ByteBuffer. Return {@code null} otherwise.
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   158
     * @return Resource data or null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public ByteBuffer getByteBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        InputStream in = cachedInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (in instanceof ByteBuffered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return ((ByteBuffered)in).getByteBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the Manifest for the Resource, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Manifest getManifest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns theCertificates for the Resource, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public java.security.cert.Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns the code signers for the Resource, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public CodeSigner[] getCodeSigners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}