在 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);
-----
摘自 草青工作室 的專欄