01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.client.util;
19:
20: import java.awt.Image;
21: import java.awt.Toolkit;
22: import java.io.ByteArrayOutputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.net.URLConnection;
26:
27: import org.apache.commons.httpclient.util.HttpURLConnection;
28:
29: import de.finix.contelligent.client.base.ContelligentConstants;
30:
31: /**
32: * Load icon from URL. Needed as ordinary way through ctor of URL does not
33: * update icons changed on server.
34: */
35: final public class URLImageProducer {
36:
37: public static Image produceFromURLConnection(URLConnection con)
38: throws IOException {
39: byte imageData[] = new byte[0];
40: con.setRequestProperty("User-Agent",
41: ContelligentConstants.CONTELLIGENT_USER_AGENT);
42:
43: if (con.getContentLength() != 0) {
44: InputStream in = con.getInputStream();
45: ByteArrayOutputStream out = new ByteArrayOutputStream();
46: byte buf[] = new byte[4096];
47: int read = -1;
48: while ((read = in.read(buf, 0, 4096)) > 0) {
49: out.write(buf, 0, read);
50: }
51: imageData = out.toByteArray();
52: out.close();
53: in.close();
54: }
55: return Toolkit.getDefaultToolkit().createImage(imageData);
56: }
57:
58: public static Image produceFromURLConnection(HttpURLConnection con)
59: throws IOException {
60: byte imageData[] = new byte[0];
61:
62: if (con.getContentLength() != 0) {
63: InputStream in = con.getInputStream();
64: ByteArrayOutputStream out = new ByteArrayOutputStream();
65: byte buf[] = new byte[4096];
66: int read = -1;
67: while ((read = in.read(buf, 0, 4096)) > 0) {
68: out.write(buf, 0, read);
69: }
70: imageData = out.toByteArray();
71: out.close();
72: in.close();
73: }
74: return Toolkit.getDefaultToolkit().createImage(imageData);
75: }
76:
77: }
|