在stream流和byte[]中查找(搜索)指定字符串

时间:2021-08-03 09:04:29

在 stream流 和 byte[] 中查找(搜索)指定字符串

这里注重看的是两个 Search 的扩展方法,一个是 stream 类型的扩展,另一个是 byte[] 类型的扩展,

如果大家有更好的“算法”,请给回复,我们一起优化!

-- 常用扩展代码,需要这部分代码的支持!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace Ims.Bll
{
  /// <summary>
  /// stream 、 string 、byte[] 间的转换扩展方法类
  /// </summary>
  public static class StreamExtend
  {
    #region Stream 扩展
    /// <summary>
    /// Stream Stream 转换为 byte 数组
    /// </summary>
    /// <returns></returns>
    public static byte[] ToByteArray(this Stream stream)
    {
      byte[] bytes = new byte[stream.Length];
      stream.Read(bytes, 0, bytes.Length);
      // 设置当前流的位置为流的开始
      stream.Seek(0, SeekOrigin.Begin);
      return bytes;
    }
    /// <summary>
    /// Stream 转换为 image 图片
    /// </summary>
    /// <returns></returns>
    public static Image ToImage(this Stream stream)
    {
      Image img = new Bitmap(stream);
      return img;
    }
    /// <summary>
    /// Stream 转换为 string ,使用 Encoding.Default 编码
    /// </summary>
    /// <returns></returns>
    public static string ToStr(this Stream stream)
    {
      return System.Text.Encoding.Default.GetString(stream.ToByteArray());
    }
    /// <summary>
    /// 在当前流中搜索指定的 byte[]
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="key">搜索关键字</param>
    /// <param name="beginPosition">搜索开始位置</param>
    /// <returns>如果存在则返回byte[]在流中首次出现的位置,否则返回 -1</returns>
    public static long Search(this Stream stream, long beginPosition, byte[] key)
    {
      if (stream == null || stream.Length <= beginPosition)
        return -1;

if (key == null || stream.Length < key.Length)
        return -1;

long i=-1;
      long j = -1;
      int currentByte = int.MinValue;
      for(i=beginPosition;i<stream.Length;i++)
      {
        if (stream.Length < key.Length + i)
          break;

stream.Seek(i, SeekOrigin.Begin);
        for (j = 0; j < key.Length; j++)
        {
          currentByte = stream.ReadByte();
          if (currentByte != key[j])
            break;
        }
        if (j == key.Length)
          return i;

if(currentByte == -1)
          break;
      }
      return -1;
    }
    #endregion

#region byte[] 扩展
    /// <summary>
    /// byte[] 转换为 stream 流
    /// </summary>
    /// <returns></returns>
    public static Stream ToStream(this byte[] arr)
    {
      Stream stream = new MemoryStream(arr);
      // 设置当前流的位置为流的开始 www.2cto.com
      stream.Seek(0, SeekOrigin.Begin);
      return stream;
    }
    /// <summary>
    /// byte[] 转换为 Image
    /// </summary>
    /// <returns></returns>
    public static Image ToImage(this byte[] arr)
    {
      return Image.FromStream(arr.ToStream());
    }
    /// <summary>
    /// 转换为 string,使用 Encoding.Default 编码
    /// </summary>
    /// <returns></returns>
    public static string ToStr(this byte[] arr)
    {
      return System.Text.Encoding.Default.GetString(arr);
    }
    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="key">搜索关键字</param>
    /// <param name="beginPos">搜索开始位置</param>
    /// <returns></returns>
    public static int Search(this byte[] arr, int beginPosition, byte[] key)
    {
      if (arr == null || arr.Length <= beginPosition)
        return -1;

if (key == null || arr.Length < key.Length)
        return -1;

int i = -1;
      int j = -1;
      for (i = beginPosition; i < arr.Length; i++)
      {
        if (arr.Length < key.Length + i)
          break;

for (j = 0; j < key.Length; j++)
        {
          if (arr[i+j] != key[j])
            break;
        }
        if (j == key.Length)
          return i;
      }
      return -1;
    }
    #endregion

#region string 扩展
    /// <summary>
    /// string 转换为 byte[]
    /// </summary>
    /// <returns></returns>
    public static byte[] ToByteArray(this string str)
    {
      return System.Text.Encoding.Default.GetBytes(str);
    }
    /// <summary>
    /// string 转换为 Stream
    /// </summary>
    /// <returns></returns>
    public static Stream ToStream(this string str)
    {
      Stream stream = new MemoryStream(str.ToByteArray());
      // 设置当前流的位置为流的开始
      stream.Seek(0, SeekOrigin.Begin);
      return stream;
    }
    #endregion
  }
}

------------------------

-- 测试脚本

byte[] arr = "0123456789111".ToByteArray();
      byte[] key1 = "123".ToByteArray();
      byte[] key2 = "678".ToByteArray();
      byte[] key3 = "911".ToByteArray();
      byte[] key4 = "111".ToByteArray();
      //流内搜索测试
      Stream sm = arr.ToStream();
      long index1 = sm.Search(0, key1);
      long index2 = sm.Search(0, key2);
      long index3 = sm.Search(0, key3);
      long index4 = sm.Search(0, key4);
      //byte[]内搜索测试
      long index10 = arr.Search(0, key1);
      long index20 = arr.Search(0, key2);
      long index30 = arr.Search(0, key3);
      long index40 = arr.Search(0, key4);

-----

摘自 草青工作室 的专栏

原文转自:http://www.2cto.com/kf/201204/129171.html
请尊重原作者版权