From 03b531554a9e9c59cef7f7bf3faa731009253f2d Mon Sep 17 00:00:00 2001 From: Oleksyuk Vadim Date: Wed, 13 Jan 2016 16:51:26 +0200 Subject: [PATCH] Home work 10 --- org/geekhub/ConnectionUtils.java | 16 ++++++++++++++-- org/geekhub/ImageCrawler.java | 26 +++++++++++++++++++++----- org/geekhub/ImageTask.java | 21 ++++++++++++++++++++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/org/geekhub/ConnectionUtils.java b/org/geekhub/ConnectionUtils.java index f67536f..1fbd018 100644 --- a/org/geekhub/ConnectionUtils.java +++ b/org/geekhub/ConnectionUtils.java @@ -1,6 +1,9 @@ package org.geekhub; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; /** @@ -16,7 +19,16 @@ public class ConnectionUtils { * @throws IOException */ public static byte[] getData(URL url) throws IOException { - //implement me - return null; + InputStream in = new BufferedInputStream(url.openStream()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int len = 0; + byte[] bytes = new byte[1024]; + while (-1!=(len=in.read(bytes))){ + out.write(bytes,0,len); + } + in.close(); + out.close(); + + return out.toByteArray(); } } diff --git a/org/geekhub/ImageCrawler.java b/org/geekhub/ImageCrawler.java index 8cad33b..3f1938b 100644 --- a/org/geekhub/ImageCrawler.java +++ b/org/geekhub/ImageCrawler.java @@ -1,8 +1,11 @@ package org.geekhub; -import java.io.*; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -29,7 +32,12 @@ public ImageCrawler(String folder) throws MalformedURLException { * @throws IOException */ public void downloadImages(String urlToPage) throws IOException { - //implement me + Page page = new Page(new URL(urlToPage)); + Collection imageLinks = page.getImageLinks(); + + imageLinks.stream() + .filter(imageLink -> isImageURL(imageLink)) + .forEach(imageLink -> executorService.execute(new ImageTask(imageLink,folder))); } /** @@ -41,10 +49,18 @@ public void stop() { //detects is current url is an image. Checking for popular extensions should be enough private boolean isImageURL(URL url) { - //implement me - return false; - } + List imageExtensions = Arrays.asList("jpg","jpeg","gif","png"); + String imageExtension = getExtension(url).toLowerCase(); + return imageExtensions.contains(imageExtension); + } + private String getExtension(URL url) { + String extension = ""; + String urlString = url.toString(); + int posDot = urlString.lastIndexOf("."); + if (posDot > 0) extension = urlString.substring(posDot + 1); + return extension; + } } diff --git a/org/geekhub/ImageTask.java b/org/geekhub/ImageTask.java index de0a340..6f17c9a 100644 --- a/org/geekhub/ImageTask.java +++ b/org/geekhub/ImageTask.java @@ -1,5 +1,9 @@ package org.geekhub; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.net.URL; /** @@ -20,7 +24,22 @@ public ImageTask(URL url, String folder) { */ @Override public void run() { - //implement me + OutputStream image = null; + try { + byte[] imageBytes = ConnectionUtils.getData(url); + String imagePath = folder + "\\" + buildFileName(url); + image = new BufferedOutputStream(new FileOutputStream(imagePath)); + image.write(imageBytes); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (image!=null) try { + image.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } //converts URL to unique file name