From 463c0aa31876073f284aec545ed128e23764d43c Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Tue, 15 Jul 2014 20:55:59 +0000 Subject: [PATCH] ICU-10944 Add ByteBuffer support for properties classes. Updates UBiDiProps, UCaseProps, UCharacterProperty and UPropertyAliases. R=markus.icu@gmail.com Review URL: https://codereview.appspot.com/112020044 X-SVN-Rev: 36038 --- .../core/src/com/ibm/icu/impl/UBiDiProps.java | 65 ++++++++---------- .../core/src/com/ibm/icu/impl/UCaseProps.java | 63 ++++++++--------- .../com/ibm/icu/impl/UCharacterProperty.java | 68 ++++++++----------- .../com/ibm/icu/impl/UPropertyAliases.java | 31 ++++----- 4 files changed, 103 insertions(+), 124 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/UBiDiProps.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/UBiDiProps.java index dd746d50dc..f4bf1d7e33 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/UBiDiProps.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/UBiDiProps.java @@ -1,28 +1,27 @@ /* -******************************************************************************* -* -* Copyright (C) 2004-2014, International Business Machines -* Corporation and others. All Rights Reserved. -* -******************************************************************************* -* file name: UBiDiProps.java -* encoding: US-ASCII -* tab size: 8 (not used) -* indentation:4 -* -* created on: 2005jan16 -* created by: Markus W. Scherer -* -* Low-level Unicode bidi/shaping properties access. -* Java port of ubidi_props.h/.c. -*/ + ******************************************************************************* + * + * Copyright (C) 2004-2014, International Business Machines + * Corporation and others. All Rights Reserved. + * + ******************************************************************************* + * file name: UBiDiProps.java + * encoding: US-ASCII + * tab size: 8 (not used) + * indentation:4 + * + * created on: 2005jan16 + * created by: Markus W. Scherer + * + * Low-level Unicode bidi/shaping properties access. + * Java port of ubidi_props.h/.c. + */ 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.Iterator; import com.ibm.icu.lang.UCharacter; @@ -36,21 +35,17 @@ public final class UBiDiProps { // port of ubidi_openProps() private UBiDiProps() throws IOException{ InputStream is=ICUData.getStream(ICUResourceBundle.ICU_BUNDLE+"/"+DATA_FILE_NAME); - BufferedInputStream b=new BufferedInputStream(is, 4096 /* data buffer size */); - readData(b); - b.close(); - is.close(); + ByteBuffer bytes=ICUBinary.getByteBufferFromInputStream(is); + readData(bytes); } - private void readData(InputStream is) throws IOException { - DataInputStream inputStream=new DataInputStream(is); - + private void readData(ByteBuffer bytes) throws IOException { // read the header - ICUBinary.readHeader(inputStream, FMT, new IsAcceptable()); + ICUBinary.readHeader(bytes, FMT, new IsAcceptable()); // read indexes[] int i, count; - count=inputStream.readInt(); + count=bytes.getInt(); if(countexpectedTrieLength) { throw new IOException(DATA_FILE_NAME+": not enough bytes for the trie"); } // skip padding after trie bytes - inputStream.skipBytes(expectedTrieLength-trieLength); + ICUBinary.skipBytes(bytes, expectedTrieLength-trieLength); // read mirrors[] count=indexes[IX_MIRROR_LENGTH]; if(count>0) { mirrors=new int[count]; for(i=0; iexpectedTrieLength) { throw new IOException(DATA_FILE_NAME+": not enough bytes for the trie"); } // skip padding after trie bytes - inputStream.skipBytes(expectedTrieLength-trieLength); + ICUBinary.skipBytes(bytes, expectedTrieLength-trieLength); // read exceptions[] count=indexes[IX_EXC_LENGTH]; if(count>0) { exceptions=new char[count]; for(i=0; i0) { unfold=new char[count]; for(i=0; i expectedTrieLength) { throw new IOException("uprops.icu: not enough bytes for main trie"); } // skip padding after trie bytes - ds.skipBytes(expectedTrieLength - trieLength); + ICUBinary.skipBytes(bytes, expectedTrieLength - trieLength); // skip unused intervening data structures - ds.skipBytes((additionalOffset - propertyOffset) * 4); + ICUBinary.skipBytes(bytes, (additionalOffset - propertyOffset) * 4); if(m_additionalColumnsCount_ > 0) { // reads the additional property block - m_additionalTrie_ = Trie2_16.createFromSerialized(ds); + m_additionalTrie_ = Trie2_16.createFromSerialized(bytes); expectedTrieLength = (additionalVectorsOffset-additionalOffset)*4; trieLength = m_additionalTrie_.getSerializedLength(); if(trieLength > expectedTrieLength) { throw new IOException("uprops.icu: not enough bytes for additional-properties trie"); } // skip padding after trie bytes - ds.skipBytes(expectedTrieLength - trieLength); + ICUBinary.skipBytes(bytes, expectedTrieLength - trieLength); // additional properties int size = scriptExtensionsOffset - additionalVectorsOffset; m_additionalVectors_ = new int[size]; for (int i = 0; i < size; i ++) { - m_additionalVectors_[i] = ds.readInt(); + m_additionalVectors_[i] = bytes.getInt(); } } @@ -1246,10 +1239,9 @@ public final class UCharacterProperty if(numChars > 0) { m_scriptExtensions_ = new char[numChars]; for(int i = 0; i < numChars; ++i) { - m_scriptExtensions_[i] = ds.readChar(); + m_scriptExtensions_[i] = bytes.getChar(); } } - is.close(); } private static final class IsAcceptable implements ICUBinary.Authenticate { @@ -1258,7 +1250,7 @@ public final class UCharacterProperty return version[0] == 7; } } - private static final byte DATA_FORMAT[] = { 0x55, 0x50, 0x72, 0x6F }; // "UPro" + private static final int DATA_FORMAT = 0x5550726F; // "UPro" // private methods ------------------------------------------------------- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/UPropertyAliases.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/UPropertyAliases.java index a69758d48d..23eee02246 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/UPropertyAliases.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/UPropertyAliases.java @@ -1,6 +1,6 @@ /* ********************************************************************** - * Copyright (c) 2002-2011, International Business Machines + * Copyright (c) 2002-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -12,10 +12,9 @@ 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.MissingResourceException; import com.ibm.icu.lang.UProperty; @@ -74,21 +73,19 @@ public final class UPropertyAliases { } } private static final IsAcceptable IS_ACCEPTABLE=new IsAcceptable(); - private static final byte DATA_FORMAT[]={ 0x70, 0x6E, 0x61, 0x6D }; // "pnam" + private static final int DATA_FORMAT=0x706E616D; // "pnam" - private void load(InputStream data) throws IOException { - BufferedInputStream bis=new BufferedInputStream(data); - //dataVersion=ICUBinary.readHeaderAndDataVersion(bis, DATA_FORMAT, IS_ACCEPTABLE); - ICUBinary.readHeader(bis, DATA_FORMAT, IS_ACCEPTABLE); - DataInputStream ds=new DataInputStream(bis); - int indexesLength=ds.readInt()/4; // inIndexes[IX_VALUE_MAPS_OFFSET]/4 + private void load(ByteBuffer bytes) throws IOException { + //dataVersion=ICUBinary.readHeaderAndDataVersion(bytes, DATA_FORMAT, IS_ACCEPTABLE); + ICUBinary.readHeader(bytes, DATA_FORMAT, IS_ACCEPTABLE); + int indexesLength=bytes.getInt()/4; // inIndexes[IX_VALUE_MAPS_OFFSET]/4 if(indexesLength<8) { // formatVersion 2 initially has 8 indexes throw new IOException("pnames.icu: not enough indexes"); } int[] inIndexes=new int[indexesLength]; inIndexes[0]=indexesLength*4; for(int i=1; i