KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > client > Resources


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

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 JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.zip.ZipEntry JavaDoc;
38 import java.util.zip.ZipFile JavaDoc;
39 import java.util.zip.ZipInputStream JavaDoc;
40
41 class Resources {
42     private static boolean runningFromJar() {
43         String JavaDoc className = Resources.class.getName().replace('.', '/');
44         String JavaDoc classJar = Util.class.getResource("/" + className + ".class").toString();
45         return classJar.startsWith("jar:");
46     }
47
48     private static String JavaDoc getJarPath() {
49         if (runningFromJar()) {
50             return System.getProperty("java.class.path");
51         }
52         return null;
53     }
54
55     static byte[] getResource(String JavaDoc path) throws MrtgException {
56         try {
57             if (runningFromJar()) {
58                 // extract from jar
59
String JavaDoc jarPath = getJarPath();
60                 JarResources jarResources = new JarResources(jarPath);
61                 return jarResources.getResource(path);
62             } else {
63                 ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
64                 FileInputStream JavaDoc inStream = new FileInputStream JavaDoc(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 JavaDoc e) {
75             throw new MrtgException(e);
76         }
77     }
78
79     static String JavaDoc getString(String JavaDoc path) throws MrtgException {
80         byte[] stringBytes = getResource(path);
81         return new String JavaDoc(stringBytes);
82     }
83
84     static ImageIcon getImageIcon(String JavaDoc path) throws MrtgException {
85         byte[] imageBytes = getResource(path);
86         return new ImageIcon(imageBytes);
87     }
88
89     static Image getImage(String JavaDoc path) throws MrtgException {
90         ImageIcon imageIcon = getImageIcon(path);
91         return imageIcon.getImage();
92     }
93 }
94
95 class JarResources {
96
97     // jar resource mapping tables
98
private Hashtable JavaDoc htSizes = new Hashtable JavaDoc();
99     private Hashtable JavaDoc htJarContents = new Hashtable JavaDoc();
100
101     // a jar file
102
private String JavaDoc jarFileName;
103
104     JarResources(String JavaDoc jarFileName) throws IOException JavaDoc {
105         this.jarFileName = jarFileName;
106         init();
107     }
108
109     byte[] getResource(String JavaDoc name) {
110         return (byte[]) htJarContents.get(name);
111     }
112
113     private void init() throws IOException JavaDoc {
114         // extracts just sizes only.
115
ZipFile JavaDoc zf = new ZipFile JavaDoc(jarFileName);
116         Enumeration JavaDoc e = zf.entries();
117         while (e.hasMoreElements()) {
118             ZipEntry JavaDoc ze = (ZipEntry JavaDoc) e.nextElement();
119             htSizes.put(ze.getName(), new Integer JavaDoc((int) ze.getSize()));
120         }
121         zf.close();
122         // extract resources and put them into the hashtable.
123
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(jarFileName);
124         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
125         ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(bis);
126         ZipEntry JavaDoc ze = null;
127         while ((ze = zis.getNextEntry()) != null) {
128             if (ze.isDirectory()) {
129                 continue;
130             }
131             int size = (int) ze.getSize();
132             // -1 means unknown size.
133
if (size == -1) {
134                 size = ((Integer JavaDoc) 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             // add to internal resource hashtable
147
htJarContents.put(ze.getName(), b);
148         }
149     }
150
151 }
152
153
Popular Tags