dumpsection.Main.java Source code

Java tutorial

Introduction

Here is the source code for dumpsection.Main.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 dumpsection;

import epgtools.loggerfactory.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import libepg.epg.section.Section;
import libepg.epg.section.sectionreconstructor.SectionReconstructor;
import libepg.ts.packet.PROGRAM_ID;
import libepg.ts.packet.TsPacketParcel;
import libepg.ts.reader.TsReader;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;

/**
 * ??ts??????????SDT????16????<br>
 * 16?????ts??+pid+SDT.txt??<br>
 * ???????????pid??????<br>
 * ????????CRC??????????????<br>
 */
public class Main {

    /**
     * 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, Main.CLASS_LOG_OUTPUT_MODE).getLOG();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            new Main().start(args);
            System.exit(0);
        } catch (ParseException ex) {
            LOG.fatal("??????", ex);
            System.exit(1);
        }
    }

    private String dumpCollection(Collection<?> target) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (Object s : target) {
            sb.append(s.getClass());
            sb.append("=");
            sb.append(s);
            sb.append(" ");
        }
        sb.append("}");
        return sb.toString();
    }

    private String dumpArgs(String[] args) {
        Collection<String> strs = Arrays.asList(args);
        return this.dumpCollection(strs);
    }

    private String dumpSet(Set<?> target) {
        return this.dumpCollection(target);
    }

    public void start(String[] args) throws org.apache.commons.cli.ParseException {
        final String fileName;
        final Long limit;

        System.out.println("args   : " + dumpArgs(args));

        final Option fileNameOption = Option.builder("f").required().longOpt("filename").desc("ts??")
                .hasArg().type(String.class).build();

        final Option limitOption = Option.builder("l").required(false).longOpt("limit")
                .desc("??(???????EOF??)").hasArg()
                .type(Long.class).build();

        Options opts = new Options();
        opts.addOption(fileNameOption);
        opts.addOption(limitOption);
        CommandLineParser parser = new DefaultParser();
        CommandLine cl;
        HelpFormatter help = new HelpFormatter();

        try {

            // parse options
            cl = parser.parse(opts, args);

            // handle interface option.
            fileName = cl.getOptionValue(fileNameOption.getOpt());
            if (fileName == null) {
                throw new ParseException("????????");
            }

            // handlet destination option.
            Long xl = null;
            try {
                xl = Long.parseUnsignedLong(cl.getOptionValue(limitOption.getOpt()));
            } catch (NumberFormatException e) {
                xl = null;
            } finally {
                limit = xl;
            }

            final PROGRAM_ID pids = PROGRAM_ID.SDT_OR_BAT;

            System.out.println("Starting application...");
            System.out.println("filename   : " + fileName);
            System.out.println("limit : " + limit);

            // your code
            TsReader reader;
            if (limit == null) {
                reader = new TsReader(new File(fileName), pids.getPids());
            } else {
                reader = new TsReader(new File(fileName), pids.getPids(), limit);
            }

            Map<Integer, List<TsPacketParcel>> ret = reader.getPackets();

            for (Integer pid : ret.keySet()) {
                try (FileWriter writer = new FileWriter(fileName + "_" + Integer.toHexString(pid) + "_SDT.txt")) {
                    SectionReconstructor sr = new SectionReconstructor(ret.get(pid), pid);
                    for (Section s : sr.getSections()) {
                        String text = Hex.encodeHexString(s.getData());
                        writer.write(text + "\n");
                    }
                    writer.flush();
                } catch (IOException ex) {
                    LOG.fatal("", ex);
                }
            }

        } catch (ParseException e) {
            // print usage.
            help.printHelp("My Java Application", opts);
            throw e;
        } catch (FileNotFoundException ex) {
            LOG.fatal("?????", ex);
        }
    }
}