libepg.ts.aligner.Alligner2.java Source code

Java tutorial

Introduction

Here is the source code for libepg.ts.aligner.Alligner2.java

Source

/*
 * Copyright (C) 2017 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.ts.aligner;

import java.lang.invoke.MethodHandles;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import libepg.ts.packet.TsPacket;
import libepg.ts.packet.TsPacketParcel;
import libepg.util.db.JDBCAccessor;
import libepg.util.db.AboutDB;
import static libepg.util.db.AboutDB.CRATE_TABLE;
import static libepg.util.db.AboutDB.INSERT_SQL;
import static libepg.util.db.AboutDB.PID;
import static libepg.util.db.AboutDB.debug_dump_table;
import loggingsupport.loggerfactory.LoggerFactory;
import org.apache.commons.logging.Log;

/**
 * ?PID???????? ????????<br>
 * ??????????????<br>
 * (??????????0?????????????????????)<br>
 *
 * @author normal
 */
public class Alligner2 {

    /**
     * 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, Alligner2.CLASS_LOG_OUTPUT_MODE).getLOG();
        if (CLASS_LOG_OUTPUT_MODE == false) {
            LOG.info("?");
        }
    }

    private final int pid;

    private final List<TsPacket> packets;

    /**
     * ?PID?????
     *
     * @param pid ?PID
     * @param packets ?
     * @throws IllegalArgumentException ?pid??????
     */
    public Alligner2(int pid, List<TsPacket> packets) throws IllegalArgumentException {
        this.pid = pid;
        if (!TsPacket.PID_RANGE.contains(this.pid)) {
            MessageFormat msg = new MessageFormat(
                    "PID(ID)?????={0} ?={1} ?={2}");
            Object[] parameters = { this.pid, TsPacket.PID_RANGE.getMinimum(), TsPacket.PID_RANGE.getMaximum() };
            throw new IllegalArgumentException(msg.format(parameters));
        }
        this.packets = new ArrayList<>();
        this.packets.addAll(packets);
    }

    public int getPid() {
        return pid;
    }

    public synchronized List<TsPacketParcel> getAllignedPackets() {
        JDBCAccessor ac = JDBCAccessor.getInstance();
        try {
            ac.connect(AboutDB.URL);
            Connection conn = ac.getConnection();

            Statement stmt = conn.createStatement();
            //?
            stmt.executeUpdate(CRATE_TABLE);

            //?PID??????
            for (TsPacket tsp : this.packets) {
                //                System.out.println(Integer.toHexString(tsp.getPid()));
                if (tsp.getPid() == this.getPid()) {
                    PreparedStatement insertStatement = conn.prepareStatement(INSERT_SQL);
                    insertStatement.setInt(1, tsp.getPid());
                    insertStatement.setInt(2, tsp.getContinuity_counter());
                    insertStatement.setInt(3, 0);
                    insertStatement.setBytes(4, tsp.getData());
                    insertStatement.executeUpdate();
                }
            }

            debug_dump_table(conn);

            //            //
            //            LinkedList<TsPacketParcel> temp = new LinkedList<>();
            //            for (TsPacket tsp : this.packets) {
            //
            //                //??????0
            //                if (tsp.getAdaptation_field_control() == TsPacket.ADAPTATION_FIELD_CONTROL.ONLY_ADAPTATION_FIELD) {
            //                    if (tsp.getContinuity_counter() == 0) {
            //                        temp.add(new TsPacketParcel(tsp, TsPacketParcel.MISSING_PACKET_FLAG.NOT_MISSING));
            //                    } else if (LOG.isWarnEnabled()) {
            //                        LOG.warn("???0????????");
            //                        LOG.warn(tsp);
            //                    }
            //                }
            //
            //                if (LOG.isTraceEnabled()) {
            //                    LOG.trace("????");
            //                }
            //                if (temp.contains(tsp)) {
            //
            //                }
            //            }
        } catch (SQLException ex) {
            LOG.fatal(ex);
        } finally {
            ac.close();
        }
        return null;
    }

}