在 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
请尊重原作者版权

最新文章

  1. 13.final关键字
  2. [OpenCV] IplImage and Functions
  3. Objective-c——UI基础开发第七天(自定义UITableView)
  4. HDU 5319 Painter
  5. JDK1.5新特性(一)&hellip;&hellip;Enhanced for Loop
  6. PHP + ajax 实现异步登录验证
  7. 微信cookie内容
  8. 激动啊,终于诞生了,编译了属于俺自己的 JDK
  9. IOS学习【VMware 12 安装 Mac OS X 10.11】-Day1
  10. 如何在仅主机模式下ping通网路上网
  11. React native和原生之间的通信
  12. MVCC 能解决幻读吗?
  13. 【UNIX环境高级编程】文件I/O
  14. 信步漫谈之Quartz&mdash;分布式调度(整合spring早期版本【低于spring3.1】)
  15. Java 过一下基础
  16. node.js读写文件
  17. EZ 2018 07 06 NOIP模拟赛
  18. C#设计模式--观察者模式(发布-订阅模式)
  19. ubantu 16.4 Hadoop 完全分布式搭建
  20. 有向图与无向图的合并操作区别D(递归与并查集)

热门文章

  1. Page Visibility(页面可见性) API介绍、微拓展[转]
  2. 理解 OpenStack Swift (1):OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置
  3. 151003-动起来-Javascript
  4. Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法
  5. svn恢复被删除的分支 svn del 分支
  6. iOS Block 内存管理的探讨
  7. Twitter Snowflake 的Java实现
  8. Sonatype Nexus Maven仓库搭建和管理
  9. smarty foreach循环
  10. [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.