From bbc8a930f706d16e45dce2376ea388a0fd74bb7a Mon Sep 17 00:00:00 2001 From: Yuichiro Hanada Date: Wed, 14 Aug 2013 12:26:06 +0900 Subject: [PATCH] Add FusionDictionaryBufferFromWritableByteBufferFactory. Change-Id: I23de0a178e7f11f2cf301fd433cde60c6152055b --- .../latin/makedict/BinaryDictReader.java | 27 +++++++++++++++++++ .../latin/makedict/BinaryDictReaderTests.java | 18 +++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictReader.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictReader.java index 57a583228..a4a7ce458 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictReader.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictReader.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; @@ -82,6 +83,32 @@ public class BinaryDictReader { } } + /** + * Creates FusionDictionaryBuffer from a RandomAccessFile. + */ + @UsedForTesting + public static final class FusionDictionaryBufferFromWritableByteBufferFactory + implements FusionDictionaryBufferFactory { + @Override + public FusionDictionaryBufferInterface getFusionDictionaryBuffer(final File file) + throws FileNotFoundException, IOException { + RandomAccessFile raFile = null; + ByteBuffer buffer = null; + try { + raFile = new RandomAccessFile(file, "rw"); + buffer = raFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, file.length()); + } finally { + if (raFile != null) { + raFile.close(); + } + } + if (buffer != null) { + return new BinaryDictInputOutput.ByteBufferWrapper(buffer); + } + return null; + } + } + private final File mDictionaryBinaryFile; private FusionDictionaryBufferInterface mFusionDictionaryBuffer; diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java index 5f6950a4e..a46e5831b 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java @@ -18,8 +18,12 @@ package com.android.inputmethod.latin.makedict; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFactory; -import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory; -import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory; +import com.android.inputmethod.latin.makedict.BinaryDictReader. + FusionDictionaryBufferFromByteArrayFactory; +import com.android.inputmethod.latin.makedict.BinaryDictReader. + FusionDictionaryBufferFromByteBufferFactory; +import com.android.inputmethod.latin.makedict.BinaryDictReader. + FusionDictionaryBufferFromWritableByteBufferFactory; import android.test.AndroidTestCase; import android.util.Log; @@ -94,6 +98,11 @@ public class BinaryDictReaderTests extends AndroidTestCase { new FusionDictionaryBufferFromByteArrayFactory()); } + public void testOpenBufferWithWritableByteBuffer() { + runTestOpenBuffer("testOpenBufferWithWritableByteBuffer", + new FusionDictionaryBufferFromWritableByteBufferFactory()); + } + @SuppressWarnings("null") public void runTestGetBuffer(final String testName, final FusionDictionaryBufferFactory factory) { @@ -135,4 +144,9 @@ public class BinaryDictReaderTests extends AndroidTestCase { runTestGetBuffer("testGetBufferWithByteArray", new FusionDictionaryBufferFromByteArrayFactory()); } + + public void testGetBufferWithWritableByteBuffer() { + runTestGetBuffer("testGetBufferWithWritableByteBuffer", + new FusionDictionaryBufferFromWritableByteBufferFactory()); + } }