001 package com.croftsoft.core.lang; 002 003 import java.awt.*; 004 import java.io.*; 005 006 /********************************************************************* 007 * Static methods to complement the java.lang.Class methods. 008 * 009 * @see 010 * java.lang.Class 011 * 012 * @version 013 * 2002-10-29 014 * @since 015 * 2001-07-24 016 * @author 017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 018 *********************************************************************/ 019 020 public final class ClassLib 021 ////////////////////////////////////////////////////////////////////// 022 ////////////////////////////////////////////////////////////////////// 023 { 024 025 public static void main ( String [ ] args ) 026 ////////////////////////////////////////////////////////////////////// 027 { 028 System.out.println ( test ( args ) ); 029 } 030 031 public static boolean test ( String [ ] args ) 032 ////////////////////////////////////////////////////////////////////// 033 { 034 return "ClassLib".equals ( getShortName ( ClassLib.class ) ); 035 } 036 037 /********************************************************************* 038 * Loads a resource into memory as a String. 039 * 040 * <p> 041 * Useful for loading a text file from a JAR on the classpath. 042 * </p> 043 * 044 * <p> 045 * Implemented using Class.getResourceAsStream(). Review of the 046 * documentation of this method is advised in order to understand the 047 * relative search path. 048 * </p> 049 * 050 * @param relativeClass 051 * 052 * The Class used for the text file search. 053 * 054 * @param filename 055 * 056 * The path to the file, usually started by "/". 057 * 058 * @see 059 * java.lang.Class#getResourceAsStream(java.lang.String) 060 *********************************************************************/ 061 public static String getResourceAsText ( 062 Class relativeClass, 063 String filename ) 064 throws IOException 065 ////////////////////////////////////////////////////////////////////// 066 { 067 NullArgumentException.check ( relativeClass ); 068 069 NullArgumentException.check ( filename ); 070 071 BufferedInputStream bufferedInputStream = null; 072 073 StringWriter out = null; 074 075 try 076 { 077 bufferedInputStream = new BufferedInputStream ( 078 relativeClass.getResourceAsStream ( filename ) ); 079 080 out = new StringWriter ( ); 081 082 int i; 083 084 while ( ( i = bufferedInputStream.read ( ) ) > -1 ) 085 { 086 out.write ( ( byte ) i ); 087 } 088 089 out.flush ( ); 090 091 return out.toString ( ); 092 } 093 finally 094 { 095 if ( bufferedInputStream != null ) 096 { 097 bufferedInputStream.close ( ); 098 } 099 100 if ( out != null ) 101 { 102 out.close ( ); 103 } 104 } 105 } 106 107 /********************************************************************* 108 * Loads an Image as a class resource. 109 * 110 * <p> 111 * Useful for when you need to load an image file from a JAR. 112 * </p> 113 * 114 * @see 115 * "Zukowski, 116 * <a href="http://amazon.com/exec/obidos/ASIN/189311578X/croftsoft-20"> 117 * Definitive Guide to Swing for Java 2</a>, 118 * Second Edition, 2000, p107." 119 *********************************************************************/ 120 public static Image getResourceAsImage ( 121 Class relativeClass, 122 String filename ) 123 throws IOException 124 ////////////////////////////////////////////////////////////////////// 125 { 126 NullArgumentException.check ( relativeClass ); 127 128 NullArgumentException.check ( filename ); 129 130 BufferedInputStream bufferedInputStream = null; 131 132 ByteArrayOutputStream byteArrayOutputStream = null; 133 134 try 135 { 136 bufferedInputStream = new BufferedInputStream ( 137 relativeClass.getResourceAsStream ( filename ) ); 138 139 byteArrayOutputStream = new ByteArrayOutputStream ( ); 140 141 int c; 142 143 while ( ( c = bufferedInputStream.read ( ) ) > -1 ) 144 { 145 byteArrayOutputStream.write ( c ); 146 } 147 148 byteArrayOutputStream.flush ( ); 149 150 return Toolkit.getDefaultToolkit ( ).createImage ( 151 byteArrayOutputStream.toByteArray ( ) ); 152 } 153 finally 154 { 155 if ( bufferedInputStream != null ) 156 { 157 bufferedInputStream.close ( ); 158 } 159 160 if ( byteArrayOutputStream != null ) 161 { 162 byteArrayOutputStream.close ( ); 163 } 164 } 165 } 166 167 /********************************************************************* 168 * Returns the name of the Class without the package prefix. 169 * 170 * <p> 171 * For example, this method would return "ClassLib" for 172 * class com.croftsoft.core.lang.ClassLib. 173 * </p> 174 * 175 * @see 176 * java.lang.Class#getName(java.lang.String) 177 *********************************************************************/ 178 public static String getShortName ( Class c ) 179 ////////////////////////////////////////////////////////////////////// 180 { 181 String longName = c.getName ( ); 182 183 return longName.substring ( longName.lastIndexOf ( '.' ) + 1 ); 184 } 185 186 ////////////////////////////////////////////////////////////////////// 187 ////////////////////////////////////////////////////////////////////// 188 189 private ClassLib ( ) { } 190 191 ////////////////////////////////////////////////////////////////////// 192 ////////////////////////////////////////////////////////////////////// 193 }