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

在 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

{

///

/// stream 、 string 、byte[] 间的转换扩展方法类

///

public static class StreamExtend

{

#region Stream 扩展

///

/// Stream Stream 转换为 byte 数组

///

///

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;

}

///

/// Stream 转换为 image 图片

///

///

public static Image ToImage(this Stream stream)

{

Image img = new Bitmap(stream);

return img;

}

///

/// Stream 转换为 string ,使用 Encoding.Default 编码

///

///

public static string ToStr(this Stream stream)

{

return System.Text.Encoding.Default.GetString(stream.ToByteArray());

}

///

/// 在当前流中搜索指定的 byte[]

///

///

/// 搜索关键字

/// 搜索开始位置

/// 如果存在则返回byte[]在流中首次出现的位置,否则返回 -1

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

{

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[] 扩展

///

/// byte[] 转换为 stream 流

///

///

public static Stream ToStream(this byte[] arr)

{

Stream stream = new MemoryStream(arr);

// 设置当前流的位置为流的开始

stream.Seek(0, SeekOrigin.Begin);

return stream;

}

///

/// byte[] 转换为 Image

///

///

public static Image ToImage(this byte[] arr)

{

return Image.FromStream(arr.ToStream());

}

///

/// 转换为 string,使用 Encoding.Default 编码

///

///

public static string ToStr(this byte[] arr)

{

return System.Text.Encoding.Default.GetString(arr);

}

///

/// 搜索

///

///

/// 搜索关键字

/// 搜索开始位置

///

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 扩展

///

/// string 转换为 byte[]

///

///

public static byte[] ToByteArray(this string str)

{

return System.Text.Encoding.Default.GetBytes(str);

}

///

/// string 转换为 Stream

///

///

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);

-----

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-04-26 14:02

浏览 589

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值