Find (Search) the specified string in stream and byte []

Source: Internet
Author: User

Find (Search) the specified string in stream and byte []

Here we will focus on two Search extension methods: stream extension and byte [] extension,

If you have a better "algorithm", please reply and we will optimize it together!

 

-- Common extension code, which must be supported by this part of code!

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

Namespace Ims. Bll
{
/// <Summary>
/// Conversion extension method class between stream, string, and byte []
/// </Summary>
Public static class StreamExtend
{
# Region Stream Extension
/// <Summary>
/// Stream to byte array
/// </Summary>
/// <Returns> </returns>
Public static byte [] ToByteArray (this Stream stream)
{
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, SeekOrigin. Begin );
Return bytes;
}
/// <Summary>
/// Stream to image
/// </Summary>
/// <Returns> </returns>
Public static Image ToImage (this Stream stream)
{
Image img = new Bitmap (stream );
Return img;
}
/// <Summary>
/// Stream to string, encoded using Encoding. Default
/// </Summary>
/// <Returns> </returns>
Public static string ToStr (this Stream stream)
{
Return System. Text. Encoding. Default. GetString (stream. ToByteArray ());
}
/// <Summary>
/// Search for the specified byte in the current stream []
/// </Summary>
/// <Param name = "arr"> </param>
/// <Param name = "key"> Search Keyword </param>
/// <Param name = "beginPosition"> Start position </param>
/// <Returns> If yes, return the location where byte [] appears for the first time in the stream; otherwise, return-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 [] extension
/// <Summary>
/// Byte [] to stream
/// </Summary>
/// <Returns> </returns>
Public static Stream ToStream (this byte [] arr)
{
Stream stream = new MemoryStream (arr );
// Set the starting position of the current stream to www.2cto.com.
Stream. Seek (0, SeekOrigin. Begin );
Return stream;
}
/// <Summary>
/// Byte [] to Image
/// </Summary>
/// <Returns> </returns>
Public static Image ToImage (this byte [] arr)
{
Return Image. FromStream (arr. ToStream ());
}
/// <Summary>
/// Convert to string and use Encoding. Default Encoding
/// </Summary>
/// <Returns> </returns>
Public static string ToStr (this byte [] arr)
{
Return System. Text. Encoding. Default. GetString (arr );
}
/// <Summary>
/// Search
/// </Summary>
/// <Param name = "arr"> </param>
/// <Param name = "key"> Search Keyword </param>
/// <Param name = "beginPos"> Start position </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 extension
/// <Summary>
/// Convert string to byte []
/// </Summary>
/// <Returns> </returns>
Public static byte [] ToByteArray (this string str)
{
Return System. Text. Encoding. Default. GetBytes (str );
}
/// <Summary>
/// String to Stream
/// </Summary>
/// <Returns> </returns>
Public static Stream ToStream (this string str)
{
Stream stream = new MemoryStream (str. ToByteArray ());
// Set the current stream position to the beginning of the stream
Stream. Seek (0, SeekOrigin. Begin );
Return stream;
}
# Endregion
}
}

 

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

-- Test script

Byte [] arr = "0123456789111". ToByteArray ();
Byte [] key1 = "123". ToByteArray ();
Byte [] key2 = "678". ToByteArray ();
Byte [] key3 = "911". ToByteArray ();
Byte [] key4 = "111". ToByteArray ();
// Test in-stream search
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 [] internal search test
Long index10 = arr. Search (0, key1 );
Long index20 = arr. Search (0, key2 );
Long index30 = arr. Search (0, key3 );
Long index40 = arr. Search (0, key4 );

-----

From the column of caoqing Studio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.