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: ResourceDecoder.java,v $ 023 Revision 1.6 2004/05/05 21:22:45 markl 024 Comment header updates. 025 026 Revision 1.5 2004/03/16 06:37:05 markl 027 *** empty log message *** 028 029 Revision 1.3 2003/01/19 09:42:39 markl 030 Javadoc & comment header updates. 031 032 Revision 1.2 2001/08/28 20:28:21 markl 033 Fixes to defer access to Toolkit for situations where no X Display is 034 available. 035 036 Revision 1.1 2001/03/12 03:16:49 markl 037 *** empty log message *** 038 ---------------------------------------------------------------------------- 039*/ 040 041package kiwi.util; 042 043import java.awt.*; 044import java.awt.image.ImageObserver; 045import java.io.*; 046import java.util.Properties; 047 048import kiwi.io.StreamUtils; 049import kiwi.ui.AudioClip; 050 051/** A class that decodes various types of resources from input streams. This 052 * class is used by resource loaders to read resources from files or network 053 * connections. The functionality is provided for its possible use in other 054 * contexts. 055 * 056 * @see kiwi.util.ResourceLoader 057 * @see kiwi.util.ResourceManager 058 * @since Kiwi 1.3 059 * 060 * @author Mark Lindner 061 */ 062 063public class ResourceDecoder implements ImageObserver 064 { 065 private static boolean imageLoaded = false; 066 067 /** Construct a new <code>ResourceDecoder</code>. 068 */ 069 070 public ResourceDecoder() 071 { 072 } 073 074 /** Decode a string from an input stream. Constructs a <code>String</code> 075 * object from all of the data read from an input stream. 076 * 077 * @param stream The input stream. 078 * @return The resulting <code>String</code> object. 079 * @throws java.io.IOException If an error occurred while reading from the 080 * stream. 081 */ 082 083 public String decodeString(InputStream stream) throws IOException 084 { 085 return(StreamUtils.readStreamToString(stream)); 086 } 087 088 /** Decode an audio clip from an input stream. Constructs an 089 * <code>AudioClip</code> object from all of the data read from an input 090 * stream. 091 * 092 * @param stream The input stream. 093 * @return The resulting <code>AudioCip</code> object. 094 * @throws java.io.IOException If an error occurred while reading from the 095 * stream. 096 */ 097 098 public AudioClip decodeAudioClip(InputStream stream) throws IOException 099 { 100 return(new AudioClip(stream)); 101 } 102 103 /** Decode an image from an input stream. Constructs an <code>Image</code> 104 * object from all of the data read from an input stream. 105 * 106 * @param stream The input stream. 107 * @return The resulting <code>Image</code> object. 108 * @throws java.io.IOException If an error occurred while reading from the 109 * stream. 110 */ 111 112 public synchronized Image decodeImage(InputStream stream) throws IOException 113 { 114 Toolkit toolkit = Toolkit.getDefaultToolkit(); 115 byte data[] = StreamUtils.readStreamToByteArray(stream); 116 Image im = toolkit.createImage(data); 117 imageLoaded = false; 118 toolkit.prepareImage(im, -1, -1, this); 119 120 while(!imageLoaded) 121 { 122 try { wait(); } 123 catch(InterruptedException ex) {} 124 } 125 126 return(im); 127 } 128 129 /** Decode a properties list from an input stream. Constructs a 130 * <code>Properties</code> object from all of the data read from an input 131 * stream. 132 * 133 * @param stream The input stream. 134 * @return The resulting <code>Properties</code> object. 135 * @throws java.io.IOException If an error occurred while reading from the 136 * stream. 137 */ 138 139 public Properties decodeProperties(InputStream stream) throws IOException 140 { 141 Properties prop = new Properties(); 142 prop.load(stream); 143 144 return(prop); 145 } 146 147 /** Decode a configuration from an input stream. Constructs a 148 * <code>Config</code> object from all of the data read from an input 149 * stream. 150 * 151 * @param stream The input stream. 152 * @return The resulting <code>Config</code> object. 153 * @throws java.io.IOException If an error occurred while reading from the 154 * stream. 155 */ 156 157 public Config decodeConfig(InputStream stream) throws IOException 158 { 159 Config config = new Config(); 160 config.load(stream); 161 162 return(config); 163 } 164 165 /** Image tracker method. This is an internal method and should not be called 166 * directly. 167 */ 168 169 public synchronized boolean imageUpdate(Image img, int infoflags, int x, 170 int y, int w, int h) 171 { 172 if((infoflags & (ALLBITS | FRAMEBITS)) != 0) 173 { 174 imageLoaded = true; 175 notifyAll(); 176 } 177 return(true); 178 } 179 } 180 181/* end of source file */