1 25 package org.jrobin.mrtg.client; 26 27 import org.jrobin.mrtg.MrtgException; 28 29 import javax.swing.*; 30 import java.awt.*; 31 import java.io.BufferedInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.FileInputStream ; 34 import java.io.IOException ; 35 import java.util.Enumeration ; 36 import java.util.Hashtable ; 37 import java.util.zip.ZipEntry ; 38 import java.util.zip.ZipFile ; 39 import java.util.zip.ZipInputStream ; 40 41 class Resources { 42 private static boolean runningFromJar() { 43 String className = Resources.class.getName().replace('.', '/'); 44 String classJar = Util.class.getResource("/" + className + ".class").toString(); 45 return classJar.startsWith("jar:"); 46 } 47 48 private static String getJarPath() { 49 if (runningFromJar()) { 50 return System.getProperty("java.class.path"); 51 } 52 return null; 53 } 54 55 static byte[] getResource(String path) throws MrtgException { 56 try { 57 if (runningFromJar()) { 58 String jarPath = getJarPath(); 60 JarResources jarResources = new JarResources(jarPath); 61 return jarResources.getResource(path); 62 } else { 63 ByteArrayOutputStream outStream = new ByteArrayOutputStream (); 64 FileInputStream inStream = new FileInputStream (path); 65 int b; 66 while((b = inStream.read()) != -1) { 67 outStream.write(b); 68 } 69 byte[] resource = outStream.toByteArray(); 70 inStream.close(); 71 outStream.close(); 72 return resource; 73 } 74 } catch (IOException e) { 75 throw new MrtgException(e); 76 } 77 } 78 79 static String getString(String path) throws MrtgException { 80 byte[] stringBytes = getResource(path); 81 return new String (stringBytes); 82 } 83 84 static ImageIcon getImageIcon(String path) throws MrtgException { 85 byte[] imageBytes = getResource(path); 86 return new ImageIcon(imageBytes); 87 } 88 89 static Image getImage(String path) throws MrtgException { 90 ImageIcon imageIcon = getImageIcon(path); 91 return imageIcon.getImage(); 92 } 93 } 94 95 class JarResources { 96 97 private Hashtable htSizes = new Hashtable (); 99 private Hashtable htJarContents = new Hashtable (); 100 101 private String jarFileName; 103 104 JarResources(String jarFileName) throws IOException { 105 this.jarFileName = jarFileName; 106 init(); 107 } 108 109 byte[] getResource(String name) { 110 return (byte[]) htJarContents.get(name); 111 } 112 113 private void init() throws IOException { 114 ZipFile zf = new ZipFile (jarFileName); 116 Enumeration e = zf.entries(); 117 while (e.hasMoreElements()) { 118 ZipEntry ze = (ZipEntry ) e.nextElement(); 119 htSizes.put(ze.getName(), new Integer ((int) ze.getSize())); 120 } 121 zf.close(); 122 FileInputStream fis = new FileInputStream (jarFileName); 124 BufferedInputStream bis = new BufferedInputStream (fis); 125 ZipInputStream zis = new ZipInputStream (bis); 126 ZipEntry ze = null; 127 while ((ze = zis.getNextEntry()) != null) { 128 if (ze.isDirectory()) { 129 continue; 130 } 131 int size = (int) ze.getSize(); 132 if (size == -1) { 134 size = ((Integer ) htSizes.get(ze.getName())).intValue(); 135 } 136 byte[] b = new byte[size]; 137 int rb = 0; 138 int chunk = 0; 139 while (size - rb > 0) { 140 chunk = zis.read(b, rb, size - rb); 141 if (chunk == -1) { 142 break; 143 } 144 rb += chunk; 145 } 146 htJarContents.put(ze.getName(), b); 148 } 149 } 150 151 } 152 153 | Popular Tags |