cz/vutbr/fit/dudka/SGVis/Lookup/SimpleDownloader.java

Go to the documentation of this file.
00001 package cz.vutbr.fit.dudka.SGVis.Lookup;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.IOException;
00005 import java.io.InputStreamReader;
00006 import java.net.URL;
00007 import java.net.URLConnection;
00008 
00009 
00014 public class SimpleDownloader {
00015   private int maxLength = 1 << 20;
00016   
00023   public String download(URL url) throws IOException {
00024     // Create connection object (next line do not connect immediately)
00025     URLConnection conn = url.openConnection();
00026     
00027     // Create reader
00028     BufferedReader reader = 
00029       new BufferedReader(new InputStreamReader(conn.getInputStream()));
00030     
00031     // Read content line after line
00032     StringBuffer buff = new StringBuffer();
00033     String inputLine;
00034     while (       
00035           buff.length()<maxLength &&
00036           null!= (inputLine = reader.readLine()))
00037       buff.append(inputLine);
00038     
00039     // close reader
00040         reader.close();
00041         
00042         // truncate unwanted content
00043         if (buff.length()>maxLength)
00044           buff.setLength(maxLength);
00045         
00046         // return content as string
00047     return buff.toString();
00048   }
00052   public int getMaxLength() {
00053     return maxLength;
00054   }
00058   public void setMaxLength(int maxLength) {
00059     this.maxLength = maxLength;
00060   }
00061 }

Generated on Sat May 3 22:56:09 2008 for SGVis by  doxygen 1.5.4