簡體   English   中英

從偏移量查找字節

[英]Find bytes from an offset

所以我有這個:

public static long FindPosition(Stream stream, byte[] byteSequence)
{
     if (byteSequence.Length > stream.Length)
         return -1;

     byte[] buffer = new byte[byteSequence.Length];

     using (BufferedStream bufStream = new BufferedStream(stream, byteSequence.Length))
     {
           int i;
           while ((i = bufStream.Read(buffer, 0, byteSequence.Length)) == byteSequence.Length)
           {
                if (byteSequence.SequenceEqual(buffer))
                    return bufStream.Position - byteSequence.Length;
                else
                    bufStream.Position -= byteSequence.Length - PadLeftSequence(buffer, byteSequence);
           }                 
     }
     return  -1;
}
private static int PadLeftSequence(byte[] bytes, byte[] seqBytes)
{
     int i = 1;
     while (i < bytes.Length)
     {
          int n = bytes.Length - i;
          byte[] aux1 = new byte[n];
          byte[] aux2 = new byte[n];
          Array.Copy(bytes, i, aux1, 0, n);
          Array.Copy(seqBytes, aux2, n);
          if (aux1.SequenceEqual(aux2))
             return i;
             i++;
      }
      return i;
}

哪個方法可以完美地獲取具有特定字節組的偏移量,但是現在我要反過來,從特定偏移量中查找一組字節。 我怎樣才能做到這一點?

試試這個例子:

long offset = 100L; // Offset
int bytesCount = 20; // Number of bytes to read
byte[] buffer = new byte[bytesCount];

stream.Seek( offset, SeekOrigin.Begin ); // Set offset from Begin of a stream
stream.Read( buffer, 0, bytesCount ); // Read bytesCount from previous set offset

有關搜索閱讀的更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM