Beispiel #1
0
 public static IProcessor Create(EngineConfig config, IReadOnlyList <IOperationProvider> operations)
 {
     return(new Processor(config, operations));
 }
Beispiel #2
0
 private Processor(EngineConfig config, IReadOnlyList <IOperationProvider> operations)
 {
     Config      = config;
     _operations = operations;
 }
Beispiel #3
0
 public static IProcessor Create(EngineConfig config, params IOperationProvider[] operations)
 {
     return(new Processor(config, operations));
 }
Beispiel #4
0
        public ProcessorState(Stream source, Stream target, int bufferSize, int flushThreshold, EngineConfig config, IReadOnlyList <IOperationProvider> operationProviders)
        {
            bool sizedToStream = false;

            //Buffer has to be at least as large as the largest BOM we could expect
            if (bufferSize < 4)
            {
                bufferSize = 4;
            }
            else
            {
                try
                {
                    if (source.Length < bufferSize)
                    {
                        sizedToStream = true;
                        bufferSize    = (int)source.Length;
                    }
                }
                catch
                {
                    //The stream may not support getting the length property (in NetworkStream for instance, which throw a NotSupportedException), suppress any errors in
                    //  accessing the property and continue with the specified buffer size
                }
            }

            _source             = source;
            _target             = target;
            Config              = config;
            _flushThreshold     = flushThreshold;
            CurrentBuffer       = new byte[bufferSize];
            CurrentBufferLength = source.Read(CurrentBuffer, 0, CurrentBuffer.Length);

            byte[]   bom;
            Encoding encoding = EncodingUtil.Detect(CurrentBuffer, CurrentBufferLength, out bom);

            Encoding = encoding;
            CurrentBufferPosition = bom.Length;
            target.Write(bom, 0, bom.Length);

            Dictionary <Encoding, Trie> byEncoding;

            if (!TrieLookup.TryGetValue(operationProviders, out byEncoding))
            {
                TrieLookup[operationProviders] = byEncoding = new Dictionary <Encoding, Trie>();
            }

            if (!byEncoding.TryGetValue(encoding, out _trie))
            {
                List <IOperation> operations = new List <IOperation>(operationProviders.Count);

                for (int i = 0; i < operationProviders.Count; ++i)
                {
                    IOperation op = operationProviders[i].GetOperation(encoding, this);
                    if (op != null)
                    {
                        operations.Add(op);
                    }
                }

                byEncoding[encoding] = _trie = Trie.Create(operations);
            }

            if (bufferSize < _trie.MaxLength && !sizedToStream)
            {
                byte[] tmp = new byte[_trie.MaxLength];
                Buffer.BlockCopy(CurrentBuffer, CurrentBufferPosition, tmp, 0, CurrentBufferLength - CurrentBufferPosition);
                int nRead = _source.Read(tmp, CurrentBufferLength - CurrentBufferPosition, tmp.Length - CurrentBufferLength);
                CurrentBuffer         = tmp;
                CurrentBufferLength  += nRead;
                CurrentBufferPosition = 0;
            }
        }