44// Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
55package graphtea .platform .extension ;
66
7- import java .util .ArrayList ;
8- import java .util .List ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
99import graphtea .platform .StaticUtils ;
1010import graphtea .platform .core .exception .ExceptionHandler ;
1111
2222 */
2323public class ExtensionClassLoader extends ClassLoader {
2424 public Map <String , byte []> classesData = new HashMap <>();
25- Map <String , Class > classes = new HashMap <>();
25+ Map <String , Class <?> > classes = new HashMap <>();
2626 List <File > unknownFiles = new ArrayList <>();
2727 public static URLClassLoader classLoader ;
2828 public static ClassLoader cl ;
@@ -31,7 +31,7 @@ public ExtensionClassLoader(String dirPath) {
3131 loadClasses (dirPath );
3232 }
3333
34- public Collection <Class > getLoadedClasses () {
34+ public Collection <Class <?> > getLoadedClasses () {
3535 return classes .values ();
3636 }
3737
@@ -60,14 +60,16 @@ private void loadClassFiles(String dir, String pack) {
6060 else if (file1 .getName ().endsWith (".class" ))
6161 try {
6262 String name ;
63- if (pack .length () > 0 )
63+ if (pack .length () > 0 ) {
6464 name = pack .substring (1 ) + "." ;
65- else
65+ } else {
6666 name = "" ;
67+ }
6768 name = name + file1 .getName ().substring (0 , file1 .getName ().length () - 6 );
6869 byte [] data = new byte [(int ) file1 .length ()];
69- FileInputStream fis = new FileInputStream (file1 );
70- fis .read (data );
70+ try (FileInputStream fis = new FileInputStream (file1 )) {
71+ fis .read (data );
72+ }
7173 classesData .put (name , data );
7274 urls .add (file1 .toURI ().toURL ());
7375 } catch (IOException e ) {
@@ -85,26 +87,28 @@ private void loadClasses(String dirPath) {
8587// defineClasses();
8688 }
8789
88- public Class findClass (String name ) throws ClassNotFoundException {
89- Class ret = null ;
90- if (!classesData .containsKey (name ))
90+ public Class <?> findClass (String name ) throws ClassNotFoundException {
91+ Class <?> ret = null ;
92+ if (!classesData .containsKey (name )) {
9193 ret = getParent ().loadClass (name );
94+ }
9295 if (ret == null && !classes .containsKey (name )) {
9396 byte [] data = classesData .get (name );
94- Class c = defineClass (name , data , 0 , data .length );
97+ Class <?> c = defineClass (name , data , 0 , data .length );
9598 classes .put (name , c );
9699 }
97100 ret = classes .get (name );
98- if (ret == null )
101+ if (ret == null ) {
99102 return classLoader .loadClass (name );
100- else
103+ } else {
101104 return ret ;
105+ }
102106 }
103107
104- public Collection <Class > getClassesImplementing (Class cl ) {
105- Collection <Class > col = new ArrayList <>();
106- for (Map .Entry <String , Class > entry1 : classes .entrySet ()) {
107- Class c = entry1 .getValue ();
108+ public Collection <Class <?>> getClassesImplementing (Class <?> cl ) {
109+ Collection <Class <?> > col = new ArrayList <>();
110+ for (Map .Entry <String , Class <?> > entry1 : classes .entrySet ()) {
111+ Class <?> c = entry1 .getValue ();
108112 if (StaticUtils .isImplementing (c , cl ))
109113 col .add (c );
110114 }
@@ -131,36 +135,23 @@ public static void copyInputStream(InputStream in, OutputStream out)
131135 * @param zipFileName The given zipped file name
132136 */
133137 public static void unZip (String zipFileName , String destDir ) {
134- System .out .println (zipFileName );
135- Enumeration entries ;
136- ZipFile zipFile ;
137-
138- try {
139- zipFile = new ZipFile (zipFileName );
140-
141- entries = zipFile .entries ();
142-
143- while (entries .hasMoreElements ()) {
144- ZipEntry entry = (ZipEntry )entries .nextElement ();
145-
146- if (entry .isDirectory ()) {
147- // Assume directories are stored parents first then children.
148- // System.err.println("Extracting directory: " + entry.getName());
149- // This is not robust, just for demonstration purposes.
150- (new File (destDir +File .separator +entry .getName ())).mkdirs ();
151- continue ;
152- }
153-
154- System .out .println ("Extracting file: " + destDir +entry .getName ());
155- copyInputStream (zipFile .getInputStream (entry ),
156- new BufferedOutputStream (new FileOutputStream (destDir +File .separator +entry .getName ())));
138+ try (ZipFile zipFile = new ZipFile (zipFileName )) {
139+ Enumeration <? extends ZipEntry > entries = zipFile .entries ();
140+ while (entries .hasMoreElements ()) {
141+ ZipEntry entry = entries .nextElement ();
142+ if (entry .isDirectory ()) {
143+ new File (destDir + File .separator + entry .getName ()).mkdirs ();
144+ continue ;
145+ }
146+ try (InputStream in = zipFile .getInputStream (entry );
147+ OutputStream out = new BufferedOutputStream (
148+ new FileOutputStream (destDir + File .separator + entry .getName ()))) {
149+ copyInputStream (in , out );
150+ }
151+ }
152+ } catch (IOException ioe ) {
153+ ExceptionHandler .catchException (ioe );
157154 }
158-
159- zipFile .close ();
160- } catch (IOException ioe ) {
161- System .err .println ("Unhandled exception:" );
162- ExceptionHandler .catchException (ioe );
163- }
164155 }
165156
166157
0 commit comments