libepg.epg.section.descriptor.Descriptor.java Source code

Java tutorial

Introduction

Here is the source code for libepg.epg.section.descriptor.Descriptor.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package libepg.epg.section.descriptor;

import libepg.util.bytearray.ByteConverter;
import java.lang.invoke.MethodHandles;
import java.text.MessageFormat;
import java.util.Objects;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import epgtools.loggerfactory.LoggerFactory;
import libepg.util.bytearray.ByteDataBlock;

/**
 * ?(descriptor()?)? <br>
 *
 * @author normal
 */
public class Descriptor {

    /**
     * false?????????????
     */
    public static final boolean CLASS_LOG_OUTPUT_MODE = true;

    protected static final Log LOG;

    static {
        final Class<?> myClass = MethodHandles.lookup().lookupClass();
        LOG = new LoggerFactory(myClass, Descriptor.CLASS_LOG_OUTPUT_MODE).getLOG();
    }

    private final ByteDataBlock data;
    private final DESCRIPTOR_TAG descriptorTag;

    /**
     * ??????????
     *
     * @param descriptor ?
     */
    public Descriptor(Descriptor descriptor) {

        this.descriptorTag = descriptor.descriptorTag;
        if (this.getClass() != this.descriptorTag.getDataType()) {
            MessageFormat msg1 = new MessageFormat(
                    "??????????????={0} ?={1} ={2}");
            Object[] parameters1 = { this.getClass(), this.descriptorTag.getDataType(),
                    Hex.encodeHexString(descriptor.getData()) };
            throw new IllegalArgumentException(msg1.format(parameters1));
        }
        this.data = descriptor.data;
    }

    /**
     * ????DescriptorsLoop?????
     *
     * @param data ??
     * @throws IllegalArgumentException
     * ?????preferedDescriptorTag???????????
     */
    Descriptor(byte[] data) {

        this.data = new ByteDataBlock(data);

        int lengthFromData = ByteConverter.byteToInt(this.data.getData()[1]) + 2;
        int lengthFromArray = this.getData().length;
        if (lengthFromData != lengthFromArray) {
            MessageFormat msg1 = new MessageFormat(
                    "?????????????={0} ?={1} ={2}");
            Object[] parameters1 = { lengthFromData, lengthFromArray, Hex.encodeHexString(this.data.getData()) };
            throw new IllegalArgumentException(msg1.format(parameters1));
        }

        int tag = ByteConverter.byteToInt(this.data.getData()[0]);
        this.descriptorTag = DESCRIPTOR_TAG.reverseLookUp(tag);
        if (this.descriptorTag == null) {
            MessageFormat msg = new MessageFormat(
                    "???????={0} ={1}");
            Object[] parameters = { Integer.toHexString(tag), Hex.encodeHexString(this.data.getData()) };
            throw new IllegalArgumentException(msg.format(parameters));
        }
    }

    /**
     * ????
     *
     * @return ?
     */
    public final synchronized byte[] getData() {
        return data.getData();
    }

    /**
     * @return ????
     */
    public final synchronized DESCRIPTOR_TAG getDescriptor_tag_const() {
        return this.descriptorTag;
    }

    /**
     * descriptor_tag?<br>
     *
     * @return ??
     */
    public final synchronized int getDescriptor_tag() {
        int temp;
        temp = ByteConverter.byteToInt(this.getData()[0]);
        return temp;
    }

    /**
     * descriptor_length???8 ??????
     * ?????????
     *
     * @return ?
     */
    public final synchronized int getDescriptor_length() {
        int temp;
        temp = ByteConverter.byteToInt(this.getData()[1]);
        return temp;
    }

    /**
     * ??(????????3????)??
     *
     * @return
     */
    public final synchronized byte[] getDescriptor_Body() {
        int length;
        length = this.getDescriptor_length();
        if (LOG.isTraceEnabled()) {
            LOG.trace("?=" + length);
            LOG.trace("????-2=" + (this.data.length() - 2));
        }
        byte[] t = new byte[length];
        System.arraycopy(this.getData(), 2, t, 0, length);
        return t;
    }

    private static final MessageFormat DESC = new MessageFormat("? = {0}\n" + "? = {1}\n"
            + "? = {2}\n" + "? = {3}\n");

    @Override
    public String toString() {
        Object[] parameters = { this.data.toString(), this.getDescriptor_tag_const(), this.getDescriptor_length(),
                Hex.encodeHexString(this.getDescriptor_Body()) };
        return DESC.format(parameters);
    }

    /**
     * @return ????????
     */
    @Override
    public final int hashCode() {
        int hash = 7;
        hash = 83 * hash + Objects.hashCode(this.data);
        hash = 83 * hash + Objects.hashCode(this.descriptorTag);
        return hash;
    }

    /**
     * @return 3??????????true<br>
     * 1.??????????<br>
     * 2.??????????????????????????<br>
     * 3.??????????????<br>
     */
    @Override
    public final boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Descriptor other = (Descriptor) obj;
        if (!Objects.equals(this.data, other.data)) {
            return false;
        }
        if (this.descriptorTag != other.descriptorTag) {
            return false;
        }
        return true;
    }

}