private void ReadData(Stream stream) { DataInputStream ds = new DataInputStream(stream); /* if (DEBUG) * System.Console.Out.WriteLine("The DataInputStream class is: " + ds.GetType().FullName); + if (DEBUG) + System.Console.Out + .WriteLine("The available bytes in the stream before reading the data: " + ds.Available()); */ /* * The following will read two integers before ds.mark(). Later, the two * integers need to be placed into data[], then ds.reset(), then * ds.readFully(into rest of data[]). * * This is necessary because we don't know the readLimit for ds.mark() * until we have read the second integer (indexLength). */ rootRes = ds.ReadInt(); // read the variable-length indexes[] array int indexLength = ds.ReadInt(); ds.Mark((indexLength - 1) * 4); indexes = new int[indexLength]; indexes[URES_INDEX_LENGTH] = indexLength; for (int i = 1; i < indexLength; i++) { indexes[i] = ds.ReadInt(); } // determine if this resource bundle falls back to a parent bundle // along normal locale ID fallback noFallback = indexLength > URES_INDEX_ATTRIBUTES && (indexes[URES_INDEX_ATTRIBUTES] & URES_ATT_NO_FALLBACK) != 0; // read the entire bundle (after the header) into data[] // put rootRes and indexLength into data[0..7] // and the rest of the data into data[8..length-1] int length = indexes[URES_INDEX_BUNDLE_TOP] * 4; if (DEBUG) { System.Console.Out.WriteLine("The number of bytes in the bundle: " + length); } data = new sbyte[length]; WriteInt(rootRes, data, 0); WriteInt(indexLength, data, 4); // now reset to the mark, which was set after reading rootRes and // indexLength ds.Reset(); ds.ReadFully(data, 8, length - 8); }
/// <summary>Check an fsimage datainputstream's version number.</summary> /// <remarks> /// Check an fsimage datainputstream's version number. /// The datainput stream is returned at the same point as it was passed in; /// this method has no effect on the datainputstream's read pointer. /// </remarks> /// <param name="in">Datainputstream of fsimage</param> /// <returns>Filesystem layout version of fsimage represented by stream</returns> /// <exception cref="System.IO.IOException">If problem reading from in</exception> private int FindImageVersion(DataInputStream @in) { @in.Mark(42); // arbitrary amount, resetting immediately int version = @in.ReadInt(); @in.Reset(); return(version); }
/* * Get an RBBIDataWrapper from an InputStream onto a pre-compiled set of * RBBI rules. */ static internal RBBIDataWrapper Get(Stream mask0) { int i; DataInputStream dis = new DataInputStream(new BufferedStream(mask0)); RBBIDataWrapper This = new RBBIDataWrapper(); // Seek past the ICU data header. // TODO: verify that the header looks good. dis.SkipBytes(0x80); // Read in the RBBI data header... This.fHeader = new RBBIDataWrapper.RBBIDataHeader(); This.fHeader.fMagic = dis.ReadInt(); This.fHeader.fVersion = dis.ReadInt(); This.fHeader.fFormatVersion[0] = (byte)(This.fHeader.fVersion >> 24); This.fHeader.fFormatVersion[1] = (byte)(This.fHeader.fVersion >> 16); This.fHeader.fFormatVersion[2] = (byte)(This.fHeader.fVersion >> 8); This.fHeader.fFormatVersion[3] = (byte)(This.fHeader.fVersion); This.fHeader.fLength = dis.ReadInt(); This.fHeader.fCatCount = dis.ReadInt(); This.fHeader.fFTable = dis.ReadInt(); This.fHeader.fFTableLen = dis.ReadInt(); This.fHeader.fRTable = dis.ReadInt(); This.fHeader.fRTableLen = dis.ReadInt(); This.fHeader.fSFTable = dis.ReadInt(); This.fHeader.fSFTableLen = dis.ReadInt(); This.fHeader.fSRTable = dis.ReadInt(); This.fHeader.fSRTableLen = dis.ReadInt(); This.fHeader.fTrie = dis.ReadInt(); This.fHeader.fTrieLen = dis.ReadInt(); This.fHeader.fRuleSource = dis.ReadInt(); This.fHeader.fRuleSourceLen = dis.ReadInt(); This.fHeader.fStatusTable = dis.ReadInt(); This.fHeader.fStatusTableLen = dis.ReadInt(); dis.SkipBytes(6 * 4); // uint32_t fReserved[6]; if (This.fHeader.fMagic != 0xb1a0 || !(This.fHeader.fVersion == 1 || // ICU // 3.2 // and // earlier This.fHeader.fFormatVersion[0] == 3) // ICU 3.4 ) { throw new IOException( "Break Iterator Rule Data Magic Number Incorrect, or unsupported data version."); } // Current position in input stream. int pos = 24 * 4; // offset of end of header, which has 24 fields, all // int32_t (4 bytes) // // Read in the Forward state transition table as an array of shorts. // // Quick Sanity Check if (This.fHeader.fFTable < pos || This.fHeader.fFTable > This.fHeader.fLength) { throw new IOException("Break iterator Rule data corrupt"); } // Skip over any padding preceding this table dis.SkipBytes(This.fHeader.fFTable - pos); pos = This.fHeader.fFTable; This.fFTable = new short[This.fHeader.fFTableLen / 2]; for (i = 0; i < This.fFTable.Length; i++) { This.fFTable[i] = dis.ReadShort(); pos += 2; } // // Read in the Reverse state table // // Skip over any padding in the file dis.SkipBytes(This.fHeader.fRTable - pos); pos = This.fHeader.fRTable; // Create & fill the table itself. This.fRTable = new short[This.fHeader.fRTableLen / 2]; for (i = 0; i < This.fRTable.Length; i++) { This.fRTable[i] = dis.ReadShort(); pos += 2; } // // Read in the Safe Forward state table // if (This.fHeader.fSFTableLen > 0) { // Skip over any padding in the file dis.SkipBytes(This.fHeader.fSFTable - pos); pos = This.fHeader.fSFTable; // Create & fill the table itself. This.fSFTable = new short[This.fHeader.fSFTableLen / 2]; for (i = 0; i < This.fSFTable.Length; i++) { This.fSFTable[i] = dis.ReadShort(); pos += 2; } } // // Read in the Safe Reverse state table // if (This.fHeader.fSRTableLen > 0) { // Skip over any padding in the file dis.SkipBytes(This.fHeader.fSRTable - pos); pos = This.fHeader.fSRTable; // Create & fill the table itself. This.fSRTable = new short[This.fHeader.fSRTableLen / 2]; for (i = 0; i < This.fSRTable.Length; i++) { This.fSRTable[i] = dis.ReadShort(); pos += 2; } } // // Unserialize the Character categories TRIE // Because we can't be absolutely certain where the Trie deserialize // will // leave the input stream, leave position unchanged. // The seek to the start of the next item following the TRIE will get us // back in sync. // dis.SkipBytes(This.fHeader.fTrie - pos); // seek input stream from end of // previous section to pos = This.fHeader.fTrie; // to the start of the trie dis.Mark(This.fHeader.fTrieLen + 100); // Mark position of start of TRIE // in the input // and tell Java to keep the mark // valid so long // as we don't go more than 100 // bytes past the // past the end of the TRIE. This.fTrie = new CharTrie(dis, fTrieFoldingFunc); // Deserialize the // TRIE, leaving input // stream at an unknown position, preceding the // padding between TRIE and following section. dis.Reset(); // Move input stream back to marked position at // the start of the serialized TRIE. Now our // "pos" variable and the input stream are in // agreement. // // Read the Rule Status Table // if (pos > This.fHeader.fStatusTable) { throw new IOException("Break iterator Rule data corrupt"); } dis.SkipBytes(This.fHeader.fStatusTable - pos); pos = This.fHeader.fStatusTable; This.fStatusTable = new int[This.fHeader.fStatusTableLen / 4]; for (i = 0; i < This.fStatusTable.Length; i++) { This.fStatusTable[i] = dis.ReadInt(); pos += 4; } // // Put the break rule source into a String // if (pos > This.fHeader.fRuleSource) { throw new IOException("Break iterator Rule data corrupt"); } dis.SkipBytes(This.fHeader.fRuleSource - pos); pos = This.fHeader.fRuleSource; StringBuilder sb = new StringBuilder(This.fHeader.fRuleSourceLen / 2); for (i = 0; i < This.fHeader.fRuleSourceLen; i += 2) { sb.Append(dis.ReadChar()); pos += 2; } This.fRuleSource = sb.ToString(); if (IBM.ICU.Text.RuleBasedBreakIterator.fDebugEnv != null && IBM.ICU.Text.RuleBasedBreakIterator.fDebugEnv.IndexOf("data") >= 0) { This.Dump(); } return(This); }
// ---------------------------------------------------------------- // Constructor /// <summary> /// Constructs a UPropertyAliases object. The binary file DATA_FILE_NAME is /// read from the jar/classpath and unflattened into member variables of this /// object. /// </summary> /// public UPropertyAliases() { // Open the .icu file from the jar/classpath Stream mask0 = IBM.ICU.Impl.ICUData.GetRequiredStream(DATA_FILE_NAME); BufferedStream b = new BufferedStream(mask0, DATA_BUFFER_SIZE); // Read and discard Unicode version... /* byte unicodeVersion[] = */ IBM.ICU.Impl.ICUBinary.ReadHeader(b, DATA_FORMAT_ID, this); DataInputStream d = new DataInputStream(b); // Record the origin position of the file. Keep enough around // to seek back to the start of the header. d.Mark(256); short enumToName_offset = d.ReadShort(); short nameToEnum_offset = d.ReadShort(); short enumToValue_offset = d.ReadShort(); short total_size = d.ReadShort(); short valueMap_offset = d.ReadShort(); short valueMap_count = d.ReadShort(); short nameGroupPool_offset = d.ReadShort(); short nameGroupPool_count = d.ReadShort(); short stringPool_offset = d.ReadShort(); short stringPool_count = d.ReadShort(); if (DEBUG) { System.Console.Out.WriteLine("enumToName_offset=" + enumToName_offset + "\n" + "nameToEnum_offset=" + nameToEnum_offset + "\n" + "enumToValue_offset=" + enumToValue_offset + "\n" + "total_size=" + total_size + "\n" + "valueMap_offset=" + valueMap_offset + "\n" + "valueMap_count=" + valueMap_count + "\n" + "nameGroupPool_offset=" + nameGroupPool_offset + "\n" + "nameGroupPool_count=" + nameGroupPool_count + "\n" + "stringPool_offset=" + stringPool_offset + "\n" + "stringPool_count=" + stringPool_count); } byte[] raw = new byte[total_size]; d.Reset(); d.ReadFully(raw); d.Close(); UPropertyAliases.Builder builder = new UPropertyAliases.Builder(raw); stringPool = builder .ReadStringPool(stringPool_offset, stringPool_count); nameGroupPool = builder.ReadNameGroupPool(nameGroupPool_offset, nameGroupPool_count); builder.SetupValueMap_map(valueMap_offset, valueMap_count); // Some of the following data structures have to be set up // here, _not_ in Builder. That's because they are instances // of non-static inner classes, and they contain implicit // references to this. builder.Seek(enumToName_offset); enumToName = new UPropertyAliases.NonContiguousEnumToShort(builder); builder.NameGroupOffsetToIndex(enumToName.offsetArray); builder.Seek(nameToEnum_offset); nameToEnum = new UPropertyAliases.NameToEnum(this, builder); builder.Seek(enumToValue_offset); enumToValue = new UPropertyAliases.NonContiguousEnumToShort(builder); builder.ValueMapOffsetToIndex(enumToValue.offsetArray); valueMapArray = new UPropertyAliases.ValueMap [valueMap_count]; for (int i = 0; i < valueMap_count; ++i) { // Must seek to the start of each entry. builder.Seek(builder.valueMap_map[i]); valueMapArray[i] = new UPropertyAliases.ValueMap(this, builder); } builder.Close(); }