Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #399: on-the-fly calculation of checksum #400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion crawler4j/src/main/java/edu/uci/ics/crawler4j/crawler/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -65,6 +69,11 @@ public class Page {
*/
protected byte[] contentData;

/**
* The checksum of this page's content.
*/
protected String contentChecksum;

/**
* The ContentType of this page.
* For example: "text/html; charset=UTF-8"
Expand Down Expand Up @@ -121,7 +130,13 @@ protected byte[] toByteArray(HttpEntity entity, int maxBytes) throws IOException
if (entity == null) {
return new byte[0];
}
try (InputStream is = entity.getContent()) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Cannot calculate checksum", e);
}
try (InputStream is = new DigestInputStream(entity.getContent(), md)) {
int size = (int) entity.getContentLength();
int readBufferLength = size;

Expand Down Expand Up @@ -150,6 +165,8 @@ protected byte[] toByteArray(HttpEntity entity, int maxBytes) throws IOException
}
}
return buffer.toByteArray();
} finally {
contentChecksum = DigestUtils.md5Hex(md.digest());
}
}

Expand Down Expand Up @@ -256,6 +273,13 @@ public void setContentData(byte[] contentData) {
this.contentData = contentData;
}

/**
* @return checksum of this page's content.
*/
public String getContentChecksum() {
return contentChecksum;
}

/**
* @return ContentType of this page.
* For example: "text/html; charset=UTF-8"
Expand Down