在stream流和byte[]中查找(搜索)指定字符串
2016-09-02 19:54
288 查看
在 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
请尊重原作者版权
这里注重看的是两个 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
请尊重原作者版权
相关文章推荐
- 在 stream流 和 byte[] 中查找(搜索)指定字符串
- 在 stream流 和 byte[] 中查找(搜索)指定字符串
- 查找包含指定字符串的文件
- BOM查找指定子项的所有父项,并以字符串的形式输出
- Python 在目录中查找指定的字符串 python完成分析log的工作
- PHP在字符串中查找指定字符串并删除
- 一个在文本文件搜索指定字符串的程序
- 生成搜索风格的字符串即关键字用指定颜色并适当剪裁字符串
- 查找包含指定字符串的所有脚本对象
- 在vim中使用查找命令查找指定字符串
- 查找该目录下指定文件类型的用 vc 6.0 编写 的关于文件搜索的例子
- 在一个10M的文本文件中搜索指定字符串
- 搜索当前数据库中含有指定字符串的表列
- 在vim中使用查找命令查找指定字符串
- 【实用】用EditPlus在指定“文件夹”中搜索“字符串”
- 指定行号后查找字符串
- 从目录中查找指定后缀文件内的指定字符串
- 查找字符串中指定字符出现的次数:性能比较
- 在目录中查找指定的字符串
- 在指定路径或者是文件名查找指定的字符串