001: /*
002: * @(#)Resource.java 1.16 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.misc;
029:
030: import java.net.URL;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.util.jar.Manifest;
034: import java.util.jar.Attributes;
035:
036: /**
037: * This class is used to represent a Resource that has been loaded
038: * from the class path.
039: *
040: * @author David Connelly
041: * @version 1.10, 02/02/00
042: * @since JDK1.2
043: */
044: public abstract class Resource {
045: /**
046: * Returns the name of the Resource.
047: */
048: public abstract String getName();
049:
050: /**
051: * Returns the URL of the Resource.
052: */
053: public abstract URL getURL();
054:
055: /**
056: * Returns the CodeSource URL for the Resource.
057: */
058: public abstract URL getCodeSourceURL();
059:
060: /**
061: * Returns an InputStream for reading the Resource data.
062: */
063: public abstract InputStream getInputStream() throws IOException;
064:
065: /**
066: * Returns the length of the Resource data, or -1 if unknown.
067: */
068: public abstract int getContentLength() throws IOException;
069:
070: /**
071: * Returns the Resource data as an array of bytes.
072: */
073: public byte[] getBytes() throws IOException {
074: byte[] b;
075: // Get stream before content length so that a FileNotFoundException
076: // can propagate upwards without being caught too early
077: InputStream in = getInputStream();
078: int len = getContentLength();
079: try {
080: if (len != -1) {
081: // Read exactly len bytes from the input stream
082: b = new byte[len];
083: while (len > 0) {
084: int n = in.read(b, b.length - len, len);
085: if (n == -1) {
086: throw new IOException("unexpected EOF");
087: }
088: len -= n;
089: }
090: } else {
091: // Read until end of stream is reached
092: b = new byte[1024];
093: int total = 0;
094: while ((len = in.read(b, total, b.length - total)) != -1) {
095: total += len;
096: if (total >= b.length) {
097: byte[] tmp = new byte[total * 2];
098: System.arraycopy(b, 0, tmp, 0, total);
099: b = tmp;
100: }
101: }
102: // Trim array to correct size, if necessary
103: if (total != b.length) {
104: byte[] tmp = new byte[total];
105: System.arraycopy(b, 0, tmp, 0, total);
106: b = tmp;
107: }
108: }
109: } finally {
110: in.close();
111: }
112: return b;
113: }
114:
115: /**
116: * Returns the Manifest for the Resource, or null if none.
117: */
118: public Manifest getManifest() throws IOException {
119: return null;
120: }
121:
122: /**
123: * Returns theCertificates for the Resource, or null if none.
124: */
125: public java.security.cert.Certificate[] getCertificates() {
126: return null;
127: }
128: }
|