コード例 #1
0
        public static void RedactCharacters(Doc doc, IList <TextFragment> fragments)
        {
            if (fragments.Count == 0)
            {
                return;
            }
            ResourceTracker tracker = new ResourceTracker(doc);
            Dictionary <int, List <TextFragment> > streams = new Dictionary <int, List <TextFragment> >();

            foreach (TextFragment fragment in fragments)
            {
                List <TextFragment> redactions = null;
                int id = tracker.CopyOnWrite(fragment.StreamID, fragment.PageID);
                streams.TryGetValue(id, out redactions);
                if (redactions == null)
                {
                    redactions  = new List <TextFragment>();
                    streams[id] = redactions;
                }
                redactions.Add(fragment);
            }
            foreach (KeyValuePair <int, List <TextFragment> > pair in streams)
            {
                // get data
                StreamObject stream = doc.ObjectSoup[pair.Key] as StreamObject;
                Debug.Assert(stream != null);             // should never happen
                stream.Decompress();                      // shouldn't really be necessary
                Debug.Assert(stream.Compressed == false); // should never happen
                byte[] data = stream.GetData();
                // construct new stream
                List <TextFragment> redactions = pair.Value;
                redactions.Sort((x, y) => x.StreamOffset.CompareTo(y.StreamOffset));
                MemoryStream mem = new MemoryStream(data.Length);
                int          p   = 0;
                for (int i = 0; i < redactions.Count; i++)
                {
                    TextFragment   redaction = redactions[i];
                    RedactionGroup group     = new RedactionGroup(data);
                    for (int j = i; j < redactions.Count; j++)
                    {
                        if (!group.Add(redactions[j]))
                        {
                            i = j - 1;
                            break;
                        }
                    }
                    if (p < redaction.StreamOffset)
                    {
                        mem.Write(data, p, redaction.StreamOffset - p);
                        byte[] tjBytes = group.GetTextOperator().GetData();
                        mem.Write(tjBytes, 1, tjBytes.Length - 2);
                    }
                    p = redaction.StreamOffset + redaction.StreamLength;
                }
                mem.Write(data, p, data.Length - p);
                stream.SetData(mem.ToArray());
            }
        }
コード例 #2
0
        /// <summary>
        /// Deserialize sub response data from byte array.
        /// </summary>
        /// <param name="byteArray">The byte array which contains sub response data.</param>
        /// <param name="currentIndex">The index special where to start.</param>
        protected override void DeserializeSubResponseDataFromByteArray(byte[] byteArray, ref int currentIndex)
        {
            int index = currentIndex;

            this.ReadAccessResponse  = StreamObject.GetCurrent <ReadAccessResponse>(byteArray, ref index);
            this.WriteAccessResponse = StreamObject.GetCurrent <WriteAccessResponse>(byteArray, ref index);

            currentIndex = index;
        }
コード例 #3
0
        private void Write_Callback(IAsyncResult ar)
        {
            StreamObject so = (StreamObject)ar.AsyncState;

            so.stream.EndWrite(ar);
            //if (OutputStringReceived != null)
            //    OutputStringReceived(so.id, StreamType.Output, so.text);
            so.stream.Flush();
        }
コード例 #4
0
        /// <summary>
        /// Deserialize items from byte array.
        /// </summary>
        /// <param name="byteArray">The byte array which contains response message.</param>
        /// <param name="currentIndex">The index special where to start.</param>
        /// <param name="lengthOfItems">The length of items.</param>
        protected override void DeserializeItemsFromByteArray(byte[] byteArray, ref int currentIndex, int lengthOfItems)
        {
            if (lengthOfItems != 0)
            {
                throw new ResponseParseErrorException(currentIndex, "ReadAccessResponse over-parse error", null);
            }

            int index = currentIndex;

            this.ReadResponseError = StreamObject.GetCurrent <ResponseError>(byteArray, ref index);
            currentIndex           = index;
        }
コード例 #5
0
    public void Test()
    {
        var          writeStream = new MemoryStream();
        long         beginLength = 0;
        long         endLength   = 0;
        EventHandler begin       = (o, e) => { beginLength = writeStream.Length; Console.WriteLine(string.Format("Begin serialization of Data, writeStream.Length = {0}", writeStream.Length)); };
        EventHandler end         = (o, e) => { endLength = writeStream.Length;  Console.WriteLine(string.Format("End serialization of Data, writeStream.Length = {0}", writeStream.Length)); };

        StreamObject.OnDataReadBegin += begin;
        StreamObject.OnDataReadEnd   += end;
        try
        {
            int length      = 1000000;
            var inputStream = new MemoryStream();
            for (int i = 0; i < length; i++)
            {
                inputStream.WriteByte(unchecked ((byte)i));
            }
            inputStream.Position = 0;
            var streamObject = new StreamObject(inputStream);
            Serializer.Serialize(writeStream, streamObject);
            var          data = writeStream.ToArray();
            StreamObject newStreamObject;
            using (var s = new MemoryStream(data))
            {
                newStreamObject = Serializer.Deserialize <StreamObject>(s);
            }
            if (beginLength >= endLength)
            {
                throw new InvalidOperationException("inputStream was completely buffered before writing to writeStream");
            }
            inputStream.Position = 0;
            newStreamObject.StreamProperty.Position = 0;
            if (!inputStream.AsEnumerable().SequenceEqual(newStreamObject.StreamProperty.AsEnumerable()))
            {
                throw new InvalidOperationException("!inputStream.AsEnumerable().SequenceEqual(newStreamObject.StreamProperty.AsEnumerable())");
            }
            else
            {
                Console.WriteLine("Streams identical.");
            }
        }
        finally
        {
            StreamObject.OnDataReadBegin -= begin;
            StreamObject.OnDataReadEnd   -= end;
        }
    }
コード例 #6
0
        private StreamObject UncachedCopyOnWrite(StreamObject so, Page page)
        {
            // this function must always be called from a function which caches the results
            if (_identity)
            {
                return(so);
            }
            Dictionary <Page, int> entry1 = null;

            _pages.TryGetValue(so, out entry1);
            if (entry1 != null)
            {
                int          index = entry1[page];
                StreamObject copy  = (StreamObject)so.Clone();
                _doc.ObjectSoup.Add(copy);
                ArrayAtom array = page.Resolve(Atom.GetItem(page.Atom, "Contents")) as ArrayAtom;
                if (array != null)
                {
                    Debug.Assert(array.Count > index);
                    array[index] = new RefAtom(copy);
                }
                else
                {
                    Debug.Assert(index == 0);
                    Atom.SetItem(page.Atom, "Contents", new RefAtom(copy));
                }
                entry1.Remove(page);
                if (entry1.Count <= 1)
                {
                    _pages.Remove(so);
                }
                return(copy);
            }
            Dictionary <Page, Tuple <DictAtom, string> > entry2 = null;

            _xobjs.TryGetValue(so, out entry2);
            if (entry2 != null)
            {
                Tuple <DictAtom, string> index = entry2[page];
                Debug.Assert(so is FormXObject);
                StreamObject copy = (StreamObject)so.Clone();
                _doc.ObjectSoup.Add(copy);
                index.Item1[index.Item2] = new RefAtom(copy);
                entry2.Remove(page);
                return(copy);
            }
            return(so);
        }
コード例 #7
0
        public void WritelnStringToProcess(string id, string data)
        {
            Utils.ProcessRunner pr = StartedProcesses[id] as Utils.ProcessRunner;

            /*string s=StartedProcesses.Count.ToString()+" ";
             * foreach (string ss in StartedProcesses.Keys)
             *  s += ss + "|";
             * if (pr == null) throw new Exception("pr==null, " + id + " " + data+" started process"+s);*/
            byte[]       buffer = InputEncoding.GetBytes(data + pr.process.StandardInput.NewLine);
            StreamObject so     = new StreamObject();

            so.stream = pr.process.StandardInput.BaseStream;
            so.text   = data + Environment.NewLine;
            so.id     = id;
            pr.process.StandardInput.BaseStream.BeginWrite(buffer, 0, buffer.Length, Write_Callback, so);
        }
コード例 #8
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (!connectionSucessfull)
            {
                MessageBox.Show("No message will be sent. Connection has not been made to server", "Connection Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            String s = textMessage.Text;
            var    o = new StreamObject();

            o.ClientId = textBoxClientId.Text;
            o.Message  = textMessage.Text;

            asynchronousClient.Send(ConversionHelper.ObjectToString(o), false);
            asynchronousClient.sendDone.WaitOne();
        }
コード例 #9
0
 public void Delete()
 {
     foreach (KeyValuePair <int, List <ImageRendition> > pair in _redactions)
     {
         StreamObject          so         = (StreamObject)_doc.ObjectSoup[pair.Key];
         List <ImageRendition> renditions = pair.Value;
         renditions.Sort((i1, i2) => { return(i1.StreamOffset.CompareTo(i2.StreamOffset)); });
         if (!so.Decompress())
         {
             throw new ArgumentException("Unable to decompress stream.");
         }
         byte[] data = so.GetData();
         foreach (ImageRendition rendition in renditions)
         {
             for (int i = 0; i < rendition.StreamLength; i++)
             {
                 data[i + rendition.StreamOffset] = 0x20;                         // space
             }
         }
         so.SetData(data);
         so.CompressFlate();
     }
 }
コード例 #10
0
        public ResourceTracker(Doc doc)
        {
            _doc   = doc;
            _pages = new Dictionary <StreamObject, Dictionary <Page, int> >();
            _xobjs = new Dictionary <StreamObject, Dictionary <Page, Tuple <DictAtom, string> > >();
            _copyOnWriteCacheID = new Dictionary <int, Dictionary <int, int> >();
            // assemble all resources which may be shared
            foreach (Page page in doc.ObjectSoup.Catalog.Pages.GetPageArrayAll())
            {
                StreamObject[] layers = page.GetLayers();
                for (int i = 0; i < layers.Length; i++)
                {
                    StreamObject           so    = layers[i];
                    Dictionary <Page, int> entry = null;
                    _pages.TryGetValue(so, out entry);
                    if (entry == null)
                    {
                        entry      = new Dictionary <Page, int>();
                        _pages[so] = entry;
                    }
                    Debug.Assert(!entry.ContainsKey(page));
                    entry[page] = i;
                }
                IList <DictAtom> resourceDicts = page.GetResourceDictsByType("XObject", true, true, true, true, new HashSet <int>());
                foreach (DictAtom resources in resourceDicts)
                {
                    foreach (var resource in resources)
                    {
                        FormXObject formxobj = page.ResolveObj(resource.Value) as FormXObject;
                        if (formxobj == null)
                        {
                            continue;
                        }
                        Dictionary <Page, Tuple <DictAtom, string> > entry = null;
                        _xobjs.TryGetValue(formxobj, out entry);
                        if (entry == null)
                        {
                            entry            = new Dictionary <Page, Tuple <DictAtom, string> >();
                            _xobjs[formxobj] = entry;
                        }
                        entry[page] = new Tuple <DictAtom, string>(resources, resource.Key);
                    }
                }
            }
            // remove unique entries just to simplify things
            List <StreamObject> unique = new List <StreamObject>();

            foreach (var pair in _pages)
            {
                if (pair.Value.Count <= 1)
                {
                    unique.Add(pair.Key);
                }
            }
            foreach (var item in unique)
            {
                _pages.Remove(item);
            }
            unique.Clear();
            foreach (var pair in _xobjs)
            {
                if (pair.Value.Count <= 1)
                {
                    unique.Add(pair.Key);
                }
            }
            foreach (var item in unique)
            {
                _xobjs.Remove(item);
            }
            _identity = (_pages.Count == 0) && (_xobjs.Count == 0);
        }
コード例 #11
0
        public static void RedactTextOps(Doc doc, IList <TextFragment> fragments)
        {
            if (fragments.Count == 0)
            {
                return;
            }
            ResourceTracker tracker = new ResourceTracker(doc);
            Dictionary <int, List <TextFragment> > streams = new Dictionary <int, List <TextFragment> >();

            foreach (TextFragment fragment in fragments)
            {
                List <TextFragment> redactions = null;
                int id = tracker.CopyOnWrite(fragment.StreamID, fragment.PageID);
                streams.TryGetValue(id, out redactions);
                if (redactions == null)
                {
                    redactions  = new List <TextFragment>();
                    streams[id] = redactions;
                }
                redactions.Add(fragment);
            }
            foreach (KeyValuePair <int, List <TextFragment> > pair in streams)
            {
                // get data
                StreamObject stream = doc.ObjectSoup[pair.Key] as StreamObject;
                Debug.Assert(stream != null);             // should never happen
                stream.Decompress();                      // shouldn't really be necessary
                Debug.Assert(stream.Compressed == false); // should never happen
                byte[] data = stream.GetData();
                // construct new stream
                List <TextFragment> redactions = pair.Value;
                redactions.Sort((x, y) => x.StreamOffset.CompareTo(y.StreamOffset));
                MemoryStream mem = new MemoryStream(data.Length);
                int          p   = 0;
                foreach (TextFragment redaction in redactions)
                {
                    if (p < redaction.StreamOffset)
                    {
                        mem.Write(data, p, redaction.StreamOffset - p);
                        IDictionary <char, int> widths = redaction.Font.Widths;
                        int defaultWidth = doc.GetInfoInt(redaction.Font.ID, "/DescendantFonts[0]*/DW*");
                        if (defaultWidth == 0)
                        {
                            defaultWidth = 1000;
                        }
                        int width1000ths = 0;
                        foreach (char c in redaction.Text)
                        {
                            int width = 0;
                            if (!widths.TryGetValue(c, out width))
                            {
                                width = defaultWidth;
                            }
                            width1000ths += defaultWidth;
                        }
                        string tj      = string.Format("[{0}] TJ\r\n", width1000ths);
                        byte[] tjBytes = ASCIIEncoding.ASCII.GetBytes(tj);
                        mem.Write(tjBytes, 0, tjBytes.Length);
                    }
                    p = redaction.StreamOffset + redaction.StreamLength;
                }
                mem.Write(data, p, data.Length - p);
                stream.SetData(mem.ToArray());
            }
        }
コード例 #12
0
ファイル: PdfOutput.cs プロジェクト: mrdrbob/NFountain
        private bool WriteObject(StreamObject obj)
        {
            if (obj == null)
                return false;

            var options = new DictionaryObject()
                .Set("Length", obj.Length)
                /*
                .Set("Filters", new ArrayObject()
                     .Add(new NameObject("ASCII85Decode"))
                     .Add(new NameObject("LZWDecode")))
                */
                ;
            WriteObject(options);
            WriteLine().WriteLine("stream");

            /*
            StringWriter w = new StringWriter();
            PdfOutput inner = new PdfOutput(w);
            inner.WriteStream(obj.Value);
            string raw = w.ToString();

            // byte[] compressed = LZW.Encode(raw);
            var lzwByteStream = LZW.ToByteStream(raw);

            w = new StringWriter();

            Ascii85.Encode(lzwByteStream, w);

            int start = _writer.Position;
            _writer.Write(w);
            _writer.WriteLine();
            int length = _writer.Position - start;
            // */

            // /*
            int start = _writer.Position;
            WriteStream(obj.Value);
            int length = _writer.Position - start;
            // */

            Write("endstream");

            obj.Length.Get<IntegerNumberObject>().Value = length;

            return true;
        }
コード例 #13
0
ファイル: RunManager.cs プロジェクト: lisiynos/pascalabcnet
 public void WritelnStringToProcess(string id, string data)
 {
     Utils.ProcessRunner pr = StartedProcesses[id] as Utils.ProcessRunner;
     /*string s=StartedProcesses.Count.ToString()+" ";
     foreach (string ss in StartedProcesses.Keys)
         s += ss + "|";
     if (pr == null) throw new Exception("pr==null, " + id + " " + data+" started process"+s);*/
     byte[] buffer = InputEncoding.GetBytes(data + pr.process.StandardInput.NewLine);
     StreamObject so=new StreamObject();
     so.stream = pr.process.StandardInput.BaseStream;
     so.text = data + Environment.NewLine;
     so.id = id;
     pr.process.StandardInput.BaseStream.BeginWrite(buffer, 0, buffer.Length, Write_Callback, so);
 }