001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ResourceLoader.java,v $ 023 Revision 1.7 2004/05/05 21:22:45 markl 024 Comment header updates. 025 026 Revision 1.6 2003/01/19 09:42:39 markl 027 Javadoc & comment header updates. 028 029 Revision 1.5 2001/10/31 23:14:04 markl 030 Documentation fix. 031 032 Revision 1.4 2001/03/12 03:16:50 markl 033 *** empty log message *** 034 035 Revision 1.3 1999/05/03 09:35:12 markl 036 Fixed typo in javadoc. 037 038 Revision 1.2 1999/01/10 03:55:05 markl 039 added GPL header & RCS tag 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.util; 044 045import java.awt.*; 046import java.awt.image.ImageObserver; 047import java.io.*; 048import java.util.Properties; 049import java.net.URL; 050 051import kiwi.ui.AudioClip; 052 053/** A utility class containing methods for retrieving application resources; 054 * these resources typically reside within a JAR file among the classes that 055 * make up an application. The location of a resource is specified as a path 056 * relative to the location of a class within the application's class 057 * hierarchy. This "anchor class" is specified in the constructor. 058 * <p> 059 * Resources may be retrieved as byte arrays, as <code>String</code>s, as 060 * <code>InputStream</code>s, as <code>AudioClip</code>s, as 061 * <code>Image</code>s, or as <code>Properties</code> objects. 062 * <p> 063 * See <code>ResourceManager</code> for a higher-level interface. 064 * 065 * @see kiwi.util.ResourceManager 066 * 067 * @author Mark Lindner 068 */ 069 070public class ResourceLoader 071 { 072 private ResourceDecoder decoder; 073 074 /** The class object associated with this resource loader. */ 075 protected Class clazz = null; 076 077 /** Construct a new <code>ResourceLoader</code>. A new resource loader is 078 * created with a default input buffer size. 079 */ 080 081 public ResourceLoader(Class clazz) 082 { 083 this.clazz = clazz; 084 085 decoder = new ResourceDecoder(); 086 } 087 088 /** Retrieve a resource as a URL. 089 * 090 * @param path The location of the resource. 091 * 092 * @return A <code>URL</code> reference to the resource. 093 */ 094 095 public URL getResourceAsURL(String path) 096 { 097 return(clazz.getResource(path)); 098 } 099 100 /** Retrieve a resource as a stream. 101 * 102 * @param path The location of the resource. 103 * 104 * @return An <code>InputStream</code> from which the resource data may be 105 * read. 106 * 107 * @exception java.io.IOException If the resource was not found. 108 */ 109 110 public InputStream getResourceAsStream(String path) throws IOException 111 { 112 InputStream is = clazz.getResourceAsStream(path); 113 114 if(is == null) 115 throw(new IOException("Resource not found")); 116 117 return(is); 118 } 119 120 /** Retrieve a resource as a <code>String</code>. Retrieves the specified 121 * resource, returning its data as a <code>String</code>. It is assumed that 122 * the resource contains printable text. 123 * 124 * @param path The location of the resource. 125 * 126 * @exception java.io.IOException If an error occurred while reading the 127 * resource's data. 128 */ 129 130 public final String getResourceAsString(String path) throws IOException 131 { 132 InputStream is = getResourceAsStream(path); 133 String s = decoder.decodeString(is); 134 is.close(); 135 136 return(s); 137 } 138 139 /** Retrieve a resource as an <code>AudioClip</code>. Retrieves the specified 140 * resource, returning its data as an <code>AudioClip</code>. It is assumed 141 * that the resource contains valid audio data. 142 * 143 * @param path The location of the resource. 144 */ 145 146 public final AudioClip getResourceAsAudioClip(String path) 147 { 148 AudioClip clip = null; 149 150 try 151 { 152 InputStream is = getResourceAsStream(path); 153 clip = decoder.decodeAudioClip(is); 154 } 155 catch(IOException ex) {} 156 157 return(clip); 158 } 159 160 /** Retrieve a resource as an <code>Image</code>. Retrieves the specified 161 * resource, returning its data as an <code>Image</code>. It is assumed that 162 * the resource contains valid image data. 163 * 164 * @param path The location of the resource. 165 */ 166 167 public synchronized final Image getResourceAsImage(String path) 168 { 169 Toolkit tk = Toolkit.getDefaultToolkit(); 170 Image im = null; 171 172 try 173 { 174 InputStream is = getResourceAsStream(path); 175 im = decoder.decodeImage(is); 176 } 177 catch(IOException ex) { } 178 179 return(im); 180 } 181 182 /** Retrieve a resource as a <code>Properties</code> object. Retrieves the 183 * specified resource, returning its data as a <code>Properties</code> 184 * object. It is assumed that the resource is a properly-formatted property 185 * list. 186 * 187 * @param path The location of the resource. 188 * 189 * @exception java.io.IOException If an error occurred while reading the 190 * resource's data. 191 */ 192 193 public final Properties getResourceAsProperties(String path) 194 throws IOException 195 { 196 InputStream is = getResourceAsStream(path); 197 Properties prop = decoder.decodeProperties(is); 198 is.close(); 199 200 return(prop); 201 } 202 203 } 204 205/* end of source file */