Ejemplo n.º 1
0
        private static void Read(ReadState state)
        {
            Bytes data = state.GetRecordData();

            bool compressed = (state.OptionsFlags & 0x01) == 0;

            ushort bytesAvailable = (ushort)(data.Length - state.Offset);

            ushort bytesToRead;

            if (state.CharactersRead.Count < state.TotalCharacters)
            {
                ushort charBytesRemaining = (ushort)(state.TotalCharacters - state.CharactersRead.Count);
                if (!compressed)
                {
                    charBytesRemaining *= 2;
                }

                if (bytesAvailable < charBytesRemaining)
                {
                    bytesToRead = bytesAvailable;
                }
                else
                {
                    bytesToRead = charBytesRemaining;
                }

                byte[] charBytes = data.Get(state.Offset, bytesToRead).ByteArray;

                if (compressed)
                {
                    //decompress
                    byte[] wideBytes = new byte[charBytes.Length * 2];
                    for (int i = 0; i < charBytes.Length; i++)
                    {
                        wideBytes[2 * i] = charBytes[i];
                    }
                    charBytes = wideBytes;
                }
                state.Offset   += bytesToRead;
                bytesAvailable -= bytesToRead;

                state.CharactersRead.AddRange(Encoding.Unicode.GetChars(charBytes));
            }

            bool allCharsRead = state.CharactersRead.Count == state.TotalCharacters;

            if (state.HasRichTextSettings && bytesAvailable > 0 && allCharsRead &&
                state.FormattingRunBytes.Count < (state.FormattingRunCount * 4))
            {
                bytesToRead = Math.Min(bytesAvailable, (ushort)Math.Min(state.FormattingRunCount * 4, ushort.MaxValue));
                state.FormattingRunBytes.AddRange(data.Get(state.Offset, bytesToRead).ByteArray);
                state.Offset   += bytesToRead;
                bytesAvailable -= bytesToRead;
            }

            if (state.HasAsianPhonetics && bytesAvailable > 0 && allCharsRead &&
                state.PhoneticSettingsBytes.Count < state.PhoneticSettingsByteCount)
            {
                bytesToRead = Math.Min(bytesAvailable, (ushort)Math.Min(state.PhoneticSettingsByteCount, ushort.MaxValue));
                state.PhoneticSettingsBytes.AddRange(data.Get(state.Offset, bytesToRead).ByteArray);
                state.Offset   += bytesToRead;
                bytesAvailable -= bytesToRead;
            }

            if (state.CharactersRead.Count < state.TotalCharacters ||
                state.FormattingRunBytes.Count < (state.FormattingRunCount * 4) ||
                state.PhoneticSettingsBytes.Count < state.PhoneticSettingsByteCount)
            {
                state.Continue(true);
                Read(state);
            }
            else if (bytesAvailable == 0 && (state.ContinueIndex + 1) < state.Record.Continues.Count)
            {
                state.Continue(false);
            }
        }
Ejemplo n.º 2
0
        private static void Read(ReadState state)
        {
            Bytes data = state.GetRecordData();

            bool compressed = (state.OptionsFlags & 0x01) == 0;

            ushort bytesAvailable = (ushort)(data.Length - state.Offset);

            ushort bytesToRead;
            if (state.CharactersRead.Count < state.TotalCharacters)
            {
                ushort charBytesRemaining = (ushort)(state.TotalCharacters - state.CharactersRead.Count);
                if (!compressed)
                    charBytesRemaining *= 2;

                if (bytesAvailable < charBytesRemaining)
                    bytesToRead = bytesAvailable;
                else
                    bytesToRead = charBytesRemaining;

                byte[] charBytes = data.Get(state.Offset, bytesToRead).ByteArray;

                if (compressed)
                {
                    //decompress
                    byte[] wideBytes = new byte[charBytes.Length * 2];
                    for (int i = 0; i < charBytes.Length; i++)
                        wideBytes[2 * i] = charBytes[i];
                    charBytes = wideBytes;
                }
                state.Offset += bytesToRead;
                bytesAvailable -= bytesToRead;

                state.CharactersRead.AddRange(Encoding.Unicode.GetChars(charBytes));
            }

            bool allCharsRead = state.CharactersRead.Count == state.TotalCharacters;

            if (state.HasRichTextSettings && bytesAvailable > 0 && allCharsRead &&
                state.FormattingRunBytes.Count < (state.FormattingRunCount * 4))
            {
                bytesToRead = Math.Min(bytesAvailable, (ushort) Math.Min(state.FormattingRunCount*4, ushort.MaxValue));
                state.FormattingRunBytes.AddRange(data.Get(state.Offset, bytesToRead).ByteArray);
                state.Offset += bytesToRead;
                bytesAvailable -= bytesToRead;
            }

            if (state.HasAsianPhonetics && bytesAvailable > 0 && allCharsRead &&
                state.PhoneticSettingsBytes.Count < state.PhoneticSettingsByteCount)
            {
                bytesToRead = Math.Min(bytesAvailable, (ushort)Math.Min(state.PhoneticSettingsByteCount, ushort.MaxValue));
                state.PhoneticSettingsBytes.AddRange(data.Get(state.Offset, bytesToRead).ByteArray);
                state.Offset += bytesToRead;
                bytesAvailable -= bytesToRead;
            }

            if (state.CharactersRead.Count < state.TotalCharacters ||
                state.FormattingRunBytes.Count < (state.FormattingRunCount * 4) ||
                state.PhoneticSettingsBytes.Count < state.PhoneticSettingsByteCount)
            {
                state.Continue(true);
                Read(state);
            }
            else if (bytesAvailable == 0 && (state.ContinueIndex + 1) < state.Record.Continues.Count)
            {
                state.Continue(false);
            }
        }