001: /**********************************************************************************
002: *
003: * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004: * Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005: *
006: * Licensed under the Educational Community License Version 1.0 (the "License");
007: * By obtaining, using and/or copying this Original Work, you agree that you have read,
008: * understand, and will comply with the terms and conditions of the Educational Community License.
009: * You may obtain a copy of the License at:
010: *
011: * http://cvs.sakaiproject.org/licenses/license_1_0.html
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014: * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015: * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018: *
019: **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: /**
025: * Byte utilities
026: */
027: public class ByteUtils {
028:
029: private static org.apache.commons.logging.Log _log = LogUtils
030: .getLog(ByteUtils.class);
031:
032: /*
033: * Byte manipulation utilities
034: *
035: * Private constructor
036: */
037: private ByteUtils() {
038: }
039:
040: /**
041: * Returns the index in the source array where the first occurrence
042: * of the specified text (a String, converted to a byte array) is found
043: *
044: * @param source Byte array to examine
045: * @param matchString String to locate in <code>source</code>
046: * @return Index of the first matching character (-1 if no match)
047: */
048: public static int indexOf(byte[] source, String matchString) {
049: return indexOf(source, matchString.getBytes());
050: }
051:
052: /**
053: * Returns the index in the source array where the first occurrence
054: * of the specified byte pattern is found
055: *
056: * @param source Byte array to examine
057: * @param match Byte array to locate in <code>source</code>
058: * @return Index of the first matching character (-1 if no match)
059: */
060: public static int indexOf(byte[] source, byte[] match) {
061: for (int i = 0; i < source.length; i++) {
062: if (startsWith(source, i, match)) {
063: return i;
064: }
065: }
066: return -1;
067: }
068:
069: /**
070: * Returns the index in the source array where the last occurrence
071: * of the specified text (a String, converted to a byte array) is found
072: *
073: * @param source Byte array to examine
074: * @param matchString String to locate in <code>source</code>
075: * @return Index of the first matching character (-1 if no match)
076: */
077: public static int lastIndexOf(byte[] source, String matchString) {
078: return lastIndexOf(source, matchString.getBytes());
079: }
080:
081: /**
082: * Returns the index in the source array where the last occurrence
083: * of the specified byte pattern is found
084: *
085: * @param source Byte array to examine
086: * @param match Byte array to locate in <code>source</code>
087: * @return Index of the last matching character (-1 if no match)
088: */
089: public static int lastIndexOf(byte[] source, byte[] match) {
090:
091: if (source.length < match.length) {
092: return -1;
093: }
094:
095: for (int i = (source.length - match.length); i >= 0; i--) {
096: if (startsWith(source, i, match)) {
097: return i;
098: }
099: }
100: return -1;
101: }
102:
103: /**
104: * Does this byte array begin with match array content?
105: *
106: * @param source Byte array to examine
107: * @param match Byte array to locate in <code>source</code>
108: * @return true If the starting bytes are equal
109: */
110: public static boolean startsWith(byte[] source, byte[] match) {
111: return startsWith(source, 0, match);
112: }
113:
114: /**
115: * Does this byte array begin with match array content?
116: *
117: * @param source Byte array to examine
118: * @param offset An offset into the <code>source</code> array
119: * @param match Byte array to locate in <code>source</code>
120: * @return true If the starting bytes are equal
121: */
122: public static boolean startsWith(byte[] source, int offset,
123: byte[] match) {
124:
125: if (match.length > (source.length - offset)) {
126: return false;
127: }
128:
129: for (int i = 0; i < match.length; i++) {
130: if (source[offset + i] != match[i]) {
131: return false;
132: }
133: }
134: return true;
135: }
136:
137: /**
138: * Does the source array equal the match array?
139: *
140: * @param source Byte array to examine
141: * @param offset An offset into the <code>source</code> array
142: * @param match Byte array to locate in <code>source</code>
143: * @return true If the two arrays are equal
144: */
145: public static boolean equals(byte[] source, byte[] match) {
146:
147: if (match.length != source.length) {
148: return false;
149: }
150: return startsWith(source, 0, match);
151: }
152:
153: /**
154: * Copies bytes from the source byte array to the destination array
155: *
156: * @param source The source array
157: * @param srcBegin Index of the first source byte to copy
158: * @param srcEnd Index after the last source byte to copy
159: * @param destination The destination array
160: * @param dstBegin The starting offset in the destination array
161: */
162: public static void getBytes(byte[] source, int srcBegin,
163: int srcEnd, byte[] destination, int dstBegin) {
164: System.arraycopy(source, srcBegin, destination, dstBegin,
165: srcEnd - srcBegin);
166: }
167:
168: /**
169: * Return a new byte array containing a sub-portion of the source array
170: *
171: * @param srcBegin The beginning index (inclusive)
172: * @param srcEnd The ending index (exclusive)
173: * @return The new, populated byte array
174: */
175: public static byte[] subbytes(byte[] source, int srcBegin,
176: int srcEnd) {
177: byte destination[];
178:
179: destination = new byte[srcEnd - srcBegin];
180: getBytes(source, srcBegin, srcEnd, destination, 0);
181:
182: return destination;
183: }
184:
185: /**
186: * Return a new byte array containing a sub-portion of the source array
187: *
188: * @param srcBegin The beginning index (inclusive)
189: * @return The new, populated byte array
190: */
191: public static byte[] subbytes(byte[] source, int srcBegin) {
192: return subbytes(source, srcBegin, source.length);
193: }
194:
195: /*
196: * Test
197: */
198: public static void main(String[] args) throws Exception {
199: byte byteText[];
200: int index, last;
201:
202: byteText = args[0].getBytes();
203: index = indexOf(byteText, "XXX");
204: last = lastIndexOf(byteText, "XXX");
205:
206: _log.debug("Index = " + index);
207: _log.debug("Last = " + last);
208: _log.debug("equal = " + equals(byteText, "XXX".getBytes()));
209: }
210: }
|