Fix: file descriptor leaking.

File descriptors will be closed in
BinaryDictionary.finalize(); but, this leads to unit test fails.

Bug: 13066902
Change-Id: I2b0d3e54ee91fa844530df54596f86bbbbac81a5
main
Keisuke Kuroyanagi 2014-02-21 17:25:34 +09:00
parent 1b3a142cac
commit d24a99cff6
2 changed files with 32 additions and 22 deletions

View File

@ -120,16 +120,10 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
// used only for testing.
private final DictionaryBufferFactory mBufferFactory;
protected DictBuffer mDictBuffer;
private final BinaryDictionary mBinaryDictionary;
/* package */ Ver2DictDecoder(final File file, final int factoryFlag) {
mDictionaryBinaryFile = file;
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) {
mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory();
} else if ((factoryFlag & MASK_DICTBUFFER) == USE_BYTEARRAY) {
@ -144,10 +138,6 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
/* package */ Ver2DictDecoder(final File file, final DictionaryBufferFactory factory) {
mDictionaryBinaryFile = file;
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
@ -172,7 +162,13 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
@Override
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) {
throw new IOException("Cannot read the dictionary header.");
}
@ -254,6 +250,11 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
@Override
public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
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 FusionDictionary fusionDict =
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
@ -261,11 +262,11 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();
do {
final BinaryDictionary.GetNextWordPropertyResult result =
mBinaryDictionary.getNextWordProperty(token);
binaryDictionary.getNextWordProperty(token);
final WordProperty wordProperty = result.mWordProperty;
if (wordProperty == null) {
binaryDictionary.close();
if (deleteDictIfBroken) {
mBinaryDictionary.close();
mDictionaryBinaryFile.delete();
}
return null;
@ -294,6 +295,7 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
}
}
binaryDictionary.close();
return fusionDict;
}

View File

@ -35,7 +35,6 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
private static final String TAG = Ver4DictDecoder.class.getSimpleName();
final File mDictDirectory;
final BinaryDictionary mBinaryDictionary;
@UsedForTesting
/* package */ Ver4DictDecoder(final File dictDirectory, final int factoryFlag) {
@ -45,24 +44,32 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
@UsedForTesting
/* package */ Ver4DictDecoder(final File dictDirectory, final DictionaryBufferFactory factory) {
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
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) {
throw new IOException("Cannot read the dictionary header.");
}
return mBinaryDictionary.getHeader();
return header;
}
@Override
public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
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 FusionDictionary fusionDict =
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
@ -70,11 +77,11 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();
do {
final BinaryDictionary.GetNextWordPropertyResult result =
mBinaryDictionary.getNextWordProperty(token);
binaryDictionary.getNextWordProperty(token);
final WordProperty wordProperty = result.mWordProperty;
if (wordProperty == null) {
binaryDictionary.close();
if (deleteDictIfBroken) {
mBinaryDictionary.close();
FileUtils.deleteRecursively(mDictDirectory);
}
return null;
@ -103,6 +110,7 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
}
}
binaryDictionary.close();
return fusionDict;
}
}