libepg.epg.section.sectionreconstructor.PayLoadSplitter.java Source code

Java tutorial

Introduction

Here is the source code for libepg.epg.section.sectionreconstructor.PayLoadSplitter.java

Source

/*
 * Copyright (C) 2016 normal
 *
 * 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 libepg.epg.section.sectionreconstructor;

import libepg.util.bytearray.ByteConverter;
import java.lang.invoke.MethodHandles;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import libepg.ts.packet.TsPacket;
import epgtools.loggerfactory.LoggerFactory;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;

/**
 * ??TS????(?1?)????????<br>
 *
 *
 * @author normal
 */
public final class PayLoadSplitter {

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

    private static final Log LOG;

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

    private TsPacket packet;

    public PayLoadSplitter() {
    }

    /**
     * @param packet 
     * @throws IllegalArgumentException
     * ?????????????
     */
    public synchronized void setPacket(TsPacket packet) throws IllegalArgumentException {

        this.packet = packet;

        if ((this.packet.getAdaptation_field_control() == TsPacket.ADAPTATION_FIELD_CONTROL.ONLY_ADAPTATION_FIELD)
                || (this.packet.getAdaptation_field_control() == TsPacket.ADAPTATION_FIELD_CONTROL.RESERVED)) {
            MessageFormat msg2 = new MessageFormat(
                    "?????????????={0}");
            Object[] parameters2 = { this.packet.toString() };
            throw new IllegalArgumentException(msg2.format(parameters2));
        }
    }

    public synchronized TsPacket getPacket() {
        return packet;
    }

    /**
     * ?????????
     */
    public enum PAYLOAD_PART_KEY {
        /**
         * ?2????????
         */
        PAYLOAD_AFTER_2_BYTE(POINTER_FIELD_EXISTENCE.EXIST),
        /**
         * ?2?????????????
         */
        PREV_POINTER(POINTER_FIELD_EXISTENCE.EXIST),
        /**
         * ????????????
         */
        NEXT_POINTER(POINTER_FIELD_EXISTENCE.EXIST),
        /**
         * ?
         */
        ALL_PAYLOAD(POINTER_FIELD_EXISTENCE.NOT_EXIST);

        /**
         * ????????
         */
        public enum POINTER_FIELD_EXISTENCE {
            /**
             * ??????
             */
            EXIST(true),
            /**
             * ???????
             */
            NOT_EXIST(false);

            private final boolean value;

            private POINTER_FIELD_EXISTENCE(boolean value) {
                this.value = value;
            }

            public boolean isValue() {
                return value;
            }

            @Override
            public String toString() {
                MessageFormat msg = new MessageFormat(
                        "{0} (???????={1})");
                Object[] parameters = { super.toString(), this.isValue() };
                return msg.format(parameters);
            }
        }

        private final POINTER_FIELD_EXISTENCE pointer_field_existence;

        private PAYLOAD_PART_KEY(POINTER_FIELD_EXISTENCE pointer_field_existence) {
            this.pointer_field_existence = pointer_field_existence;
        }

        /**
         * ????????
         *
         * @return
         */
        public POINTER_FIELD_EXISTENCE getPointer_field_existence() {
            return pointer_field_existence;
        }

        @Override
        public String toString() {
            MessageFormat msg = new MessageFormat("{0}(?={1})");
            Object[] parameters = { super.toString(), this.getPointer_field_existence() };
            return msg.format(parameters);
        }
    }

    /**
     * 1:payload_unit_start_indicator?1?????0?????2??????????1?<br>
     * 2:payload_unit_start_indicator?1?????0????<br>
     * ?2???????????????<br>
     * ????+1??????????2?<br>
     * 3:payload_unit_start_indicator?0????????1?
     *
     * @return ??? <br>
     */
    public synchronized Map<PAYLOAD_PART_KEY, byte[]> getSplittedPayLoad() {

        if (LOG.isTraceEnabled()) {
            LOG.trace(this.packet);
        }

        Map<PAYLOAD_PART_KEY, byte[]> temp = new HashMap<>();
        EXEC: {
            if (this.packet
                    .getPayload_unit_start_indicator() == TsPacket.PAYLOAD_UNIT_START_INDICATOR.NOT_START_POINT) {
                LOG.trace("????");
                temp.put(PAYLOAD_PART_KEY.ALL_PAYLOAD, this.packet.getPayload());
                break EXEC;
            }
            if (this.packet
                    .getPayload_unit_start_indicator() == TsPacket.PAYLOAD_UNIT_START_INDICATOR.START_PES_OR_START_SECTION) {
                LOG.trace("???");
                int pointerField = ByteConverter.byteToInt(this.packet.getPayload()[0]);
                int tempLength = this.packet.getPayload().length - 1;
                byte[] tempArray = new byte[tempLength];
                System.arraycopy(this.packet.getPayload(), 1, tempArray, 0, tempArray.length);
                if (LOG.isTraceEnabled()) {
                    MessageFormat msg1 = new MessageFormat(
                            "?={0} ={1} 2??={2}");
                    Object[] parameters1 = { pointerField, Hex.encodeHexString(this.packet.getPayload()),
                            Hex.encodeHexString(tempArray) };
                    LOG.trace(msg1.format(parameters1));
                }
                if (pointerField == 0) {
                    temp.put(PAYLOAD_PART_KEY.PAYLOAD_AFTER_2_BYTE, tempArray);
                    break EXEC;
                } else {
                    byte[] prev = new byte[pointerField];
                    System.arraycopy(tempArray, 0, prev, 0, prev.length);
                    temp.put(PAYLOAD_PART_KEY.PREV_POINTER, prev);
                    byte[] next = new byte[tempArray.length - pointerField];
                    System.arraycopy(tempArray, pointerField, next, 0, next.length);
                    temp.put(PAYLOAD_PART_KEY.NEXT_POINTER, next);
                    break EXEC;
                }
            }
        }

        dumpMap(temp);

        return Collections.unmodifiableMap(temp);
    }

    private synchronized void dumpMap(Map<PayLoadSplitter.PAYLOAD_PART_KEY, byte[]> t_map) {
        if (LOG.isTraceEnabled() && PayLoadSplitter.CLASS_LOG_OUTPUT_MODE == true) {
            StringBuilder s = new StringBuilder();
            s.append("?[");
            MessageFormat msg1 = new MessageFormat("={0} ?={1}");
            Set<PayLoadSplitter.PAYLOAD_PART_KEY> keys = t_map.keySet();
            for (PayLoadSplitter.PAYLOAD_PART_KEY key : keys) {
                Object[] parameters1 = { key, Hex.encodeHexString(t_map.get(key)) };
                s.append(msg1.format(parameters1));
            }
            s.append("]");
            LOG.trace(s.toString());
        }
    }

}