コード例 #1
0
        /// <summary>Encodes the given unprintable char codes in the given stream.</summary>
        /// <remarks>Encodes the given unprintable char codes in the given stream.</remarks>
        /// <exception cref="System.IO.IOException"></exception>
        private static void WriteUnprintableCodes(int charOffset, byte[] bytes, ByteUtil.ByteStream
                                                  unprintableCodes, GeneralLegacyIndexCodes.ExtraCodesStream extraCodes)
        {
            // the offset seems to be calculated based on the number of bytes in the
            // "extra codes" part of the entry (even if there are no extra codes bytes
            // actually written in the final entry).
            int unprintCharOffset = charOffset;

            if (extraCodes != null)
            {
                // we need to account for some extra codes which have not been written
                // yet.  additionally, any unprintable bytes added to the beginning of
                // the extra codes are ignored.
                unprintCharOffset = extraCodes.GetLength() + (charOffset - extraCodes.GetNumChars
                                                                  ()) - extraCodes.GetUnprintablePrefixLen();
            }
            // we write a whacky combo of bytes for each unprintable char which
            // includes a funky offset and extra char itself
            int offset = (UNPRINTABLE_COUNT_START + (UNPRINTABLE_COUNT_MULTIPLIER * unprintCharOffset
                                                     )) | UNPRINTABLE_OFFSET_FLAGS;

            // write offset as big-endian short
            unprintableCodes.Write((offset >> 8) & unchecked ((int)(0xFF)));
            unprintableCodes.Write(offset & unchecked ((int)(0xFF)));
            unprintableCodes.Write(UNPRINTABLE_MIDFIX);
            unprintableCodes.Write(bytes);
        }
コード例 #2
0
        /// <summary>Encodes the given extra code info in the given stream.</summary>
        /// <remarks>Encodes the given extra code info in the given stream.</remarks>
        /// <exception cref="System.IO.IOException"></exception>
        private static void WriteExtraCodes(int charOffset, byte[] bytes, byte extraCodeModifier
                                            , GeneralLegacyIndexCodes.ExtraCodesStream extraCodes)
        {
            // we fill in a placeholder value for any chars w/out extra codes
            int numChars = extraCodes.GetNumChars();

            if (numChars < charOffset)
            {
                int fillChars = charOffset - numChars;
                extraCodes.WriteFill(fillChars, INTERNATIONAL_EXTRA_PLACEHOLDER);
                extraCodes.IncrementNumChars(fillChars);
            }
            if (bytes != null)
            {
                // write the actual extra codes and update the number of chars
                extraCodes.Write(bytes);
                extraCodes.IncrementNumChars(1);
            }
            else
            {
                // extra code modifiers modify the existing extra code bytes and do not
                // count as additional extra code chars
                int lastIdx = extraCodes.GetLength() - 1;
                if (lastIdx >= 0)
                {
                    // the extra code modifier is added to the last extra code written
                    byte lastByte = extraCodes.Get(lastIdx);
                    lastByte += extraCodeModifier;
                    extraCodes.Set(lastIdx, lastByte);
                }
                else
                {
                    // there is no previous extra code, add a new code (but keep track of
                    // this "unprintable code" prefix)
                    extraCodes.Write(extraCodeModifier);
                    extraCodes.SetUnprintablePrefixLen(1);
                }
            }
        }