remove Id keyword
[jSite2.git] / src / net / pterodactylus / util / image / IconLoader.java
1 /*
2  * This program is free software; you can redistribute it and/or modify it under
3  * the terms of the GNU General Public License as published by the Free Software
4  * Foundation; either version 2 of the License, or (at your option) any later
5  * version.
6  * 
7  * This program is distributed in the hope that it will be useful, but WITHOUT
8  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10  * details.
11  * 
12  * You should have received a copy of the GNU General Public License along with
13  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
14  * Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 package net.pterodactylus.util.image;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import javax.swing.Icon;
24 import javax.swing.ImageIcon;
25
26 /**
27  * Loads an {@link Icon} from the class path.
28  * 
29  * @author <a href="mailto:droden@gmail.com">David Roden</a>
30  */
31 public class IconLoader {
32
33         /**
34          * Loads an icon from the class path.
35          * 
36          * @param resourceName
37          *            The name of the icon
38          * @return The icon, or <code>null</code> if no icon was found
39          */
40         public static Icon loadIcon(String resourceName) {
41                 try {
42                         InputStream resourceStream = IconLoader.class.getResourceAsStream(resourceName);
43                         if (resourceStream == null) {
44                                 return null;
45                         }
46                         ByteArrayOutputStream imageOutput = new ByteArrayOutputStream();
47                         byte[] buffer = new byte[16384];
48                         int r = 0;
49                         while ((r = resourceStream.read(buffer)) != -1) {
50                                 imageOutput.write(buffer, 0, r);
51                         }
52                         imageOutput.flush();
53                         return new ImageIcon(imageOutput.toByteArray());
54                 } catch (IOException e) {
55                         /* ignore. */
56                 }
57                 return null;
58         }
59
60 }