<commit_before>/*  
 * Copyright 2008 CoreMedia AG, Hamburg
 *
 * Licensed under the Apache License, Version 2.0 (the License); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an AS IS BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */

package com.coremedia.iso.boxes;

import com.coremedia.iso.BoxParser;
import com.coremedia.iso.IsoBufferWrapper;
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.IsoOutputStream;
import com.coremedia.iso.boxes.fragment.MovieFragmentBox;
import com.coremedia.iso.boxes.fragment.TrackFragmentBox;
import com.coremedia.iso.mdta.Chunk;
import com.coremedia.iso.mdta.Sample;
import com.coremedia.iso.mdta.SampleImpl;
import com.coremedia.iso.mdta.Track;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * This box contains the media data. In video tracks, this box would contain video frames. A presentation may
 * contain zero or more Media Data Boxes. The actual media data follows the type field; its structure is described
 * by the metadata (see {@link SampleTableBox}).<br>
 * In large presentations, it may be desirable to have more data in this box than a 32-bit size would permit. In this
 * case, the large variant of the size field is used.<br>
 * There may be any number of these boxes in the file (including zero, if all the media data is in other files). The
 * metadata refers to media data by its absolute offset within the file (see {@link StaticChunkOffsetBox});
 * so Media Data Box headers and free space may easily be skipped, and files without any box structure may
 * also be referenced and used.
 */
public final class MediaDataBox<T extends TrackMetaDataContainer> extends AbstractBox {
    public static final String TYPE = "mdat";

    private boolean contentsParsed = false;

    private byte[] deadBytesBefore = new byte[0];

    private long sizeIfNotParsed;

    private long startOffset;


    private Map<Long, Track<T>> tracks = new HashMap<Long, Track<T>>();

    private List<SampleHolder<T>> sampleList = new ArrayList<SampleHolder<T>>();
    private TrackBoxContainer<TrackFragmentBox> movieFragmentBoxBefore;
    private IsoBufferWrapper isoBufferWrapper;
    private long parsedSize = -1;

    public MediaDataBox(MovieFragmentBox lastMovieFragmentBox) {
        super(IsoFile.fourCCtoBytes(TYPE));
        this.movieFragmentBoxBefore = lastMovieFragmentBox;
    }

    public boolean isContentsParsed() {
        return contentsParsed;
    }

    public byte[] getDeadBytesBefore() {
        return deadBytesBefore;
    }


    @Override
    public void getBox(IsoOutputStream os) throws IOException {
        os.write(getHeader());
        os.write(getDeadBytesBefore());
        getContent(os);
        for (ByteBuffer buffer : deadBytes) {
            buffer.rewind();
            byte[] bufAsAr = new byte[buffer.limit()];
            buffer.get(bufAsAr);
            os.write(bufAsAr);
        }

    }

  @Override
  public long getSize() {
    long contentSize = getContentSize();  // avoid calling getContentSize() twice
    long headerSize = 4 + // headerSize
            4 + // type
            (contentSize >= 4294967296L ? 8 : 0);
    return headerSize + contentSize + getDeadBytes().length + getDeadBytesBefore().length;
  }

    public long getSampleCount() {
        return sampleList.size();
    }

    public Sample<T> getSample(int index) {
        return sampleList.get(index).getSample();
    }

    public void replaceSample(int index, Sample<?> s) {
        sampleList.get(index).setSample((Sample<T>) s);
    }

    @Override
    protected long getContentSize() {
        if (parsedSize == -1 && contentsParsed) {
            long size = 0;

            for (Track<T> track : tracks.values()) {
                //System.out.println("getting size of track " + track.getTrackId());
                size += track.getSize();
            }

            //todo: not fast
            assert size == calculateSampleSizes();
            parsedSize = size;
            return size;
        } else if (parsedSize == -1) {
            return sizeIfNotParsed;
        } else {
            return parsedSize;
        }
    }

    private long calculateSampleSizes() {
        System.out.println("getting size of samples. listsize: " + sampleList.size());
        long size2 = 0;
        for (SampleHolder<T> sample : sampleList) {<commit_msg>DO NOT USE System.err.println<commit_after>/*  
 * Copyright 2008 CoreMedia AG, Hamburg
 *
 * Licensed under the Apache License, Version 2.0 (the License); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an AS IS BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */

package com.coremedia.iso.boxes;

import com.coremedia.iso.BoxParser;
import com.coremedia.iso.IsoBufferWrapper;
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.IsoOutputStream;
import com.coremedia.iso.boxes.fragment.MovieFragmentBox;
import com.coremedia.iso.boxes.fragment.TrackFragmentBox;
import com.coremedia.iso.mdta.Chunk;
import com.coremedia.iso.mdta.Sample;
import com.coremedia.iso.mdta.SampleImpl;
import com.coremedia.iso.mdta.Track;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * This box contains the media data. In video tracks, this box would contain video frames. A presentation may
 * contain zero or more Media Data Boxes. The actual media data follows the type field; its structure is described
 * by the metadata (see {@link SampleTableBox}).<br>
 * In large presentations, it may be desirable to have more data in this box than a 32-bit size would permit. In this
 * case, the large variant of the size field is used.<br>
 * There may be any number of these boxes in the file (including zero, if all the media data is in other files). The
 * metadata refers to media data by its absolute offset within the file (see {@link StaticChunkOffsetBox});
 * so Media Data Box headers and free space may easily be skipped, and files without any box structure may
 * also be referenced and used.
 */
public final class MediaDataBox<T extends TrackMetaDataContainer> extends AbstractBox {
    public static final String TYPE = "mdat";

    private boolean contentsParsed = false;

    private byte[] deadBytesBefore = new byte[0];

    private long sizeIfNotParsed;

    private long startOffset;


    private Map<Long, Track<T>> tracks = new HashMap<Long, Track<T>>();

    private List<SampleHolder<T>> sampleList = new ArrayList<SampleHolder<T>>();
    private TrackBoxContainer<TrackFragmentBox> movieFragmentBoxBefore;
    private IsoBufferWrapper isoBufferWrapper;
    private long parsedSize = -1;

    public MediaDataBox(MovieFragmentBox lastMovieFragmentBox) {
        super(IsoFile.fourCCtoBytes(TYPE));
        this.movieFragmentBoxBefore = lastMovieFragmentBox;
    }

    public boolean isContentsParsed() {
        return contentsParsed;
    }

    public byte[] getDeadBytesBefore() {
        return deadBytesBefore;
    }


    @Override
    public void getBox(IsoOutputStream os) throws IOException {
        os.write(getHeader());
        os.write(getDeadBytesBefore());
        getContent(os);
        for (ByteBuffer buffer : deadBytes) {
            buffer.rewind();
            byte[] bufAsAr = new byte[buffer.limit()];
            buffer.get(bufAsAr);
            os.write(bufAsAr);
        }

    }

  @Override
  public long getSize() {
    long contentSize = getContentSize();  // avoid calling getContentSize() twice
    long headerSize = 4 + // headerSize
            4 + // type
            (contentSize >= 4294967296L ? 8 : 0);
    return headerSize + contentSize + getDeadBytes().length + getDeadBytesBefore().length;
  }

    public long getSampleCount() {
        return sampleList.size();
    }

    public Sample<T> getSample(int index) {
        return sampleList.get(index).getSample();
    }

    public void replaceSample(int index, Sample<?> s) {
        sampleList.get(index).setSample((Sample<T>) s);
    }

    @Override
    protected long getContentSize() {
        if (parsedSize == -1 && contentsParsed) {
            long size = 0;

            for (Track<T> track : tracks.values()) {
                //System.out.println("getting size of track " + track.getTrackId());
                size += track.getSize();
            }

            //todo: not fast
            assert size == calculateSampleSizes();
            parsedSize = size;
            return size;
        } else if (parsedSize == -1) {
            return sizeIfNotParsed;
        } else {
            return parsedSize;
        }
    }

    private long calculateSampleSizes() {
        long size2 = 0;
        for (SampleHolder<T> sample : sampleList) {
{'fraction_non_alphanumeric': 0.09180631232651383, 'fraction_numerical': 0.006374010486275316, 'mean_word_length': 2.9967132292522596, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5827889', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2590, 'n_words': 895}