source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/util/JarTools.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 4.0 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2005 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.util;
28
29
30import java.awt.*;
31import java.io.*;
32import java.net.*;
33import javax.swing.*;
34import org.greenstone.gatherer.DebugStream;
35
36
37public class JarTools
38{
39 static private Class root_class = null;
40 static private ImageIcon ERROR_ICON = null;
41
42
43 static public void initialise(Object root_object)
44 {
45 root_class = root_object.getClass();
46 ERROR_ICON = getImage("error.gif");
47 }
48
49
50 static public void extractFromJar(String filename, String dst_dir, boolean must_be_present)
51 {
52 try {
53 // setup input stream for slurping out file
54 InputStream fis = root_class.getResourceAsStream("/"+filename);
55 BufferedInputStream fbis = new BufferedInputStream(fis);
56 DataInputStream fdbis = new DataInputStream(fbis);
57
58 // set up output stream for writing to disk
59 String ofname = dst_dir + filename;
60 FileOutputStream fos = new FileOutputStream(ofname);
61 BufferedOutputStream bfos = new BufferedOutputStream(fos);
62
63 byte[] buf = new byte[1024];
64 int len;
65 int total_bytes = 0;
66 while ((len = fdbis.read(buf)) >= 0) {
67 bfos.write(buf,0,len);
68 total_bytes += len;
69 }
70
71 fdbis.close();
72 bfos.close();
73 }
74 catch (Exception exception) {
75 if (must_be_present) {
76 exception.printStackTrace();
77 }
78 }
79 }
80
81
82 /** Method to retrieve an image icon with the given filename found in classpath or the resouces directory.
83 * @return The specified <strong>ImageIcon</strong>, or an error image replacement if no such images exists.
84 */
85 static public ImageIcon getImage(String filename) {
86 return getImage(filename, false);
87 }
88
89 static public ImageIcon getImage(String filename, boolean wait_until_complete)
90 {
91 ImageIcon image = null;
92 try {
93 image = new ImageIcon(root_class.getResource("/images/" + filename));
94 }
95 catch (NullPointerException exception) {
96 System.err.println("Error: Could not load image " + filename);
97 DebugStream.println("Error: Could not load image " + filename);
98 }
99
100 if (image == null) {
101 image = ERROR_ICON;
102 }
103
104 if (wait_until_complete) {
105 int load_status;
106 do {
107 load_status = image.getImageLoadStatus();
108 }
109 while (load_status != MediaTracker.ABORTED && load_status != MediaTracker.ERRORED && load_status != MediaTracker.COMPLETE);
110 }
111
112 return image;
113 }
114
115
116 static public URL getResource(String resource_name)
117 {
118 return root_class.getResource(resource_name);
119 }
120
121
122 static public InputStream getResourceAsStream(String resource_name)
123 {
124 return root_class.getResourceAsStream(resource_name);
125 }
126
127
128 static public boolean isInJar(String filename)
129 {
130 try {
131 InputStream fis = root_class.getResourceAsStream("/" + filename);
132 fis.close();
133 }
134 catch (Exception exception) {
135 exception.printStackTrace();
136 return false;
137 }
138
139 return true;
140 }
141}
Note: See TracBrowser for help on using the repository browser.