0N/A/*
2362N/A * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
1822N/Aimport java.io.EOFException;
0N/Aimport java.net.URL;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InterruptedIOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.security.CodeSigner;
0N/Aimport java.util.jar.Manifest;
0N/Aimport java.nio.ByteBuffer;
1822N/Aimport java.util.Arrays;
0N/Aimport sun.nio.ByteBuffered;
0N/A
0N/A/**
0N/A * This class is used to represent a Resource that has been loaded
0N/A * from the class path.
0N/A *
0N/A * @author David Connelly
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class Resource {
0N/A /**
0N/A * Returns the name of the Resource.
0N/A */
0N/A public abstract String getName();
0N/A
0N/A /**
0N/A * Returns the URL of the Resource.
0N/A */
0N/A public abstract URL getURL();
0N/A
0N/A /**
0N/A * Returns the CodeSource URL for the Resource.
0N/A */
0N/A public abstract URL getCodeSourceURL();
0N/A
0N/A /**
0N/A * Returns an InputStream for reading the Resource data.
0N/A */
0N/A public abstract InputStream getInputStream() throws IOException;
0N/A
0N/A /**
0N/A * Returns the length of the Resource data, or -1 if unknown.
0N/A */
0N/A public abstract int getContentLength() throws IOException;
0N/A
0N/A private InputStream cis;
0N/A
0N/A /* Cache result in case getBytes is called after getByteBuffer. */
0N/A private synchronized InputStream cachedInputStream() throws IOException {
0N/A if (cis == null) {
0N/A cis = getInputStream();
0N/A }
0N/A return cis;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Resource data as an array of bytes.
0N/A */
0N/A public byte[] getBytes() throws IOException {
0N/A byte[] b;
0N/A // Get stream before content length so that a FileNotFoundException
0N/A // can propagate upwards without being caught too early
0N/A InputStream in = cachedInputStream();
0N/A
0N/A // This code has been uglified to protect against interrupts.
0N/A // Even if a thread has been interrupted when loading resources,
0N/A // the IO should not abort, so must carefully retry, failing only
0N/A // if the retry leads to some other IO exception.
0N/A
0N/A boolean isInterrupted = Thread.interrupted();
0N/A int len;
0N/A for (;;) {
0N/A try {
0N/A len = getContentLength();
0N/A break;
0N/A } catch (InterruptedIOException iioe) {
0N/A Thread.interrupted();
0N/A isInterrupted = true;
0N/A }
0N/A }
0N/A
0N/A try {
1822N/A b = new byte[0];
1822N/A if (len == -1) len = Integer.MAX_VALUE;
1822N/A int pos = 0;
1822N/A while (pos < len) {
1822N/A int bytesToRead;
1822N/A if (pos >= b.length) { // Only expand when there's no room
1822N/A bytesToRead = Math.min(len - pos, b.length + 1024);
1822N/A if (b.length < pos + bytesToRead) {
1822N/A b = Arrays.copyOf(b, pos + bytesToRead);
0N/A }
1822N/A } else {
1822N/A bytesToRead = b.length - pos;
0N/A }
1822N/A int cc = 0;
1822N/A try {
1822N/A cc = in.read(b, pos, bytesToRead);
1822N/A } catch (InterruptedIOException iioe) {
1822N/A Thread.interrupted();
1822N/A isInterrupted = true;
1822N/A }
1822N/A if (cc < 0) {
1822N/A if (len != Integer.MAX_VALUE) {
1822N/A throw new EOFException("Detect premature EOF");
1822N/A } else {
1822N/A if (b.length != pos) {
1822N/A b = Arrays.copyOf(b, pos);
1822N/A }
1822N/A break;
0N/A }
0N/A }
1822N/A pos += cc;
0N/A }
0N/A } finally {
0N/A try {
0N/A in.close();
0N/A } catch (InterruptedIOException iioe) {
0N/A isInterrupted = true;
0N/A } catch (IOException ignore) {}
0N/A
0N/A if (isInterrupted) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A }
0N/A return b;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Resource data as a ByteBuffer, but only if the input stream
0N/A * was implemented on top of a ByteBuffer. Return <tt>null</tt> otherwise.
0N/A */
0N/A public ByteBuffer getByteBuffer() throws IOException {
0N/A InputStream in = cachedInputStream();
0N/A if (in instanceof ByteBuffered) {
0N/A return ((ByteBuffered)in).getByteBuffer();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Manifest for the Resource, or null if none.
0N/A */
0N/A public Manifest getManifest() throws IOException {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns theCertificates for the Resource, or null if none.
0N/A */
0N/A public java.security.cert.Certificate[] getCertificates() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the code signers for the Resource, or null if none.
0N/A */
0N/A public CodeSigner[] getCodeSigners() {
0N/A return null;
0N/A }
0N/A}