ICU-10944 Switch DictionaryData to use ByteBuffer.

R=markus.icu@gmail.com

Review URL: https://codereview.appspot.com/106610043

X-SVN-Rev: 36041
This commit is contained in:
Fredrik Roubert 2014-07-15 21:14:31 +00:00
parent 4a92ee0841
commit dee32dda95

View File

@ -1,14 +1,15 @@
/* /*
******************************************************************************* *******************************************************************************
* Copyright (C) 2012, International Business Machines Corporation and * * Copyright (C) 2012-2014, International Business Machines Corporation and
* others. All Rights Reserved. * * others. All Rights Reserved.
******************************************************************************* *******************************************************************************
*/ */
package com.ibm.icu.text; package com.ibm.icu.text;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer;
import com.ibm.icu.impl.Assert; import com.ibm.icu.impl.Assert;
import com.ibm.icu.impl.ICUBinary; import com.ibm.icu.impl.ICUBinary;
@ -39,26 +40,25 @@ final class DictionaryData {
public static final int IX_RESERVED7 = 7; public static final int IX_RESERVED7 = 7;
public static final int IX_COUNT = 8; public static final int IX_COUNT = 8;
private static final byte DATA_FORMAT_ID[] = { (byte) 0x44, (byte) 0x69, private static final int DATA_FORMAT_ID = 0x44696374;
(byte) 0x63, (byte) 0x74 };
public static DictionaryMatcher loadDictionaryFor(String dictType) throws IOException { public static DictionaryMatcher loadDictionaryFor(String dictType) throws IOException {
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BRKITR_BASE_NAME); ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BRKITR_BASE_NAME);
String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType); String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType);
dictFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + dictFileName; dictFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + dictFileName;
InputStream is = ICUData.getStream(dictFileName); InputStream is = ICUData.getStream(dictFileName);
ICUBinary.readHeader(is, DATA_FORMAT_ID, null); ByteBuffer bytes = ICUBinary.getByteBufferFromInputStream(is);
DataInputStream s = new DataInputStream(is); ICUBinary.readHeader(bytes, DATA_FORMAT_ID, null);
int[] indexes = new int[IX_COUNT]; int[] indexes = new int[IX_COUNT];
// TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[] // TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[]
for (int i = 0; i < IX_COUNT; i++) { for (int i = 0; i < IX_COUNT; i++) {
indexes[i] = s.readInt(); indexes[i] = bytes.getInt();
} }
int offset = indexes[IX_STRING_TRIE_OFFSET]; int offset = indexes[IX_STRING_TRIE_OFFSET];
Assert.assrt(offset >= (4 * IX_COUNT)); Assert.assrt(offset >= (4 * IX_COUNT));
if (offset > (4 * IX_COUNT)) { if (offset > (4 * IX_COUNT)) {
int diff = offset - (4 * IX_COUNT); int diff = offset - (4 * IX_COUNT);
s.skipBytes(diff); ICUBinary.skipBytes(bytes, diff);
} }
int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK; int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
int totalSize = indexes[IX_TOTAL_SIZE] - offset; int totalSize = indexes[IX_TOTAL_SIZE] - offset;
@ -68,7 +68,7 @@ final class DictionaryData {
byte[] data = new byte[totalSize]; byte[] data = new byte[totalSize];
int i; int i;
for (i = 0; i < data.length; i++) { for (i = 0; i < data.length; i++) {
data[i] = s.readByte(); data[i] = bytes.get();
} }
Assert.assrt(i == totalSize); Assert.assrt(i == totalSize);
m = new BytesDictionaryMatcher(data, transform); m = new BytesDictionaryMatcher(data, transform);
@ -77,14 +77,12 @@ final class DictionaryData {
int num = totalSize / 2; int num = totalSize / 2;
char[] data = new char[totalSize / 2]; char[] data = new char[totalSize / 2];
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
data[i] = s.readChar(); data[i] = bytes.getChar();
} }
m = new CharsDictionaryMatcher(new String(data)); m = new CharsDictionaryMatcher(new String(data));
} else { } else {
m = null; m = null;
} }
s.close();
is.close();
return m; return m;
} }
} }