whitespace fixups
[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.awt.Image;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import javax.swing.Icon;
25 import javax.swing.ImageIcon;
26
27 /**
28  * Loads an {@link Icon} from the class path.
29  *
30  * @author <a href="mailto:droden@gmail.com">David Roden</a>
31  */
32 public class IconLoader {
33
34         /**
35          * Loads an icon from the class path.
36          *
37          * @param resourceName
38          *            The name of the icon
39          * @return The icon, or <code>null</code> if no icon was found
40          */
41         public static Icon loadIcon(String resourceName) {
42                 try {
43                         InputStream resourceStream = IconLoader.class.getResourceAsStream(resourceName);
44                         if (resourceStream == null) {
45                                 return null;
46                         }
47                         ByteArrayOutputStream imageOutput = new ByteArrayOutputStream();
48                         byte[] buffer = new byte[16384];
49                         int r = 0;
50                         while ((r = resourceStream.read(buffer)) != -1) {
51                                 imageOutput.write(buffer, 0, r);
52                         }
53                         imageOutput.flush();
54                         return new ImageIcon(imageOutput.toByteArray());
55                 } catch (IOException e) {
56                         /* ignore. */
57                 }
58                 return null;
59         }
60
61         /**
62          * Loads an image from the class path.
63          *
64          * @param resourceName
65          *            The name of the image
66          * @return The image, or <code>null</code> if no image was found
67          */
68         public static Image loadImage(String resourceName) {
69                 ImageIcon imageIcon = (ImageIcon) loadIcon(resourceName);
70                 if (imageIcon == null) {
71                         return null;
72                 }
73                 return imageIcon.getImage();
74         }
75
76 }