Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
VLC-Android
Manage
Activity
Members
Labels
Plan
Issues
529
Issue boards
Milestones
Wiki
Code
Merge requests
14
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VideoLAN
VLC-Android
Commits
7e131d91
Commit
7e131d91
authored
8 years ago
by
Geoffrey Métais
Browse files
Options
Downloads
Patches
Plain Diff
Add computeHash for opensubtitles id
parent
01439c1c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
vlc-android/src/org/videolan/vlc/util/FileUtils.java
+53
-0
53 additions, 0 deletions
vlc-android/src/org/videolan/vlc/util/FileUtils.java
with
53 additions
and
0 deletions
vlc-android/src/org/videolan/vlc/util/FileUtils.java
+
53
−
0
View file @
7e131d91
...
...
@@ -44,9 +44,18 @@ import java.io.FileOutputStream;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.nio.ByteBuffer
;
import
java.nio.ByteOrder
;
import
java.nio.LongBuffer
;
import
java.nio.channels.FileChannel
;
public
class
FileUtils
{
/**
* Size of the chunks that will be hashed in bytes (64 KB)
*/
private
static
final
int
HASH_CHUNK_SIZE
=
64
*
1024
;
public
interface
Callback
{
void
onResult
(
boolean
success
);
}
...
...
@@ -240,4 +249,48 @@ public class FileUtils {
File
file
=
new
File
(
path
);
return
(
file
.
exists
()
&&
file
.
canWrite
());
}
public
static
String
computeHash
(
File
file
)
{
long
size
=
file
.
length
();
long
chunkSizeForFile
=
Math
.
min
(
HASH_CHUNK_SIZE
,
size
);
long
head
=
0
;
long
tail
=
0
;
FileInputStream
fis
=
null
;
FileChannel
fileChannel
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
fileChannel
=
fis
.
getChannel
();
head
=
computeHashForChunk
(
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
0
,
chunkSizeForFile
));
//Alternate way to calculate tail hash for files over 4GB.
ByteBuffer
bb
=
ByteBuffer
.
allocateDirect
((
int
)
chunkSizeForFile
);
int
read
;
long
position
=
Math
.
max
(
size
-
HASH_CHUNK_SIZE
,
0
);
while
((
read
=
fileChannel
.
read
(
bb
,
position
))
>
0
)
{
position
+=
read
;
}
bb
.
flip
();
tail
=
computeHashForChunk
(
bb
);
return
String
.
format
(
"%016x"
,
size
+
head
+
tail
);
}
catch
(
FileNotFoundException
e1
)
{
e1
.
printStackTrace
();
return
null
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
finally
{
Util
.
close
(
fileChannel
);
Util
.
close
(
fis
);
}
}
private
static
long
computeHashForChunk
(
ByteBuffer
buffer
)
{
LongBuffer
longBuffer
=
buffer
.
order
(
ByteOrder
.
LITTLE_ENDIAN
).
asLongBuffer
();
long
hash
=
0
;
while
(
longBuffer
.
hasRemaining
())
hash
+=
longBuffer
.
get
();
return
hash
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment