/*-
 * Copyright (C) 2021 Erik Larsson
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.catacombae.bplist.types;

import java.io.PrintStream;
import org.catacombae.util.Util;

/** This class was generated by CStructToJavaClass. */
public class BinaryPlistHeader {
    /*
     * struct BinaryPlistHeader
     * size: 8 bytes
     * description:
     *
     * BP  Size   Type     Identifier  Description
     * -------------------------------------------
     * 0   1 * 6  char[6]  signature
     * 6   1 * 2  char[2]  version
     */

    public static final int STRUCTSIZE = 8;

    private final byte[] signature = new byte[1 * 6];
    private short version;

    public BinaryPlistHeader(byte[] data, int offset) {
        System.arraycopy(data, offset + 0, this.signature, 0, 1 * 6);
        this.version = Util.readShortBE(data, offset + 6);
    }

    public static int length() { return STRUCTSIZE; }

    /**  */
    public final byte[] getSignature() {
        return Util.readByteArrayBE(this.signature);
    }

    /**  */
    public final byte[] getVersion() {
        return Util.toByteArrayBE(this.version);
    }

    public void printFields(PrintStream ps, String prefix) {
        ps.println(prefix + " signature: ");
        {
            byte[] _array = getSignature();
            for(int _i = 0; _i < 6; ++_i) {
                ps.println(prefix + "  [" + _i + "]:");
                ps.println(prefix + "    " + _array[_i]);
            }
        }
        ps.println(prefix + " version: ");
        {
            byte[] _array = getVersion();
            for(int _i = 0; _i < 2; ++_i) {
                ps.println(prefix + "  [" + _i + "]:");
                ps.println(prefix + "    " + _array[_i]);
            }
        }
    }

    public void print(PrintStream ps, String prefix) {
        ps.println(prefix + "BinaryPlistHeader:");
        printFields(ps, prefix);
    }

    public byte[] getBytes() {
        byte[] result = new byte[length()];
        getBytes(result, 0);
        return result;
    }

    public int getBytes(byte[] result, int offset) {
        final int startOffset = offset;

        System.arraycopy(this.signature, 0, result, offset, this.signature.length); offset += this.signature.length;
        Util.arrayPutBE(result, offset, this.version); offset += 2;

        return offset - startOffset;
    }
}
