[Polycasso-commit] SF.net SVN: polycasso:[196] trunk/polycasso/src/com/mebigfatguy/polycasso/ Rando
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-12-18 04:27:26
|
Revision: 196
http://polycasso.svn.sourceforge.net/polycasso/?rev=196&view=rev
Author: dbrosius
Date: 2009-12-18 04:27:18 +0000 (Fri, 18 Dec 2009)
Log Message:
-----------
refactor to use URLFetcher
Modified Paths:
--------------
trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java
Modified: trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java 2009-12-18 04:24:16 UTC (rev 195)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java 2009-12-18 04:27:18 UTC (rev 196)
@@ -19,12 +19,7 @@
package com.mebigfatguy.polycasso;
import java.awt.Image;
-import java.io.BufferedInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.ArrayList;
@@ -35,8 +30,6 @@
import javax.swing.ImageIcon;
-import org.apache.commons.io.IOUtils;
-
/**
* finds an image on the web to try to draw with polygons
*/
@@ -89,7 +82,7 @@
*/
private static final Image findImageAt(String url) throws IOException {
- String html = new String(getUrlData(url), "UTF-8");
+ String html = new String(URLFetcher.fetchURLData(url), "UTF-8");
List<String> images = new ArrayList<String>();
@@ -98,46 +91,15 @@
images.add(m.group(1));
}
- html = new String(getUrlData(ROOTURL + images.get((int)(Math.random() * images.size()))), "UTF-8");
+ html = new String(URLFetcher.fetchURLData(ROOTURL + images.get((int)(Math.random() * images.size()))), "UTF-8");
m = IMAGE_PATTERN.matcher(html);
if (m.find()) {
String imageUrl = URLDecoder.decode(m.group(1), "UTF-8");
- return new ImageIcon(getUrlData(imageUrl)).getImage();
+ return new ImageIcon(URLFetcher.fetchURLData(imageUrl)).getImage();
}
throw new IOException("Failed to find image");
}
-
- /**
- * retrieve arbitrary data found at a specific url
- *
- * @param url the url to retrieve
- * @return a byte array of the content
- *
- * @throws IOException the site fails to respond
- */
- private static byte[] getUrlData(String url) throws IOException {
- HttpURLConnection con = null;
- InputStream is = null;
-
- try {
- URL u = new URL(url);
- con = (HttpURLConnection)u.openConnection();
- con.addRequestProperty("User-Agent", "Mozilla/4.76");
- con.setDoInput(true);
- con.setDoOutput(false);
- con.connect();
-
- is = new BufferedInputStream(con.getInputStream());
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- IOUtils.copy(is, baos);
- return baos.toByteArray();
- } finally {
- IOUtils.closeQuietly(is);
- if (con != null)
- con.disconnect();
- }
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|