Path: blob/master/src/java.base/share/classes/jdk/internal/loader/Resource.java
41159 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.loader;2627import java.io.EOFException;28import java.net.URL;29import java.io.IOException;30import java.io.InterruptedIOException;31import java.io.InputStream;32import java.security.CodeSigner;33import java.util.jar.Manifest;34import java.nio.ByteBuffer;35import java.util.Arrays;36import sun.nio.ByteBuffered;3738/**39* This class is used to represent a Resource that has been loaded40* from the class path.41*42* @author David Connelly43* @since 1.244*/45public abstract class Resource {46/**47* Returns the name of the Resource.48*/49public abstract String getName();5051/**52* Returns the URL of the Resource.53*/54public abstract URL getURL();5556/**57* Returns the CodeSource URL for the Resource.58*/59public abstract URL getCodeSourceURL();6061/**62* Returns an InputStream for reading the Resource data.63*/64public abstract InputStream getInputStream() throws IOException;6566/**67* Returns the length of the Resource data, or -1 if unknown.68*/69public abstract int getContentLength() throws IOException;7071private InputStream cis;7273/* Cache result in case getBytes is called after getByteBuffer. */74private synchronized InputStream cachedInputStream() throws IOException {75if (cis == null) {76cis = getInputStream();77}78return cis;79}8081/**82* Returns the Resource data as an array of bytes.83*/84public byte[] getBytes() throws IOException {85byte[] b;86// Get stream before content length so that a FileNotFoundException87// can propagate upwards without being caught too early88InputStream in = cachedInputStream();8990// This code has been uglified to protect against interrupts.91// Even if a thread has been interrupted when loading resources,92// the IO should not abort, so must carefully retry, failing only93// if the retry leads to some other IO exception.9495boolean isInterrupted = Thread.interrupted();96int len;97for (;;) {98try {99len = getContentLength();100break;101} catch (InterruptedIOException iioe) {102Thread.interrupted();103isInterrupted = true;104}105}106107try {108b = new byte[0];109if (len == -1) len = Integer.MAX_VALUE;110int pos = 0;111while (pos < len) {112int bytesToRead;113if (pos >= b.length) { // Only expand when there's no room114bytesToRead = Math.min(len - pos, b.length + 1024);115if (bytesToRead < 0) {116// Can overflow only due to large b.length117bytesToRead = len - pos;118}119b = Arrays.copyOf(b, pos + bytesToRead);120} else {121bytesToRead = b.length - pos;122}123int cc = 0;124try {125cc = in.read(b, pos, bytesToRead);126} catch (InterruptedIOException iioe) {127Thread.interrupted();128isInterrupted = true;129}130if (cc < 0) {131if (len != Integer.MAX_VALUE) {132throw new EOFException("Detect premature EOF");133} else {134if (b.length != pos) {135b = Arrays.copyOf(b, pos);136}137break;138}139}140pos += cc;141}142} finally {143try {144in.close();145} catch (InterruptedIOException iioe) {146isInterrupted = true;147} catch (IOException ignore) {}148149if (isInterrupted) {150Thread.currentThread().interrupt();151}152}153return b;154}155156/**157* Returns the Resource data as a ByteBuffer, but only if the input stream158* was implemented on top of a ByteBuffer. Return {@code null} otherwise.159* @return Resource data or null.160*/161public ByteBuffer getByteBuffer() throws IOException {162InputStream in = cachedInputStream();163if (in instanceof ByteBuffered) {164return ((ByteBuffered)in).getByteBuffer();165}166return null;167}168169/**170* Returns the Manifest for the Resource, or null if none.171*/172public Manifest getManifest() throws IOException {173return null;174}175176/**177* Returns theCertificates for the Resource, or null if none.178*/179public java.security.cert.Certificate[] getCertificates() {180return null;181}182183/**184* Returns the code signers for the Resource, or null if none.185*/186public CodeSigner[] getCodeSigners() {187return null;188}189}190191192