org.foray.common
Class ByteSequenceSearcher

java.lang.Object
  extended by org.foray.common.ByteSequenceSearcher

public final class ByteSequenceSearcher
extends Object

For illustration purposes, consider the file to be a sequence of bytes ordered from left to right. The goal here is to create an array that acts like a "viewbox" on the contents of the file. We will slide the viewbox over the contents of the file to see it. (We slide the box in big chunks to minimize the cost of i/o operations. If performance were not an issue, we could just read the file one byte at a time). If we are reading the file forward, we will be sliding the right end of the viewbox over the left end of the file. If we are reading the file backward, we will be sliding the left end of the viewbox over the right end of the file. Searches binary (or text) files for the specified contents in a relatively efficient manner. The two extreme approaches are as follows: 1) Read the entire file into a byte array and search that array. This can use quite a bit of memory. 2) Read one byte at a time. This doesn't use much memory, but can be very slow. Instead of either of these approaches, searchBytes strikes a middle ground in which an intermediate amount of memory is read and searched. Please note that searchBytes is looking for bytes that match, not characters that match.


Method Summary
static boolean byteArrayCompare(byte[] bytesToSearch, int bytesToSearchOffset, byte[] bytesToFind)
          Determine if the contents of bytesToFind is equal to the contents of bytesToSearch at a given location.
 long searchBytes()
          Searches the content for the sought byte sequence.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

searchBytes

public long searchBytes()
                 throws IOException
Searches the content for the sought byte sequence.

Returns:
The offset in the file that corresponds to the first byte of the byte sequence that matches bytesToFind. If bytesToFind is the byte equivalent of "ABC", and that match is at bytes 6 through 8, then 6 will be returned. If no match is found, -1 is returned.
Throws:
IOException - For errors reading the file.

byteArrayCompare

public static boolean byteArrayCompare(byte[] bytesToSearch,
                                       int bytesToSearchOffset,
                                       byte[] bytesToFind)
Determine if the contents of bytesToFind is equal to the contents of bytesToSearch at a given location.

Parameters:
bytesToSearch - The (bigger) byte array that is being searched.
bytesToSearchOffset - The location in bytesToSearch at which to start the comparison.
bytesToFind - The (smaller) byte array that contains the content to be matched in bytesToSearch.
Returns:
True if the contents match, false otherwise.


Copyright © 2017. All rights reserved.