CmisCmdlets.ReadCmisDocumentCommand.ProcessRecord C# (CSharp) 메소드

ProcessRecord() 보호된 메소드

protected ProcessRecord ( ) : void
리턴 void
        protected override void ProcessRecord()
        {
            var doc = Document;
            if (doc == null)
            {
                doc = new CmisNavigation(CmisSession, WorkingFolder).GetDocument(Path);
            }
            var writeToPipeline = String.IsNullOrEmpty(Destination);
            var size = (long) doc.ContentStreamLength;

            // Do some security checks firsat if we should write the output to pipeline
            var msg = String.Format("The content is not plain text, but of type {0}. Do you want to"
                                    + "write it to pipeline anyway?", doc.ContentStreamMimeType);
            if (writeToPipeline && !IsPlaintextType(doc.ContentStreamMimeType) && !Force &&
                !ShouldContinue(msg, "Document Content Warning"))
            {
                return;
            }

            msg = String.Format("The document is pretty big (greater than {0:0.##} MB). " +
                "Are you sure that you want to write it to the pipeline anyway?",
                ((double)_pipelineMaxSize) / (1024 * 1024));
            if (writeToPipeline && size > _pipelineMaxSize && !Force &&
                !ShouldContinue(msg, "Big Document Warning"))
            {
                return;
            }

            msg = "The destination already exists. Do you want to overwrite that file?";
            if (!writeToPipeline && File.Exists(Destination) && !Force &&
                !ShouldContinue(msg, "Destination File Exists"))
            {
                return;
            }

            // TODO: better download mechanism anywhere
            byte[] stringBuffer = null;
            Stream outputStream;
            if (writeToPipeline)
            {
                stringBuffer = new byte[size];
                outputStream = new MemoryStream(stringBuffer);
            }
            else
            {
                outputStream = new FileStream(Destination, FileMode.Create);
            }

            IContentStream stream = null;
            try
            {
                var buffer = new byte[8 * 1024];
                int offset = 0;
                int bytesRead = 0;
                stream = doc.GetContentStream();

                while ((bytesRead = stream.Stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputStream.Write(buffer, 0, bytesRead);
                    offset += bytesRead;
                }
                outputStream.Flush();
            }
            finally
            {
                if (stream != null)
                {
                    stream.Stream.Close();
                }
                outputStream.Close();
            }

            if (writeToPipeline)
            {
                // TODO: support encodings
                WriteObject(Encoding.UTF8.GetString(stringBuffer));
            }
        }
ReadCmisDocumentCommand