Clover coverage report - Flock Flock - 0.7-dev
Coverage timestamp: Thu Jan 30 2003 01:35:37 EST
file stats: LOC: 93   Methods: 9
NCLOC: 65   Classes: 1
This license of Clover is provided to support the development of Flock only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
URLLoader.java 0% 0% 0% 0%
 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  0
     public URLLoader(URL url) {
 37  0
         this.url = url;
 38   
     }
 39   
     
 40  0
     public void load() throws IOException {
 41  0
         URLConnection connection = url.openConnection();
 42  0
         InputStream input = connection.getInputStream();
 43  0
         encoding = connection.getContentEncoding();
 44  0
         LOGGER.debug("open connection to "+url);
 45  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
 46  0
         byte[] buf = new byte[1024];
 47  0
         try {
 48  0
             int len = 0;
 49  0
             do {
 50  0
                 len = input.read(buf);
 51  0
                 LOGGER.debug("read "+len+" bytes");
 52  0
                 if (len>0) 
 53  0
                     baos.write(buf,0,len);
 54  0
             } while(len>0);
 55   
         } catch (IOException io) {
 56  0
             LOGGER.debug(io);
 57   
         }
 58  0
         result = baos.toByteArray();
 59   
     }
 60   
     
 61  0
     public String getAsString() throws UnsupportedEncodingException {
 62  0
         return getAsString(getEncoding());
 63   
     }
 64   
     
 65  0
     public String getAsString(String encod) throws UnsupportedEncodingException {
 66  0
         if (encod == null) {
 67  0
             encod = "UTF-8";
 68   
         }
 69  0
         return new String(result, encod);
 70   
     }
 71   
 
 72  0
     public Reader getAsReader() throws UnsupportedEncodingException {
 73  0
         return getAsReader(getEncoding());
 74   
     }
 75   
 
 76  0
     public Reader getAsReader(String encod) throws UnsupportedEncodingException {
 77  0
         return new StringReader(getAsString(encod));
 78   
     }
 79   
     
 80  0
     public InputStream getAsInputStream() {
 81  0
         return new ByteArrayInputStream(result);
 82   
     }
 83   
     
 84  0
     public String getEncoding() {
 85  0
         return encoding;
 86   
     }
 87   
 
 88  0
     public void setEncoding(String encoding) {
 89  0
         this.encoding = encoding;
 90   
     }
 91   
 
 92   
 }
 93