package com.googlecode.mp4parser.boxes.apple;

import com.coremedia.iso.IsoTypeReaderVariable;
import com.coremedia.iso.IsoTypeWriterVariable;

import java.nio.ByteBuffer;

/**
 * Created by sannies on 10/22/13.
 */
public abstract class AppleVariableSignedIntegerBox extends AppleDataBox {
    protected AppleVariableSignedIntegerBox(String type) {
        super(type, 15);
    }

    long value;
    int intLength = -1;

    public long getValue() {
        return value;
    }

    public void setValue(long value) {


        if (value <= 32767 && value > -32768 && intLength<2) {
            intLength = 2;
        } else if (value <= 8388607 && value > -8388608&& intLength<3) {
            intLength = 3;
        } else {
            intLength = 4;
        }

        this.value = value;
    }

    @Override
    protected byte[] writeData() {
        int dLength = getDataLength();
        ByteBuffer b = ByteBuffer.wrap(new byte[dLength]);
        IsoTypeWriterVariable.write(value, b, dLength);
        return b.array();
    }

    @Override
    protected void parseData(ByteBuffer data) {
        int intLength = data.remaining();
        value = IsoTypeReaderVariable.read(data, intLength);
        this.intLength = intLength;
    }

    @Override
    protected int getDataLength() {
        return intLength;
    }
}

@@ -431,15 +431,176 @@
 h = 
--
 1;%0D%0A%0D%0A
+    public int getIntLength() %7B%0D%0A        return intLength;%0D%0A    %7D%0D%0A%0D%0A    public void setIntLength(int intLength) %7B%0D%0A        this.intLength = intLength;%0D%0A    %7D%0D%0A%0D%0A
     
@@ -699,18 +699,16 @@
 e) %7B%0D%0A%0D%0A
-%0D%0A
         
@@ -719,16 +719,96 @@
 value %3C=
+ 127 && value %3E -128) %7B%0D%0A            intLength = 1;%0D%0A        %7D else if (value %3C=
  32767 &

allow set of int length