From f58b3f8fd7dde508ecabe04e8eac1d690804193a Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Tue, 15 Jul 2014 20:32:17 +0000 Subject: [PATCH] ICU-10944 Add ByteBuffer support for Normalizer2Impl. R=markus.icu@gmail.com Review URL: https://codereview.appspot.com/106530045 X-SVN-Rev: 36034 --- .../src/com/ibm/icu/impl/Norm2AllModes.java | 29 +++++------ .../src/com/ibm/icu/impl/Normalizer2Impl.java | 50 +++++++++---------- .../src/com/ibm/icu/text/Normalizer2.java | 27 +++++++--- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/Norm2AllModes.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/Norm2AllModes.java index f50f1cfc069..4a69d36d25e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/Norm2AllModes.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/Norm2AllModes.java @@ -1,13 +1,14 @@ /* -******************************************************************************* -* Copyright (C) 2009-2014, International Business Machines -* Corporation and others. All Rights Reserved. -******************************************************************************* -*/ + ******************************************************************************* + * Copyright (C) 2009-2014, International Business Machines + * Corporation and others. All Rights Reserved. + ******************************************************************************* + */ + package com.ibm.icu.impl; import java.io.IOException; -import java.io.InputStream; +import java.nio.ByteBuffer; import com.ibm.icu.text.Normalizer; import com.ibm.icu.text.Normalizer2; @@ -318,8 +319,8 @@ public final class Norm2AllModes { default: return null; } } - public static Norm2AllModes getInstance(InputStream data, String name) { - if(data==null) { + public static Norm2AllModes getInstance(ByteBuffer bytes, String name) { + if(bytes==null) { Norm2AllModesSingleton singleton; if(name.equals("nfc")) { singleton=NFCSingleton.INSTANCE; @@ -337,16 +338,16 @@ public final class Norm2AllModes { return singleton.allModes; } } - return cache.getInstance(name, data); + return cache.getInstance(name, bytes); } - private static CacheBase cache = - new SoftCache() { - protected Norm2AllModes createInstance(String key, InputStream data) { + private static CacheBase cache = + new SoftCache() { + protected Norm2AllModes createInstance(String key, ByteBuffer bytes) { Normalizer2Impl impl; - if(data==null) { + if(bytes==null) { impl=new Normalizer2Impl().load(ICUResourceBundle.ICU_BUNDLE+"/"+key+".nrm"); } else { - impl=new Normalizer2Impl().load(data); + impl=new Normalizer2Impl().load(bytes); } return new Norm2AllModes(impl); } 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 0959a1251dc..33f07000729 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 @@ -1,15 +1,14 @@ /* -******************************************************************************* -* Copyright (C) 2009-2014, International Business Machines -* Corporation and others. All Rights Reserved. -******************************************************************************* -*/ + ******************************************************************************* + * Copyright (C) 2009-2014, International Business Machines + * Corporation and others. All Rights Reserved. + ******************************************************************************* + */ + package com.ibm.icu.impl; -import java.io.BufferedInputStream; -import java.io.DataInputStream; import java.io.IOException; -import java.io.InputStream; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Iterator; @@ -417,42 +416,40 @@ public final class Normalizer2Impl { } } private static final IsAcceptable IS_ACCEPTABLE = new IsAcceptable(); - private static final byte DATA_FORMAT[] = { 0x4e, 0x72, 0x6d, 0x32 }; // "Nrm2" + private static final int DATA_FORMAT = 0x4e726d32; // "Nrm2" - public Normalizer2Impl load(InputStream data) { + public Normalizer2Impl load(ByteBuffer bytes) { try { - BufferedInputStream bis=new BufferedInputStream(data); - dataVersion=ICUBinary.readHeaderAndDataVersion(bis, DATA_FORMAT, IS_ACCEPTABLE); - DataInputStream ds=new DataInputStream(bis); - int indexesLength=ds.readInt()/4; // inIndexes[IX_NORM_TRIE_OFFSET]/4 + dataVersion=ICUBinary.readHeaderAndDataVersion(bytes, DATA_FORMAT, IS_ACCEPTABLE); + int indexesLength=bytes.getInt()/4; // inIndexes[IX_NORM_TRIE_OFFSET]/4 if(indexesLength<=IX_MIN_MAYBE_YES) { throw new ICUUncheckedIOException("Normalizer2 data: not enough indexes"); } int[] inIndexes=new int[indexesLength]; inIndexes[0]=indexesLength*4; for(int i=1; i(nextOffset-offset)) { throw new ICUUncheckedIOException("Normalizer2 data: not enough bytes for normTrie"); } - ds.skipBytes((nextOffset-offset)-trieLength); // skip padding after trie bytes - + ICUBinary.skipBytes(bytes, (nextOffset-offset)-trieLength); // skip padding after trie bytes + // Read the composition and mapping data. offset=nextOffset; nextOffset=inIndexes[IX_SMALL_FCD_OFFSET]; @@ -461,7 +458,7 @@ public final class Normalizer2Impl { if(numChars!=0) { chars=new char[numChars]; for(int i=0; iAny {@link java.io.IOException} is wrapped into a {@link com.ibm.icu.util.ICUUncheckedIOException}. * @param data the binary, big-endian normalization (.nrm file) data, or null for ICU data * @param name "nfc" or "nfkc" or "nfkc_cf" or name of custom data file * @param mode normalization mode (compose or decompose etc.) @@ -182,7 +188,16 @@ public abstract class Normalizer2 { * @stable ICU 4.4 */ public static Normalizer2 getInstance(InputStream data, String name, Mode mode) { - Norm2AllModes all2Modes=Norm2AllModes.getInstance(data, name); + // TODO: If callers really use this API, then we should add an overload that takes a ByteBuffer. + ByteBuffer bytes = null; + if (data != null) { + try { + bytes = ICUBinary.getByteBufferFromInputStream(data); + } catch (IOException e) { + throw new ICUUncheckedIOException(e); + } + } + Norm2AllModes all2Modes=Norm2AllModes.getInstance(bytes, name); switch(mode) { case COMPOSE: return all2Modes.comp; case DECOMPOSE: return all2Modes.decomp;