X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FStreamUtils.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FStreamUtils.java;h=6b8251a6e692f498452d8dcacab6d0d1958318f7;hb=c8ce09c4dc57142f94afcdc290dd79c4bddf7820;hp=0000000000000000000000000000000000000000;hpb=8e76f6ab13a8a4a651f2518b6c91d5e6424c7922;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/StreamUtils.java b/src/be/nikiroo/utils/streams/StreamUtils.java new file mode 100644 index 0000000..6b8251a --- /dev/null +++ b/src/be/nikiroo/utils/streams/StreamUtils.java @@ -0,0 +1,70 @@ +package be.nikiroo.utils.streams; + +import java.io.UnsupportedEncodingException; + +/** + * Some non-public utilities used in the stream classes. + * + * @author niki + */ +class StreamUtils { + /** + * Check if the buffer starts with the given search term (given as an array, + * a start position and an end position). + *

+ * Note: the parameter stop is the index of the last + * position, not the length. + *

+ * Note: the search term size must be smaller or equal the internal + * buffer size. + * + * @param search + * the term to search for + * @param buffer + * the buffer to look into + * @param start + * the offset at which to start the search + * @param stop + * the maximum index of the data to check (this is not a + * length, but an index) + * + * @return TRUE if the search content is present at the given location and + * does not exceed the len index + */ + static public boolean startsWith(byte[] search, byte[] buffer, int start, + int stop) { + + // Check if there even is enough space for it + if (search.length > (stop - start)) { + return false; + } + + boolean same = true; + for (int i = 0; i < search.length; i++) { + if (search[i] != buffer[start + i]) { + same = false; + break; + } + } + + return same; + } + + /** + * Return the bytes array representation of the given {@link String} in + * UTF-8. + * + * @param str + * the string to transform into bytes + * @return the content in bytes + */ + static public byte[] bytes(String str) { + try { + return str.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + // All conforming JVM must support UTF-8 + e.printStackTrace(); + return null; + } + } +}