KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > util > ByteArrayComparator


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.util;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12
13
14 /**
15  * ByteArrayComparator is used for finding first appearance of the inner byte
16  * array in the outher byte array.
17  */

18 public class ByteArrayComparator {
19
20 /**
21  * Position of first element from array of elements in the lookUpArray0 which
22  * corresponds to toFind0 array
23  */

24   private int positionFirst = -1;
25
26 /**
27  * Construction with the given byte arrays (first is being searched in the
28  * second (lookup) byte array)
29  * @param toFind0 byte array which is being searhced
30  * @param lookUpArray0 byte array to be searched
31  * @exception SMIMEException if toFind0 array is bigger than or equal to
32  * lookUpArray0 array!
33  */

34   public ByteArrayComparator (byte[] toFind0, byte[] lookUpArray0) throws SMIMEException
35   {
36     if (toFind0.length >= lookUpArray0.length)
37       throw new SMIMEException(this, 1033);
38     positionFirst = find(toFind0, lookUpArray0);
39   }
40
41 /**
42  * Finds first appearance of the byte array in the outher byte array
43  * @param toFind0 byte array which is being searched
44  * @param lookUpArray0 byte array to be searched
45  * @return Position of the first matching byte array
46  */

47   private int find (byte[] toFind0, byte[] lookUpArray0) {
48     int k;
49     int positionFirst = -1;
50     byte[] prStr;
51     for (k = 0; k != lookUpArray0.length; k++) {
52       for (int z = 0; z != toFind0.length; z++) {
53         positionFirst = k;
54         if (toFind0[z] != lookUpArray0[k + z]) {
55           positionFirst = -1;
56           break;
57         }
58       }
59       if (positionFirst == k)
60         break;
61     }
62     return positionFirst; // -1 if no matching found
63
}
64
65 /**
66  * Returns the position of the first matching byte in the outher byte array
67  * @return Position of the first matching byte in the outher byte array
68  */

69   public int getMatchingIndex () {
70     return positionFirst;
71   }
72 }
73
74
75
76
Popular Tags