ICU-10944 Add ByteBuffer support for StringPrep.
Also adds the ICUBinary.getVersionByteArrayFromCompactInt() function. R=markus.icu@gmail.com Review URL: https://codereview.appspot.com/106580043 X-SVN-Rev: 36037
This commit is contained in:
parent
ffc1584fe7
commit
4aea6c889a
@ -261,6 +261,18 @@ public final class ICUBinary
|
||||
version >>> 24, (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the bytes in the compact version integer.
|
||||
*/
|
||||
public static byte[] getVersionByteArrayFromCompactInt(int version) {
|
||||
return new byte[] {
|
||||
(byte)(version >> 24),
|
||||
(byte)(version >> 16),
|
||||
(byte)(version >> 8),
|
||||
(byte)(version)
|
||||
};
|
||||
}
|
||||
|
||||
// private variables -------------------------------------------------
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
******************************************************************************
|
||||
* Copyright (C) 2003-2008, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
* Copyright (C) 2003-2014, International Business Machines Corporation and
|
||||
* others. All Rights Reserved.
|
||||
******************************************************************************
|
||||
*
|
||||
* Created on May 2, 2003
|
||||
@ -9,12 +9,11 @@
|
||||
* To change the template for this generated file go to
|
||||
* Window>Preferences>Java>Code Generation>Code and Comments
|
||||
*/
|
||||
|
||||
package com.ibm.icu.impl;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
|
||||
/**
|
||||
@ -25,38 +24,29 @@ import java.io.InputStream;
|
||||
*/
|
||||
public final class StringPrepDataReader implements ICUBinary.Authenticate {
|
||||
private final static boolean debug = ICUDebug.enabled("NormalizerDataReader");
|
||||
|
||||
|
||||
/**
|
||||
* <p>private constructor.</p>
|
||||
* @param inputStream ICU uprop.dat file input stream
|
||||
* @exception IOException throw if data file fails authentication
|
||||
* @param bytes ICU StringPrep data file buffer
|
||||
* @exception IOException throw if data file fails authentication
|
||||
*/
|
||||
public StringPrepDataReader(InputStream inputStream)
|
||||
public StringPrepDataReader(ByteBuffer bytes)
|
||||
throws IOException{
|
||||
if(debug) System.out.println("Bytes in inputStream " + inputStream.available());
|
||||
|
||||
unicodeVersion = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID, this);
|
||||
|
||||
if(debug) System.out.println("Bytes left in inputStream " +inputStream.available());
|
||||
|
||||
dataInputStream = new DataInputStream(inputStream);
|
||||
|
||||
if(debug) System.out.println("Bytes left in dataInputStream " +dataInputStream.available());
|
||||
}
|
||||
|
||||
public void read(byte[] idnaBytes,
|
||||
char[] mappingTable)
|
||||
throws IOException{
|
||||
if(debug) System.out.println("Bytes in buffer " + bytes.remaining());
|
||||
|
||||
//Read the bytes that make up the idnaTrie
|
||||
dataInputStream.readFully(idnaBytes);
|
||||
|
||||
byteBuffer = bytes;
|
||||
unicodeVersion = ICUBinary.readHeader(byteBuffer, DATA_FORMAT_ID, this);
|
||||
|
||||
if(debug) System.out.println("Bytes left in byteBuffer " + byteBuffer.remaining());
|
||||
}
|
||||
|
||||
public void read(char[] mappingTable) throws IOException{
|
||||
//Read the extra data
|
||||
for(int i=0;i<mappingTable.length;i++){
|
||||
mappingTable[i]=dataInputStream.readChar();
|
||||
mappingTable[i]=byteBuffer.getChar();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public byte[] getDataFormatVersion(){
|
||||
return DATA_FORMAT_VERSION;
|
||||
}
|
||||
@ -70,31 +60,29 @@ public final class StringPrepDataReader implements ICUBinary.Authenticate {
|
||||
int[] indexes = new int[length];
|
||||
//Read the indexes
|
||||
for (int i = 0; i <length ; i++) {
|
||||
indexes[i] = dataInputStream.readInt();
|
||||
indexes[i] = byteBuffer.getInt();
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
|
||||
public byte[] getUnicodeVersion(){
|
||||
return unicodeVersion;
|
||||
return ICUBinary.getVersionByteArrayFromCompactInt(unicodeVersion);
|
||||
}
|
||||
// private data members -------------------------------------------------
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ICU data file input stream
|
||||
*/
|
||||
private DataInputStream dataInputStream;
|
||||
private byte[] unicodeVersion;
|
||||
private ByteBuffer byteBuffer;
|
||||
private int unicodeVersion;
|
||||
/**
|
||||
* File format version that this class understands.
|
||||
* No guarantees are made if a older version is used
|
||||
* see store.c of gennorm for more information and values
|
||||
*/
|
||||
///* dataFormat="SPRP" 0x53, 0x50, 0x52, 0x50 */
|
||||
private static final byte DATA_FORMAT_ID[] = {(byte)0x53, (byte)0x50,
|
||||
(byte)0x52, (byte)0x50};
|
||||
private static final byte DATA_FORMAT_VERSION[] = {(byte)0x3, (byte)0x2,
|
||||
///* dataFormat="SPRP" 0x53, 0x50, 0x52, 0x50 */
|
||||
private static final int DATA_FORMAT_ID = 0x53505250;
|
||||
private static final byte DATA_FORMAT_VERSION[] = {(byte)0x3, (byte)0x2,
|
||||
(byte)0x5, (byte)0x2};
|
||||
|
||||
}
|
||||
|
@ -4,15 +4,16 @@
|
||||
* others. All Rights Reserved.
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
package com.ibm.icu.text;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.ibm.icu.impl.CharTrie;
|
||||
import com.ibm.icu.impl.ICUBinary;
|
||||
import com.ibm.icu.impl.ICUData;
|
||||
import com.ibm.icu.impl.ICUResourceBundle;
|
||||
import com.ibm.icu.impl.StringPrepDataReader;
|
||||
@ -209,7 +210,7 @@ public final class StringPrep {
|
||||
//private static final int MAX_INDEX_TOP_LENGTH = 0x0003;
|
||||
|
||||
/* indexes[] value names */
|
||||
private static final int INDEX_TRIE_SIZE = 0; /* number of bytes in normalization trie */
|
||||
// private static final int INDEX_TRIE_SIZE = 0; /* number of bytes in normalization trie */
|
||||
private static final int INDEX_MAPPING_DATA_SIZE = 1; /* The array that contains the mapping */
|
||||
private static final int NORM_CORRECTNS_LAST_UNI_VERSION = 2; /* The index of Unicode version of last entry in NormalizationCorrections.txt */
|
||||
private static final int ONE_UCHAR_MAPPING_INDEX_START = 3; /* The starting index of 1 UChar mapping index in the mapping data array */
|
||||
@ -220,11 +221,6 @@ public final class StringPrep {
|
||||
private static final int INDEX_TOP = 16; /* changing this requires a new formatVersion */
|
||||
|
||||
|
||||
/**
|
||||
* Default buffer size of datafile
|
||||
*/
|
||||
private static final int DATA_BUFFER_SIZE = 25000;
|
||||
|
||||
// CharTrie implmentation for reading the trie data
|
||||
private CharTrie sprepTrie;
|
||||
// Indexes read from the data file
|
||||
@ -257,43 +253,41 @@ public final class StringPrep {
|
||||
int major =(comp >> 24) & 0xFF;
|
||||
return VersionInfo.getInstance(major,minor,milli,micro);
|
||||
}
|
||||
|
||||
private static VersionInfo getVersionInfo(byte[] version){
|
||||
if(version.length != 4){
|
||||
return null;
|
||||
}
|
||||
return VersionInfo.getInstance((int)version[0],(int) version[1],(int) version[2],(int) version[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an StringPrep object after reading the input stream.
|
||||
* The object does not hold a reference to the input steam, so the stream can be
|
||||
* closed after the method returns.
|
||||
*
|
||||
* @param inputStream The stream for reading the StringPrep profile binarySun
|
||||
*
|
||||
* @param inputStream The stream for reading the StringPrep profile binarySun
|
||||
* @throws IOException An exception occurs when I/O of the inputstream is invalid
|
||||
* @stable ICU 2.8
|
||||
*/
|
||||
public StringPrep(InputStream inputStream) throws IOException{
|
||||
// TODO: Add a public constructor that takes ByteBuffer directly.
|
||||
ByteBuffer bytes = ICUBinary.getByteBufferFromInputStream(inputStream);
|
||||
StringPrepDataReader reader = new StringPrepDataReader(bytes);
|
||||
|
||||
BufferedInputStream b = new BufferedInputStream(inputStream,DATA_BUFFER_SIZE);
|
||||
|
||||
StringPrepDataReader reader = new StringPrepDataReader(b);
|
||||
|
||||
// read the indexes
|
||||
// read the indexes
|
||||
indexes = reader.readIndexes(INDEX_TOP);
|
||||
|
||||
byte[] sprepBytes = new byte[indexes[INDEX_TRIE_SIZE]];
|
||||
|
||||
|
||||
//indexes[INDEX_MAPPING_DATA_SIZE] store the size of mappingData in bytes
|
||||
mappingData = new char[indexes[INDEX_MAPPING_DATA_SIZE]/2];
|
||||
sprepTrie = new CharTrie(bytes, null);
|
||||
|
||||
//indexes[INDEX_MAPPING_DATA_SIZE] store the size of mappingData in bytes
|
||||
mappingData = new char[indexes[INDEX_MAPPING_DATA_SIZE]/2];
|
||||
// load the rest of the data data and initialize the data members
|
||||
reader.read(sprepBytes,mappingData);
|
||||
|
||||
sprepTrie = new CharTrie(new ByteArrayInputStream(sprepBytes), null);
|
||||
|
||||
// get the data format version
|
||||
reader.read(mappingData);
|
||||
|
||||
// get the data format version
|
||||
/*formatVersion = */reader.getDataFormatVersion();
|
||||
|
||||
|
||||
// get the options
|
||||
doNFKC = ((indexes[OPTIONS] & NORMALIZATION_ON) > 0);
|
||||
checkBiDi = ((indexes[OPTIONS] & CHECK_BIDI_ON) > 0);
|
||||
@ -306,13 +300,12 @@ public final class StringPrep {
|
||||
){
|
||||
throw new IOException("Normalization Correction version not supported");
|
||||
}
|
||||
b.close();
|
||||
|
||||
|
||||
if(checkBiDi) {
|
||||
bdp=UBiDiProps.INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a StringPrep instance for the specified profile
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user