简体   繁体   English

在System.IO.Stream中搜索模式

[英]Pattern search in a System.IO.Stream

I am receiving System IO Streams from a source. 我从源接收系统IO流。 I will proceed with the stream object only if it contains the string "MSTND" . 只有当它包含字符串"MSTND"我才会继续使用流对象。

I realize there is not much I can do on the stream unless I convert it into string. 我意识到除非我将它转换为字符串,否则我在流上无能为力。 The string conversion is only for sub-string matching. 字符串转换仅用于子字符串匹配。 But I don't want to do anything that takes up lot of time or space. 但我不想做任何占用大量时间或空间的事情。 How time / space intensive is a conversion from Stream to string just for sub-string matching? 时间/空间密集是如何从Stream转换为字符串仅用于子字符串匹配?

The code I have written is: 我写的代码是:

private bool StreamHasString (Stream vStream)
{
     bool containsStr = false;
     byte[] streamBytes = new byte[vStream.Length];
     vStream.Read( streamBytes, 0, (int) vStream.Length);
     string stringOfStream = Encoding.UTF32.GetString(streamBytes);
     if (stringOfStream.Contains("MSTND"))
     {
        containsStr = true;
     }     
     return containsStr ;
}

What you are doing would work fine, but once you have read the stream into a string, you could just return the string so that you don't have to read the stream again. 你正在做的事情可以正常工作,但是一旦你将流读入字符串,你可以只返回字符串,这样你就不必再次读取流。

Note also that you are using the Read method wrong. 另请注意,您使用的是Read方法错误。 It returns the number of bytes read intot he array, because it doesn't have to return as many bytes as you requested, even if it's not at the end of the stream. 它返回在数组中读取的字节数,因为它不必返回您请求的字节数,即使它不在流的末尾。 You have to loop until you have read all the bytes from the stream. 您必须循环,直到您已从流中读取所有字节。

private string StreamHasString (Stream vStream) {
  byte[] streamBytes = new byte[vStream.Length];

  int pos = 0;
  int len = (int)vStream.Length;
  while (pos < len) {
    int n = vStream.Read(streamBytes, pos, len - pos);
    pos += n;
  }

  string stringOfStream = Encoding.UTF32.GetString(streamBytes);
  if (stringOfStream.Contains("MSTND")) {
    return stringOfStream;
  } else {
    return null;
  }
}

Usage: 用法:

string s = StreamHasString(vStream);
if (s != null) {
  // proceed
}

Depending on where in the stream you're expecting this sequence it would be fairly efficient to convert to a string to perform the substring. 根据您期望此序列的流中的位置,转换为字符串以执行子字符串将非常有效。 If its in a standard spot each time then you can read through the number of bytes required and convert them to a string. 如果每次都在标准位置,那么您可以读取所需的字节数并将它们转换为字符串。

Take a look at this for some reference: http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx 看看这个以供参考: http//msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

Alternatively you could convert the string "MSTND" to a byte[] and search the stream for the byte[]. 或者,您可以将字符串“MSTND”转换为byte []并在流中搜索byte []。

Edit: 编辑:

I found How do I get a consistent byte representation of strings in C# without manually specifying an encoding? 我发现如何在不手动指定编码的情况下在C#中获得字符串的一致字节表示? which should help with converting the string to byte[]. 这应该有助于将字符串转换为byte []。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM