/*
 * 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.apache.qpid.proton4j.amqp;

import java.nio.ByteBuffer;
import java.util.Collection;

public final class Binary {

    private final byte[] data;
    private final int offset;
    private final int length;
    private int hashCode;

    public Binary(final byte[] data) {
        this(data, 0, data.length);
    }

    public Binary(final byte[] data, final int offset, final int length) {
        this.data = data;
        this.offset = offset;
        this.length = length;
    }

    public Binary copy() {
        byte[] dataCopy = new byte[length];
        System.arraycopy(data, offset, dataCopy, 0, length);
        return new Binary(dataCopy, 0, length);
    }

    public ByteBuffer asByteBuffer() {
        return ByteBuffer.wrap(data, offset, length);
    }

    @Override
    public final int hashCode() {
        int hc = hashCode;
        if (hc == 0) {
            for (int i = 0; i < length; i++) {
                hc = 31 * hc + (0xFF & data[offset + i]);
            }
            hashCode = hc;
        }
        return hc;
    }

    @Override
    public final boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Binary buf = (Binary) o;
        final int size = length;
        if (size != buf.length) {
            return false;
        }

        final byte[] myData = data;
        final byte[] theirData = buf.data;
        int myOffset = offset;
        int theirOffset = buf.offset;
        final int myLimit = myOffset + size;

        while (myOffset < myLimit) {
            if (myData[myOffset++] != theirData[theirOffset++]) {
                return false;
            }
        }

        return true;
    }

    public int getArrayOffset() {
        return offset;
    }

    public byte[] getArray() {
        return data;
    }

    public int getLength() {
        return length;
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();

        for (int i = 0; i < length; i++) {
            byte c = data[offset + i];

            if (c > 31 && c < 127 && c != '\\') {
                str.append((char) c);
            } else {
                str.append(String.format("\\x%02x", c));
            }
        }

        return str.toString();
    }

    public static Binary combine(final Collection<Binary> binaries) {

        if (binaries.size() == 1) {
            return binaries.iterator().next();
        }

        int size = 0;
        for (Binary binary : binaries) {
            size += binary.getLength();
        }
        byte[] data = new byte[size];
        int offset = 0;
        for (Binary binary : binaries) {
            System.arraycopy(binary.data, binary.offset, data, offset, binary.length);
            offset += binary.length;
        }
        return new Binary(data);
    }

    public Binary subBinary(final int offset, final int length) {
        return new Binary(data, offset + offset, length);
    }

    public static Binary create(ByteBuffer buffer) {
        if (buffer == null)
            return null;
        if (buffer.isDirect() || buffer.isReadOnly()) {
            byte data[] = new byte[buffer.remaining()];
            ByteBuffer dup = buffer.duplicate();
            dup.get(data);
            return new Binary(data);
        } else {
            return new Binary(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
        }
    }
}

@@ -3167,32 +3167,177 @@
 tring();%0A    %7D%0A%0A
+    // TODO - These aren't used currently and could be removed to allow this type of operation%0A    //        to fall on the ProtonBuffer class.%0A%0A
     public stati
@@ -3390,17 +3390,16 @@
 ries) %7B%0A
-%0A
         
@@ -3966,22 +3966,32 @@
  Binary(
+this.
 data, 
+this.
 offset +
@@ -4095,16 +4095,18 @@
 == null)
+ %7B
 %0A       
@@ -4123,16 +4123,37 @@
 n null;%0A
+        %7D else %7B%0A    
         
@@ -4208,24 +4208,28 @@
             
+   
+ 
 byte data%5B%5D 
@@ -4272,16 +4272,20 @@
         
+    
 ByteBuff
@@ -4309,24 +4309,28 @@
 uplicate();%0A
+    
             
@@ -4348,32 +4348,36 @@
 a);%0A            
+    
 return new Binar
@@ -4385,32 +4385,36 @@
 (data);%0A        
+    
 %7D else %7B%0A       
@@ -4410,32 +4410,36 @@
 e %7B%0A            
+ 
+   
 return new Binar
@@ -4515,24 +4515,38 @@
 maining());%0A
+            %7D%0A
         %7D%0A  

Fix issue with wrong offset value being used and add some notes