/**
Copyright 2013 project Ardulink http://www.ardulink.org/
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package org.ardulink.util;

import static org.ardulink.util.Preconditions.checkNotNull;
import static org.ardulink.util.Preconditions.checkState;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;

public class ByteArray {

	private byte[] byteArray;

	public ByteArray(byte[] byteArray) {
		checkNotNull(byteArray, "Array can't be null");
		this.byteArray = byteArray;
	}

	public ByteArray(ByteArrayOutputStream os) {
		this(os.toByteArray());
	}

	public void resetWith(byte[] byteArray) {
		checkNotNull(byteArray, "Array can't be null");

		this.byteArray = byteArray;
	}

	private int posOf(byte[] delimiter) {
		checkNotNull(delimiter, "delimiter must not be null");
		checkState(delimiter.length > 0, "delimiter must not be empty");

		for (int pos = 0; pos < byteArray.length - delimiter.length + 1; pos++) {
			if (isAtPos(delimiter, pos)) {
				return pos;
			}
		}
		return -1;
	}

	private boolean isAtPos(byte[] delimiter, int pos) {
		for (int i = 0; i < delimiter.length; i++) {
			if (byteArray[pos + i] != delimiter[i]) {
				return false;
			}

		}
		return true;
	}

	public byte[] next(byte[] delimiter) {
		int foundIndex = posOf(delimiter);
		if (foundIndex < 0) {
			return null;
		}

		byte[] retvalue = Arrays.copyOfRange(byteArray, 0, foundIndex);
		// TODO PF Instead of creating new arrays move a pointer
		byteArray = Arrays.copyOfRange(byteArray,
				foundIndex + delimiter.length, byteArray.length);

		return retvalue;
	}

	public int size() {
		return byteArray.length;
	}

	/**
	 * 
	 * @return the array (WARNING modifies to the returned array are shared with
	 *         the ByteArray object)
	 */
	public byte[] getRemainingBytes() {
		return byteArray;
	}

}

@@ -849,16 +849,38 @@
 teArray;
+%0A%09private int pointer;
 %0A%0A%09publi
@@ -917,75 +917,18 @@
 %7B%0A%09%09
-checkNotNull(byteArray, %22Array can't be null%22);%0A%09%09this.byteArray = 
+resetWith(
 byte
@@ -924,32 +924,33 @@
 etWith(byteArray
+)
 ;%0A%09%7D%0A%0A%09public By
@@ -1056,24 +1056,41 @@
 eArray) %7B%0A%09%09
+this.byteArray = 
 checkNotNull
@@ -1112,12 +1112,15 @@
 ray 
-can'
+must no
 t be
@@ -1132,37 +1132,26 @@
 %22);%0A
-%0A
 %09%09this.
-byteArray = byteArray
+pointer = 0
 ;%0A%09%7D
@@ -1333,17 +1333,23 @@
 t pos = 
-0
+pointer
 ; pos %3C 
@@ -1713,26 +1713,26 @@
 %7B%0A%09%09int 
-foundIndex
+newPointer
  = posOf
@@ -1750,26 +1750,26 @@
 ;%0A%09%09if (
-foundIndex
+newPointer
  %3C 0) %7B%0A
@@ -1843,181 +1843,77 @@
 ay, 
-0, foundIndex);%0A%09%09// TODO PF Instead of creating new arrays move a p
+pointer, newP
 ointer
+);
 %0A%09%09
-byteArray = Arrays.copyOfRange(byteArray,%0A%09%09%09%09foundIndex + delimiter.length, byteArray.length);%0A
+this.pointer = newPointer + delimiter.length;
 %0A%09%09r
@@ -1978,16 +1978,26 @@
 y.length
+ - pointer
 ;%0A%09%7D%0A%0A%09/

move pointer instead of copying the array all the time