Example #1
0
        private void BackupForRestart(byte[] buffer, int offset, int count, int fileOffset, bool force)
        {
            InternalDebug.Assert(restartConsumer != null);

            if (!force && fileOffset > restartMax)
            {
                restartConsumer.DisableRestart();
                restartConsumer = null;

                preamble = null;
                return;
            }

            if (restartCache == null)
            {
                restartCache = new ByteCache();
            }

            byte[] cacheBuffer;
            int    cacheOffset;

            restartCache.GetBuffer(count, out cacheBuffer, out cacheOffset);

            Buffer.BlockCopy(buffer, offset, cacheBuffer, cacheOffset, count);

            restartCache.Commit(count);
        }
        private void EncodeBuffer(char[] buffer, int offset, int count, bool flush)
        {
            var maxSpaceRequired = encoding.GetMaxByteCount(count);

            byte[] outputBuffer, directBuffer = null;
            int    outputOffset, directOffset = 0;
            int    outputCount, directSpace = 0;
            var    encodingToCache = true;



            if (canRestart || pullSink == null || cache.Length != 0)
            {
                cache.GetBuffer(maxSpaceRequired, out outputBuffer, out outputOffset);
            }
            else
            {
                pullSink.GetOutputBuffer(out directBuffer, out directOffset, out directSpace);

                if (directSpace >= maxSpaceRequired)
                {
                    outputBuffer = directBuffer;
                    outputOffset = directOffset;

                    encodingToCache = false;
                }
                else
                {
                    cache.GetBuffer(maxSpaceRequired, out outputBuffer, out outputOffset);
                }
            }

            var encodedCount = encoder.GetBytes(buffer, offset, count, outputBuffer, outputOffset, flush);

            if (encodingToCache)
            {
                cache.Commit(encodedCount);

                if (pullSink == null)
                {
                    if (!canRestart || restartablePushSink)
                    {
                        while (cache.Length != 0)
                        {
                            cache.GetData(out outputBuffer, out outputOffset, out outputCount);

                            pushSink.Write(outputBuffer, outputOffset, outputCount);

                            cache.ReportRead(outputCount);

                            InternalDebug.Assert(outputCount > 0);
                        }
                    }
                }
                else
                {
                    if (!canRestart)
                    {
                        encodedCount = cache.Read(directBuffer, directOffset, directSpace);

                        pullSink.ReportOutput(encodedCount);
                    }
                }
            }
            else
            {
                pullSink.ReportOutput(encodedCount);
            }
        }