[Polycasso-commit] SF.net SVN: polycasso:[194] trunk/polycasso/src/com/mebigfatguy/polycasso/ URLFe
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-12-18 04:23:27
|
Revision: 194
http://polycasso.svn.sourceforge.net/polycasso/?rev=194&view=rev
Author: dbrosius
Date: 2009-12-18 04:23:13 +0000 (Fri, 18 Dec 2009)
Log Message:
-----------
split out fetching url data into a separate class and add support for file:// urls
Added Paths:
-----------
trunk/polycasso/src/com/mebigfatguy/polycasso/URLFetcher.java
Added: trunk/polycasso/src/com/mebigfatguy/polycasso/URLFetcher.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/URLFetcher.java (rev 0)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/URLFetcher.java 2009-12-18 04:23:13 UTC (rev 194)
@@ -0,0 +1,75 @@
+/*
+ * polycasso - Cubism Artwork generator
+ * Copyright 2009 MeBigFatGuy.com
+ * Copyright 2009 Dave Brosius
+ * Inspired by work by Roger Alsing
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package com.mebigfatguy.polycasso;
+
+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 org.apache.commons.io.IOUtils;
+
+/**
+ * manages downloading data from a url
+ */
+public class URLFetcher {
+
+ /**
+ * private to avoid construction of this static access only class
+ */
+ private URLFetcher() {
+ }
+
+ /**
+ * fetches the data at a specified url, whether http or file protocols
+ * @param url the url to fetch from
+ * @return a byte array of data from the url
+ *
+ * @throws IOException if the data at the url can't be retrieved
+ */
+ public static byte[] fetchURLData(String url) throws IOException {
+ HttpURLConnection con = null;
+ InputStream is = null;
+
+ try {
+ URL u = new URL(url);
+ if (url.startsWith("file://")) {
+ is = new BufferedInputStream(u.openStream());
+ } else {
+ 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();
+ }
+ }
+}
Property changes on: trunk/polycasso/src/com/mebigfatguy/polycasso/URLFetcher.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|