
com.gc.iotools.stream.utils.ArrayTools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easystream Show documentation
Show all versions of easystream Show documentation
EasyStream is a small set of utilities for dealing with
streams (InputStreams
and OutputStreams).
The aim is to ease the use of
pipes when they're required.
Main features are:
* "Convert" an
OutputStream to an InputStream.
* Count the number of bytes read or
wrote to a given stream.
* While reading the data from an InputStream
copy it to a supplied
OutputStream.
* Read the content of an InputStream
multiple times or seek to a
definite position
The newest version!
package com.gc.iotools.stream.utils;
/*
* Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
* under the BSD License.
*/
/**
* Miscellaneous utilities for Arrays, i haven't found anywhere.
*
* @author dvd.smnt
* @since 1.0.9
* @version $Id: ArrayTools.java 576 2015-03-28 00:03:33Z gcontini $
*/
public final class ArrayTools {
/**
* Find the index of the contained array in the src array.
*
* @param src
* Source array.
* @param contained
* Array to search for.
* @return position of the contained array or -1 if not found.
*/
public static int indexOf(final byte[] src, final byte[] contained) {
if (src == null) {
throw new IllegalArgumentException("Source array can not be null");
}
int result = -1;
if (src.length >= contained.length) {
boolean found = false;
int pos = 0;
for (; (pos <= (src.length - contained.length)) && !found; pos++) {
boolean equals = true;
for (int j = 0; (j < contained.length) && equals; j++) {
equals = (src[pos + j] == contained[j]);
found = (j == (contained.length - 1)) && equals;
}
}
result = (found ? (pos - 1) : -1);
}
return result;
}
public static byte[] subarray(final byte[] array,
int startIndexInclusive, int endIndexExclusive) {
if (array == null) {
return null;
}
if (endIndexExclusive > array.length) {
endIndexExclusive = array.length;
}
int newSize = endIndexExclusive - startIndexInclusive;
if (newSize <= 0) {
return new byte[0];
}
byte[] subarray = new byte[newSize];
System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
return subarray;
}
/*
* utility class, shouldn't be instantiated.
*/
private ArrayTools() {
// utility class, shouldn't be instantiated.
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy