From d24a99cff6da3a7121a507e77409261e4f6704dc Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Fri, 21 Feb 2014 17:25:34 +0900 Subject: [PATCH] Fix: file descriptor leaking. File descriptors will be closed in BinaryDictionary.finalize(); but, this leads to unit test fails. Bug: 13066902 Change-Id: I2b0d3e54ee91fa844530df54596f86bbbbac81a5 --- .../latin/makedict/Ver2DictDecoder.java | 28 ++++++++++--------- .../latin/makedict/Ver4DictDecoder.java | 26 +++++++++++------ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java index 71e120c5f..bf776cfc5 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java +++ b/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java @@ -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 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; } diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java index 88fff38f2..afe82317e 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java +++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java @@ -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 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; } }