Merge "Fix: file descriptor leaking."

main
Keisuke Kuroyanagi 2014-02-21 11:37:09 +00:00 committed by Android (Google) Code Review
commit bb6b978a19
2 changed files with 32 additions and 22 deletions

View File

@ -120,16 +120,10 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
// used only for testing. // used only for testing.
private final DictionaryBufferFactory mBufferFactory; private final DictionaryBufferFactory mBufferFactory;
protected DictBuffer mDictBuffer; protected DictBuffer mDictBuffer;
private final BinaryDictionary mBinaryDictionary;
/* package */ Ver2DictDecoder(final File file, final int factoryFlag) { /* package */ Ver2DictDecoder(final File file, final int factoryFlag) {
mDictionaryBinaryFile = file; mDictionaryBinaryFile = file;
mDictBuffer = null; mDictBuffer = null;
// dictType is not being used in dicttool. Passing an empty string.
mBinaryDictionary = new BinaryDictionary(file.getAbsolutePath(),
0 /* offset */, file.length() /* length */, true /* useFullEditDistance */,
null /* locale */, "" /* dictType */, false /* isUpdatable */);
if ((factoryFlag & MASK_DICTBUFFER) == USE_READONLY_BYTEBUFFER) { if ((factoryFlag & MASK_DICTBUFFER) == USE_READONLY_BYTEBUFFER) {
mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory(); mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory();
} else if ((factoryFlag & MASK_DICTBUFFER) == USE_BYTEARRAY) { } else if ((factoryFlag & MASK_DICTBUFFER) == USE_BYTEARRAY) {
@ -144,10 +138,6 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
/* package */ Ver2DictDecoder(final File file, final DictionaryBufferFactory factory) { /* package */ Ver2DictDecoder(final File file, final DictionaryBufferFactory factory) {
mDictionaryBinaryFile = file; mDictionaryBinaryFile = file;
mBufferFactory = factory; mBufferFactory = factory;
// dictType is not being used in dicttool. Passing an empty string.
mBinaryDictionary = new BinaryDictionary(file.getAbsolutePath(),
0 /* offset */, file.length() /* length */, true /* useFullEditDistance */,
null /* locale */, "" /* dictType */, false /* isUpdatable */);
} }
@Override @Override
@ -172,7 +162,13 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
@Override @Override
public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException { public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
final DictionaryHeader header = mBinaryDictionary.getHeader(); // dictType is not being used in dicttool. Passing an empty string.
final BinaryDictionary binaryDictionary = new BinaryDictionary(
mDictionaryBinaryFile.getAbsolutePath(), 0 /* offset */,
mDictionaryBinaryFile.length() /* length */, true /* useFullEditDistance */,
null /* locale */, "" /* dictType */, false /* isUpdatable */);
final DictionaryHeader header = binaryDictionary.getHeader();
binaryDictionary.close();
if (header == null) { if (header == null) {
throw new IOException("Cannot read the dictionary header."); throw new IOException("Cannot read the dictionary header.");
} }
@ -254,6 +250,11 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
@Override @Override
public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken) public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException { throws FileNotFoundException, IOException, UnsupportedFormatException {
// dictType is not being used in dicttool. Passing an empty string.
final BinaryDictionary binaryDictionary = new BinaryDictionary(
mDictionaryBinaryFile.getAbsolutePath(), 0 /* offset */,
mDictionaryBinaryFile.length() /* length */, true /* useFullEditDistance */,
null /* locale */, "" /* dictType */, false /* isUpdatable */);
final DictionaryHeader header = readHeader(); final DictionaryHeader header = readHeader();
final FusionDictionary fusionDict = final FusionDictionary fusionDict =
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions); new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
@ -261,11 +262,11 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList(); final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();
do { do {
final BinaryDictionary.GetNextWordPropertyResult result = final BinaryDictionary.GetNextWordPropertyResult result =
mBinaryDictionary.getNextWordProperty(token); binaryDictionary.getNextWordProperty(token);
final WordProperty wordProperty = result.mWordProperty; final WordProperty wordProperty = result.mWordProperty;
if (wordProperty == null) { if (wordProperty == null) {
binaryDictionary.close();
if (deleteDictIfBroken) { if (deleteDictIfBroken) {
mBinaryDictionary.close();
mDictionaryBinaryFile.delete(); mDictionaryBinaryFile.delete();
} }
return null; return null;
@ -294,6 +295,7 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo); fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
} }
} }
binaryDictionary.close();
return fusionDict; return fusionDict;
} }

View File

@ -35,7 +35,6 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
private static final String TAG = Ver4DictDecoder.class.getSimpleName(); private static final String TAG = Ver4DictDecoder.class.getSimpleName();
final File mDictDirectory; final File mDictDirectory;
final BinaryDictionary mBinaryDictionary;
@UsedForTesting @UsedForTesting
/* package */ Ver4DictDecoder(final File dictDirectory, final int factoryFlag) { /* package */ Ver4DictDecoder(final File dictDirectory, final int factoryFlag) {
@ -45,24 +44,32 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
@UsedForTesting @UsedForTesting
/* package */ Ver4DictDecoder(final File dictDirectory, final DictionaryBufferFactory factory) { /* package */ Ver4DictDecoder(final File dictDirectory, final DictionaryBufferFactory factory) {
mDictDirectory = dictDirectory; mDictDirectory = dictDirectory;
// dictType is not being used in dicttool. Passing an empty string.
mBinaryDictionary = new BinaryDictionary(dictDirectory.getAbsolutePath(),
0 /* offset */, 0 /* length */, true /* useFullEditDistance */, null /* locale */,
"" /* dictType */, true /* isUpdatable */);
} }
@Override @Override
public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException { public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
final DictionaryHeader header = mBinaryDictionary.getHeader(); // dictType is not being used in dicttool. Passing an empty string.
final BinaryDictionary binaryDictionary= new BinaryDictionary(
mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
true /* useFullEditDistance */, null /* locale */,
"" /* dictType */, true /* isUpdatable */);
final DictionaryHeader header = binaryDictionary.getHeader();
binaryDictionary.close();
if (header == null) { if (header == null) {
throw new IOException("Cannot read the dictionary header."); throw new IOException("Cannot read the dictionary header.");
} }
return mBinaryDictionary.getHeader(); return header;
} }
@Override @Override
public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken) public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException { throws FileNotFoundException, IOException, UnsupportedFormatException {
// dictType is not being used in dicttool. Passing an empty string.
final BinaryDictionary binaryDictionary = new BinaryDictionary(
mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
true /* useFullEditDistance */, null /* locale */,
"" /* dictType */, true /* isUpdatable */);
final DictionaryHeader header = readHeader(); final DictionaryHeader header = readHeader();
final FusionDictionary fusionDict = final FusionDictionary fusionDict =
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions); new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
@ -70,11 +77,11 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList(); final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();
do { do {
final BinaryDictionary.GetNextWordPropertyResult result = final BinaryDictionary.GetNextWordPropertyResult result =
mBinaryDictionary.getNextWordProperty(token); binaryDictionary.getNextWordProperty(token);
final WordProperty wordProperty = result.mWordProperty; final WordProperty wordProperty = result.mWordProperty;
if (wordProperty == null) { if (wordProperty == null) {
binaryDictionary.close();
if (deleteDictIfBroken) { if (deleteDictIfBroken) {
mBinaryDictionary.close();
FileUtils.deleteRecursively(mDictDirectory); FileUtils.deleteRecursively(mDictDirectory);
} }
return null; return null;
@ -103,6 +110,7 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo); fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
} }
} }
binaryDictionary.close();
return fusionDict; return fusionDict;
} }
} }