Ejemplo n.º 1
0
        private static void WriteContent(ByteCounterStreamWriter builder, string content, ContextObject context)
        {
            content = content ?? context.Options.Null;

            var sourceCount = builder.BytesWritten;

            if (context.Options.MaxSize == 0)
            {
                builder.Write(content);
                return;
            }

            if (sourceCount >= context.Options.MaxSize - 1)
            {
                builder.ReachedLimit = true;
                return;
            }

            var cl = context.Options.Encoding.GetByteCount(content);

            var overflow = sourceCount + cl - context.Options.MaxSize;

            if (overflow <= 0)
            {
                //builder.BaseStream.Write(binaryContent, 0, binaryContent.Length);
                builder.Write(content, cl);
                return;
            }

            if (overflow < content.Length)
            {
                builder.Write(content.ToCharArray(0, (int)(cl - overflow)), cl - overflow);
            }
            else
            {
                builder.Write(content, cl);
            }
        }
Ejemplo n.º 2
0
        private static void WriteContent(ByteCounterStreamWriter builder, string content, ContextObject context)
        {
            content = content ?? context.Options.Null;

            var sourceCount = builder.BytesWritten;

            if (context.Options.MaxSize == 0)
            {
                builder.Write(content);
                return;
            }

            if (sourceCount >= context.Options.MaxSize - 1)
            {
                builder.ReachedLimit = true;
                return;
            }
            //TODO this is a performance critical operation. As we might deal with variable-length encodings this cannot be compute initial
            var cl = context.Options.Encoding.GetByteCount(content);

            var overflow = sourceCount + cl - context.Options.MaxSize;

            if (overflow <= 0)
            {
                builder.Write(content, cl);
                return;
            }

            if (overflow < content.Length)
            {
                builder.Write(content.ToCharArray(0, (int)(cl - overflow)), cl - overflow);
            }
            else
            {
                builder.Write(content, cl);
            }
        }