Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LatinIME
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
keyboard
LatinIME
Commits
fcee70ea
Commit
fcee70ea
authored
13 years ago
by
Jean Chalard
Committed by
Android (Google) Code Review
13 years ago
Browse files
Options
Downloads
Plain Diff
Merge "Check the magic number of a decoded file"
parents
b8dc6746
7a408431
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+36
-6
36 additions, 6 deletions
...android/inputmethod/latin/BinaryDictionaryFileDumper.java
with
36 additions
and
6 deletions
java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+
36
−
6
View file @
fcee70ea
...
...
@@ -25,12 +25,15 @@ import android.net.Uri;
import
android.text.TextUtils
;
import
android.util.Log
;
import
java.io.BufferedInputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Locale
;
...
...
@@ -46,7 +49,9 @@ public class BinaryDictionaryFileDumper {
/**
* The size of the temporary buffer to copy files.
*/
static
final
int
FILE_READ_BUFFER_SIZE
=
1024
;
private
static
final
int
FILE_READ_BUFFER_SIZE
=
1024
;
// TODO: make the following data common with the native code
private
static
final
byte
[]
MAGIC_NUMBER
=
new
byte
[]
{
0x78
,
(
byte
)
0xB1
};
private
static
final
String
DICTIONARY_PROJECTION
[]
=
{
"id"
};
...
...
@@ -135,6 +140,7 @@ public class BinaryDictionaryFileDumper {
for
(
int
mode
=
MODE_MIN
;
mode
<=
MODE_MAX
;
++
mode
)
{
InputStream
originalSourceStream
=
null
;
InputStream
inputStream
=
null
;
File
outputFile
=
null
;
FileOutputStream
outputStream
=
null
;
AssetFileDescriptor
afd
=
null
;
try
{
...
...
@@ -144,7 +150,8 @@ public class BinaryDictionaryFileDumper {
if
(
null
==
afd
)
return
null
;
originalSourceStream
=
afd
.
createInputStream
();
// Open output.
outputStream
=
new
FileOutputStream
(
outputFileName
);
outputFile
=
new
File
(
outputFileName
);
outputStream
=
new
FileOutputStream
(
outputFile
);
// Get the appropriate decryption method for this try
switch
(
mode
)
{
case
COMPRESSED_CRYPTED_COMPRESSED:
...
...
@@ -171,7 +178,7 @@ public class BinaryDictionaryFileDumper {
inputStream
=
originalSourceStream
;
break
;
}
c
opyFileTo
(
inputStream
,
outputStream
);
c
heckMagicAndCopyFileTo
(
new
BufferedInputStream
(
inputStream
)
,
outputStream
);
if
(
0
>=
resolver
.
delete
(
wordListUri
,
null
,
null
))
{
Log
.
e
(
TAG
,
"Could not have the dictionary pack delete a word list"
);
}
...
...
@@ -181,6 +188,12 @@ public class BinaryDictionaryFileDumper {
if
(
DEBUG
)
{
Log
.
i
(
TAG
,
"Can't open word list in mode "
+
mode
+
" : "
+
e
);
}
if
(
null
!=
outputFile
)
{
// This may or may not fail. The file may not have been created if the
// exception was thrown before it could be. Hence, both failure and
// success are expected outcomes, so we don't check the return value.
outputFile
.
delete
();
}
// Try the next method.
}
finally
{
// Ignore exceptions while closing files.
...
...
@@ -234,12 +247,29 @@ public class BinaryDictionaryFileDumper {
}
/**
* Copies the data in an input stream to a target file.
* Copies the data in an input stream to a target file if the magic number matches.
*
* If the magic number does not match the expected value, this method throws an
* IOException. Other usual conditions for IOException or FileNotFoundException
* also apply.
*
* @param input the stream to be copied.
* @param outputFile an outputstream to copy the data to.
*/
private
static
void
copyFileTo
(
final
InputStream
input
,
final
FileOutputStream
output
)
throws
FileNotFoundException
,
IOException
{
private
static
void
checkMagicAndCopyFileTo
(
final
BufferedInputStream
input
,
final
FileOutputStream
output
)
throws
FileNotFoundException
,
IOException
{
// Check the magic number
final
byte
[]
magicNumberBuffer
=
new
byte
[
MAGIC_NUMBER
.
length
];
final
int
readMagicNumberSize
=
input
.
read
(
magicNumberBuffer
,
0
,
MAGIC_NUMBER
.
length
);
if
(
readMagicNumberSize
<
MAGIC_NUMBER
.
length
)
{
throw
new
IOException
(
"Less bytes to read than the magic number length"
);
}
if
(!
Arrays
.
equals
(
MAGIC_NUMBER
,
magicNumberBuffer
))
{
throw
new
IOException
(
"Wrong magic number for downloaded file"
);
}
output
.
write
(
MAGIC_NUMBER
);
// Actually copy the file
final
byte
[]
buffer
=
new
byte
[
FILE_READ_BUFFER_SIZE
];
for
(
int
readBytes
=
input
.
read
(
buffer
);
readBytes
>=
0
;
readBytes
=
input
.
read
(
buffer
))
output
.
write
(
buffer
,
0
,
readBytes
);
...
...
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