Skip to content

Commit

Permalink
squash: Fix seeking the original file.
Browse files Browse the repository at this point in the history
  • Loading branch information
damencho committed Dec 18, 2024
1 parent 38f491c commit f21f5d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/main/java/org/ebml/matroska/MatroskaFileTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class MatroskaFileTags

private final ArrayList<MatroskaFileTagEntry> tags = new ArrayList<>();

private long myPosition;
private long myStartPosition;
private long myEndPosition;

public void addTag(final MatroskaFileTagEntry tag)
{
Expand All @@ -28,15 +29,18 @@ public void addTag(final MatroskaFileTagEntry tag)

public long writeTags(final DataWriter ioDW)
{
myPosition = ioDW.getFilePointer();
myStartPosition = ioDW.getFilePointer();
final MasterElement tagsElem = MatroskaDocTypes.Tags.getInstance();

for (final MatroskaFileTagEntry tag : tags)
{
tagsElem.addChildElement(tag.toElement());
}

if (BLOCK_SIZE < tagsElem.getTotalSize() && ioDW.isSeekable())
if (BLOCK_SIZE < tagsElem.getTotalSize() && ioDW.isSeekable()
// do the shuffling the data only if the file is big enough to contain the data
// if it is not it means we are writing the file for the first time and we don't need to shuffle the data
&& ioDW.length() > myStartPosition + tagsElem.getTotalSize())
{
long len;

Expand All @@ -48,10 +52,11 @@ public long writeTags(final DataWriter ioDW)
len = tagsElem.writeElement(dw);

// now let's copy the rest of the original file by first setting the position after the tags
ioDW.seek(myPosition + BLOCK_SIZE);
ioDW.seek(myEndPosition);

// copy the rest of the original file
((FileDataWriter)ioDW).copyEndOfFile(dw);
myEndPosition = myStartPosition + len;
}
catch (IOException ex)
{
Expand All @@ -62,7 +67,7 @@ public long writeTags(final DataWriter ioDW)
}

long len = tagsElem.writeElement(ioDW);

myEndPosition = ioDW.getFilePointer();
if (BLOCK_SIZE > tagsElem.getTotalSize() && ioDW.isSeekable())
{
new VoidElement(BLOCK_SIZE - tagsElem.getTotalSize()).writeElement(ioDW);
Expand All @@ -76,7 +81,7 @@ public long update(final DataWriter ioDW)
{
LOG.info("Updating tags list!");
final long start = ioDW.getFilePointer();
ioDW.seek(myPosition);
ioDW.seek(myStartPosition);
long len = writeTags(ioDW);
ioDW.seek(start);
return len;
Expand All @@ -87,7 +92,9 @@ public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getPropertyName().equals(MatroskaFileTracks.RESIZED))
{
myPosition = myPosition + ((long) evt.getNewValue() - (long) evt.getOldValue());
long increase = (long) evt.getNewValue() - (long) evt.getOldValue();
myStartPosition += increase;
myEndPosition += increase;
}
}
}
17 changes: 12 additions & 5 deletions src/main/java/org/ebml/matroska/MatroskaFileTracks.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class MatroskaFileTracks

private final ArrayList<MatroskaFileTrack> tracks = new ArrayList<>();

private long myPosition;
private long myStartPosition;
private long myEndPosition;

public void addTrack(final MatroskaFileTrack track)
{
Expand All @@ -31,15 +32,18 @@ public void addTrack(final MatroskaFileTrack track)

public long writeTracks(final DataWriter ioDW)
{
myPosition = ioDW.getFilePointer();
myStartPosition = ioDW.getFilePointer();
final MasterElement tracksElem = MatroskaDocTypes.Tracks.getInstance();

for (final MatroskaFileTrack track : tracks)
{
tracksElem.addChildElement(track.toElement());
}

if (BLOCK_SIZE < tracksElem.getTotalSize() && ioDW.isSeekable())
if (BLOCK_SIZE < tracksElem.getTotalSize() && ioDW.isSeekable()
// do the shuffling the data only if the file is big enough to contain the data
// if it is not it means we are writing the file for the first time and we don't need to shuffle the data
&& ioDW.length() > myStartPosition + tracksElem.getTotalSize())
{
long len;

Expand All @@ -51,10 +55,12 @@ public long writeTracks(final DataWriter ioDW)
len = tracksElem.writeElement(dw);

// now let's copy the rest of the original file by first setting the position after the tracks
ioDW.seek(myPosition + BLOCK_SIZE);
ioDW.seek(myEndPosition);

// copy the rest of the original file
((FileDataWriter)ioDW).copyEndOfFile(dw);

myEndPosition = myStartPosition + len;
}
catch (IOException ex)
{
Expand All @@ -70,6 +76,7 @@ public long writeTracks(final DataWriter ioDW)

long size = tracksElem.writeElement(ioDW);

myEndPosition = ioDW.getFilePointer();
if (BLOCK_SIZE > tracksElem.getTotalSize() && ioDW.isSeekable())
{
new VoidElement(BLOCK_SIZE - tracksElem.getTotalSize()).writeElement(ioDW);
Expand All @@ -83,7 +90,7 @@ public long update(final DataWriter ioDW)
{
LOG.info("Updating tracks list!");
final long start = ioDW.getFilePointer();
ioDW.seek(myPosition);
ioDW.seek(myStartPosition);
long len = writeTracks(ioDW);
ioDW.seek(start);
return len;
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/ebml/matroska/MatroskaFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*/
package org.ebml.matroska;

import java.io.Closeable;
import java.io.*;
import java.nio.file.*;
import java.util.HashSet;
import java.util.Set;

import org.ebml.MasterElement;
import org.ebml.StringElement;
import org.ebml.UnsignedIntegerElement;
import org.ebml.io.DataWriter;
import org.ebml.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -89,8 +90,28 @@ void initialize()
metaSeek.addIndexedElement(MatroskaDocTypes.Tracks.getType(), ioDW.getFilePointer());
tracks.writeTracks(ioDW);

// try
// {
// Files.copy(Paths.get(((FileDataWriter)ioDW).filename),
// Paths.get("/Users/dminkov/Desktop/multitrack/large-call/rec-init-1.mka"), StandardCopyOption.REPLACE_EXISTING);
// }
// catch (IOException e)
// {
// throw new RuntimeException(e);
// }
System.err.println("11111111 " + ioDW.getFilePointer());
metaSeek.addIndexedElement(MatroskaDocTypes.Tags.getType(), ioDW.getFilePointer());
System.err.println("2222 " + ioDW.getFilePointer());
tags.writeTags(ioDW);
// try
// {
// Files.copy(Paths.get(((FileDataWriter)ioDW).filename),
// Paths.get("/Users/dminkov/Desktop/multitrack/large-call/rec-init-2.mka"), StandardCopyOption.REPLACE_EXISTING);
// }
// catch (IOException e)
// {
// throw new RuntimeException(e);
// }

// If tracks got expanded beyond the void element, tags needs to adjust its pointer
tracks.addPropertyChangeListener(tags);
Expand Down

0 comments on commit f21f5d2

Please sign in to comment.