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