All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcodec.containers.mp4.boxes.NodeBox Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package org.jcodec.containers.mp4.boxes;

import java.io.DataOutput;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Predicate;
import org.jcodec.containers.mp4.io.Input;
import org.jcodec.containers.mp4.io.WindowInput;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * A node box
 * 
 * A box containing children, no data
 * 
 * @author Stanislav Vitvitskiy
 * 
 */
public class NodeBox extends Box {
    protected List boxes = new LinkedList();
    protected BoxFactory factory = BoxFactory.getDefault();

    public NodeBox(Header atom) {
        super(atom);
    }

    public NodeBox(NodeBox other) {
        super(other);
        this.boxes = other.boxes;
        this.factory = other.factory;
    }

    public void parse(Input input) throws IOException {

        Box box;
        while ((box = parseChildBox(input, factory)) != null)
            boxes.add(box);
    }

    protected static Box parseChildBox(Input input, BoxFactory factory) throws IOException {
        Header childAtom = Header.read(input);
        if (childAtom == null)
            return null;

        WindowInput wi = new WindowInput(input, childAtom.getBodySize());
        return parseBox(wi, childAtom, factory);
    }

    public static Box newBox(Header header, BoxFactory factory) {
        Class claz = factory.toClass(header.getFourcc());
        if (claz == null)
            return new LeafBox(header);
        try {
            try {
                return claz.getConstructor(Header.class).newInstance(header);
            } catch (NoSuchMethodException e) {
                return claz.newInstance();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Box parseBox(Input input, Header childAtom, BoxFactory factory) throws IOException {
        Box box = newBox(childAtom, factory);

        WindowInput window = new WindowInput(input, (int) childAtom.getBodySize());
        box.parse(window);
        window.skipRemaining();

        return box;
    }

    public List getBoxes() {
        return boxes;
    }

    public void add(Box box) {
        boxes.add(box);
    }

    protected void doWrite(DataOutput out) throws IOException {
        for (Box box : boxes) {
            box.write(out);
        }
    }

    public void addFirst(MovieHeaderBox box) {
        boxes.add(0, box);
    }

    public void filter(Predicate predicate) {
        CollectionUtils.filter(boxes, predicate);
    }

    public void replace(String fourcc, Box box) {
        filter(not(fourcc));
        add(box);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy