1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +02:00

Book.getContentHash() implementation

This commit is contained in:
Nikolay Pultsin 2011-12-30 12:21:34 +00:00
parent a63666d224
commit 8ea49e9f6a
2 changed files with 40 additions and 2 deletions

View file

@ -186,8 +186,7 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A
}
public String getBookHash() {
// TODO: implement
return null;
return myReader.Model.Book.getContentHashCode();
}
// page information

View file

@ -20,6 +20,10 @@
package org.geometerplus.fbreader.library;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.zlibrary.core.filesystem.*;
@ -415,6 +419,41 @@ public class Book {
}
}
public String getContentHashCode() {
InputStream stream = null;
try {
final MessageDigest hash = MessageDigest.getInstance("SHA-256");
stream = File.getInputStream();
final byte[] buffer = new byte[2048];
while (true) {
final int nread = stream.read(buffer);
if (nread == -1) {
break;
}
hash.update(buffer, 0, nread);
}
final Formatter f = new Formatter();
for (byte b : hash.digest()) {
f.format("%02X", b & 0xFF);
}
return f.toString();
} catch (IOException e) {
return null;
} catch (NoSuchAlgorithmException e) {
return null;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
}
@Override
public int hashCode() {
return (int)myId;