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:
parent
a63666d224
commit
8ea49e9f6a
2 changed files with 40 additions and 2 deletions
|
@ -186,8 +186,7 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBookHash() {
|
public String getBookHash() {
|
||||||
// TODO: implement
|
return myReader.Model.Book.getContentHashCode();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// page information
|
// page information
|
||||||
|
|
|
@ -20,6 +20,10 @@
|
||||||
package org.geometerplus.fbreader.library;
|
package org.geometerplus.fbreader.library;
|
||||||
|
|
||||||
import java.util.*;
|
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.util.ZLMiscUtil;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.*;
|
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
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return (int)myId;
|
return (int)myId;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue