/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.thingml.generated.network;

//This Class is extracted from Apache ArrayUtils, distributed under Apache License 2.0
public class JavaBinaryHelper {
    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
    public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];

    public static byte[] toPrimitive(final Byte[] array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return EMPTY_BYTE_ARRAY;
        }
        final byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    public static Byte[] toObject(final byte[] array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return EMPTY_BYTE_OBJECT_ARRAY;
        }
        final Byte[] result = new Byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = Byte.valueOf(array[i]);
        }
        return result;
    }
}//see https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -836,16 +836,42 @@
 twork;%0A%0A
+import java.util.Arrays;%0A%0A
 //This C
@@ -887,16 +887,31 @@
 xtracted
+ (and enhanced)
  from Ap
@@ -1876,32 +1876,32 @@
 %5Bi%5D);%0A        %7D%0A
-
         return r
@@ -1912,16 +1912,1500 @@
 t;%0A    %7D
+%0A%0A    public static byte%5B%5D escape(final byte%5B%5D array, java.util.List%3CByte%3E escapes, byte esc) %7B%0A        int newSize = array.length;%0A        for (byte b : array) %7B%0A            if (escapes.contains(b))%0A                newSize++;%0A        %7D%0A        final byte%5B%5D result = new byte%5BnewSize%5D;%0A        int i = 0;%0A        for (byte b : array) %7B%0A            if (escapes.contains(b)) %7B%0A                result%5Bi%5D = esc;%0A                i++;%0A            %7D%0A            result%5Bi%5D = b;%0A            i++;%0A        %7D%0A        return result;%0A    %7D%0A%0A    public static byte%5B%5D unescape(final byte%5B%5D array, java.util.List%3CByte%3E escapes, byte esc) %7B%0A        int newSize = array.length;%0A        for (byte b : array) %7B%0A            if (escapes.contains(b))%0A                newSize--;%0A        %7D%0A        final byte%5B%5D buffer = new byte%5BnewSize%5D;%0A        final int RCV_MSG = 1;%0A        final int RCV_ESC = 2;%0A        int buffer_idx = 0;%0A        int state = RCV_MSG;%0A        for(byte data : array) %7B%0A            if (state == RCV_MSG) %7B%0A                if (data == esc) %7B%0A                    state = RCV_ESC;%0A                %7D else %7B // it is just a byte to store%0A                    buffer%5Bbuffer_idx%5D = data;%0A                    buffer_idx++;%0A                %7D%0A            %7D else if (state == RCV_ESC) %7B%0A                // Store the byte without looking at it%0A                buffer%5Bbuffer_idx%5D = data;%0A                buffer_idx++;%0A                state = RCV_MSG;%0A            %7D%0A        %7D%0A        return buffer;%0A    %7D
 %0A%7D//see 

add Java helper to (un)escape bytes