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 f9753ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 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

0 comments on commit f9753ba

Please sign in to comment.