From d875ce368adf00ef57385c555548698699677415 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Tue, 19 Jan 2010 23:02:01 +0000 Subject: [PATCH] ICU-7273 port Normalizer2Impl.load() to Java X-SVN-Rev: 27336 --- .../src/com/ibm/icu/impl/Normalizer2Impl.java | 75 +++++++++++++++++-- .../core/src/com/ibm/icu/impl/Trie2_16.java | 14 +++- .../core/src/com/ibm/icu/impl/Trie2_32.java | 14 +++- .../src/com/ibm/icu/text/Normalizer2.java | 5 +- 4 files changed, 92 insertions(+), 16 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/Normalizer2Impl.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/Normalizer2Impl.java index b64976174a..93fa92fe23 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/Normalizer2Impl.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/Normalizer2Impl.java @@ -6,9 +6,12 @@ */ package com.ibm.icu.impl; +import java.io.BufferedInputStream; +import java.io.DataInputStream; import java.io.InputStream; +import java.io.IOException; -import com.ibm.icu.impl.Trie2; +import com.ibm.icu.impl.ICUBinary; import com.ibm.icu.impl.Trie2_16; import com.ibm.icu.text.Normalizer; import com.ibm.icu.text.Normalizer2; @@ -65,7 +68,7 @@ class Normalizer2Impl { return 3; } } - }; + } public static final class ReorderingBuffer { public ReorderingBuffer(Normalizer2Impl ni, StringBuilder dest) { @@ -215,12 +218,72 @@ class Normalizer2Impl { } private int codePointStart, codePointLimit; - }; + } public Normalizer2Impl() {} - public final void load(InputStream data, String name) { - // TODO + private static final class Reader implements ICUBinary.Authenticate { + // @Override when we switch to Java 6 + public boolean isDataVersionAcceptable(byte version[]) { + return version[0]==1; + } + public VersionInfo readHeader(InputStream data) throws IOException { + byte[] dataVersion=ICUBinary.readHeader(data, DATA_FORMAT, this); + return VersionInfo.getInstance(dataVersion[0], dataVersion[1], + dataVersion[2], dataVersion[3]); + } + private static final byte DATA_FORMAT[] = { 0x4e, 0x72, 0x6d, 0x32 }; // "Nrm2" + } + private static final Reader READER=new Reader(); + public final void load(InputStream data) throws IOException { + BufferedInputStream bis=new BufferedInputStream(data); + dataVersion=READER.readHeader(bis); + DataInputStream ds=new DataInputStream(bis); + int indexesLength=ds.readInt()/4; // inIndexes[IX_NORM_TRIE_OFFSET]/4 + if(indexesLength<=IX_MIN_MAYBE_YES) { + throw new IOException("Normalizer2 data: not enough indexes"); + } + int[] inIndexes=new int[indexesLength]; + inIndexes[0]=indexesLength*4; + for(int i=1; i(nextOffset-offset)) { + throw new IOException("Normalizer2 data: not enough bytes for normTrie"); + } + ds.skipBytes((nextOffset-offset)-trieLength); // skip padding after trie bytes + + // Read the composition and mapping data. + offset=nextOffset; + nextOffset=inIndexes[IX_RESERVED2_OFFSET]; + int numChars=(nextOffset-offset)/2; + char[] chars; + if(numChars!=0) { + chars=new char[numChars]; + for(int i=0; i