捕获来自Process.StandardOutput二进制输出 [英] Capturing binary output from Process.StandardOutput

查看:1309
本文介绍了捕获来自Process.StandardOutput二进制输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#(.NET 4.0在SuSE 2.8单下运行)我想运行一个外部批处理命令和捕获其输出中以二进制形式。我使用的外部工具被称为samtools(samtools.sourceforge.net)以及除其他事项外可以从所谓的BAM索引二进制文件格式返回记录。

In C# (.NET 4.0 running under Mono 2.8 on SuSE) I would like to run an external batch command and capture its ouput in binary form. The external tool I use is called 'samtools' (samtools.sourceforge.net) and among other things it can return records from an indexed binary file format called BAM.

我使用的Process.Start运行外部命令,我知道我可以通过重定向Process.StandardOutput捕捉它的输出。现在的问题是,这是与编码文本流,因此它不给我访问输出的原始字节。几乎-工作液,我发现是访问底层流。

I use Process.Start to run the external command, and I know that I can capture its output by redirecting Process.StandardOutput. The problem is, that's a text stream with an encoding, so it doesn't give me access to the raw bytes of the output. The almost-working solution I found is to access the underlying stream.

下面是我的code:

        Process cmdProcess = new Process();
        ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
        cmdStartInfo.FileName = "samtools";

        cmdStartInfo.RedirectStandardError = true;
        cmdStartInfo.RedirectStandardOutput = true;
        cmdStartInfo.RedirectStandardInput = false;
        cmdStartInfo.UseShellExecute = false;
        cmdStartInfo.CreateNoWindow = true;

        cmdStartInfo.Arguments = "view -u " + BamFileName + " " + chromosome + ":" + start + "-" + end;

        cmdProcess.EnableRaisingEvents = true;
        cmdProcess.StartInfo = cmdStartInfo;
        cmdProcess.Start();

        // Prepare to read each alignment (binary)
        var br = new BinaryReader(cmdProcess.StandardOutput.BaseStream);

        while (!cmdProcess.StandardOutput.EndOfStream)
        {
            // Consume the initial, undocumented BAM data 
            br.ReadBytes(23);

// ...更多解析如下

// ... more parsing follows

但是当我运行这一点,我读的第一23bytes不是第一次23个字节的输出中,而是地方几百或几千个字节的下游。我假设的StreamReader做一些缓冲等底层流已经提前说4K到输出。底层流不支持查找回到开始。

But when I run this, the first 23bytes that I read are not the first 23 bytes in the ouput, but rather somewhere several hundred or thousand bytes downstream. I assume that StreamReader does some buffering and so the underlying stream is already advanced say 4K into the output. The underlying stream does not support seeking back to the start.

和我困在这里。有没有人有运行一个外部命令,并以二进制形式捕捉它的标准输出工作的解决方案?在输出中可能是非常大的,所以我想它流

And I'm stuck here. Does anyone have a working solution for running an external command and capturing its stdout in binary form? The ouput may be very large so I would like to stream it.

任何帮助AP preciated。

Any help appreciated.

顺便说一句,我目前的解决方法是让samtools文本格式返回的记录,然后解析这些,但这是pretty慢,我希望通过直接使用二进制格式来加快速度。

By the way, my current workaround is to have samtools return the records in text format, then parse those, but this is pretty slow and I'm hoping to speed things up by using the binary format directly.

推荐答案

使用 StandardOutput.BaseStream 是正确的做法,但你不能使用任何其他属性或方法 cmdProcess.StandardOutput 。例如,访问 cmdProcess.StandardOutput.EndOfStream 将导致的StreamReader StandardOutput 读取流的一部分,消除您要访问的数据。

Using StandardOutput.BaseStream is the correct approach, but you must not use any other property or method of cmdProcess.StandardOutput. For example, accessing cmdProcess.StandardOutput.EndOfStream will cause the StreamReader for StandardOutput to read part of the stream, removing the data you want to access.

相反,简单地读取并解析 BR (假设你知道如何解析数据中的数据,而不会看过去流的末尾,还是愿意赶上一个 EndOfStreamException )。另外,如果你不知道数据有多大,使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.stream.copyto.aspx\"><$c$c>Stream.CopyTo在整个标准输出流复制到一个新文件或内存流。

Instead, simply read and parse the data from br (assuming you know how to parse the data, and won't read past the end of stream, or are willing to catch an EndOfStreamException). Alternatively, if you don't know how big the data is, use Stream.CopyTo to copy the entire standard output stream to a new file or memory stream.

这篇关于捕获来自Process.StandardOutput二进制输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆