1 /***
2 * Created on Jan 10, 2003
3 *
4 */
5 package net.sf.flock.util;
6
7 import java.io.ByteArrayInputStream;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.Reader;
12 import java.io.StringReader;
13 import java.io.UnsupportedEncodingException;
14 import java.net.URL;
15 import java.net.URLConnection;
16
17 import org.apache.log4j.LogManager;
18 import org.apache.log4j.Logger;
19
20 /***
21 * net.sf.flock.parser
22 * Created at Jan 10, 2003 in URLLoader.java
23 * @author zsombor.gegesy
24 *
25 */
26 class URLLoader {
27
28 private final static Logger LOGGER = LogManager.getLogger(URLLoader.class);
29
30 private URL url;
31
32 private String encoding;
33
34 private byte[] result;
35
36 public URLLoader(URL url) {
37 this.url = url;
38 }
39
40 public void load() throws IOException {
41 URLConnection connection = url.openConnection();
42 InputStream input = connection.getInputStream();
43 encoding = connection.getContentEncoding();
44 LOGGER.debug("open connection to "+url);
45 ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
46 byte[] buf = new byte[1024];
47 try {
48 int len = 0;
49 do {
50 len = input.read(buf);
51 LOGGER.debug("read "+len+" bytes");
52 if (len>0)
53 baos.write(buf,0,len);
54 } while(len>0);
55 } catch (IOException io) {
56 LOGGER.debug(io);
57 }
58 result = baos.toByteArray();
59 }
60
61 public String getAsString() throws UnsupportedEncodingException {
62 return getAsString(getEncoding());
63 }
64
65 public String getAsString(String encod) throws UnsupportedEncodingException {
66 if (encod == null) {
67 encod = "UTF-8";
68 }
69 return new String(result, encod);
70 }
71
72 public Reader getAsReader() throws UnsupportedEncodingException {
73 return getAsReader(getEncoding());
74 }
75
76 public Reader getAsReader(String encod) throws UnsupportedEncodingException {
77 return new StringReader(getAsString(encod));
78 }
79
80 public InputStream getAsInputStream() {
81 return new ByteArrayInputStream(result);
82 }
83
84 public String getEncoding() {
85 return encoding;
86 }
87
88 public void setEncoding(String encoding) {
89 this.encoding = encoding;
90 }
91
92 }
This page was automatically generated by Maven