Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions org/geekhub/ConnectionUtils.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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();
}
}
26 changes: 21 additions & 5 deletions org/geekhub/ImageCrawler.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<URL> imageLinks = page.getImageLinks();

imageLinks.stream()
.filter(imageLink -> isImageURL(imageLink))
.forEach(imageLink -> executorService.execute(new ImageTask(imageLink,folder)));
}

/**
Expand All @@ -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<String> 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;
}

}
21 changes: 20 additions & 1 deletion org/geekhub/ImageTask.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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
Expand Down