Skip to content

Commit f9753ba

Browse files
committed
squash: Fix seeking the original file.
1 parent 38f491c commit f9753ba

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/main/java/org/ebml/matroska/MatroskaFileTags.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class MatroskaFileTags
1919

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

22-
private long myPosition;
22+
private long myStartPosition;
23+
private long myEndPosition;
2324

2425
public void addTag(final MatroskaFileTagEntry tag)
2526
{
@@ -28,15 +29,18 @@ public void addTag(final MatroskaFileTagEntry tag)
2829

2930
public long writeTags(final DataWriter ioDW)
3031
{
31-
myPosition = ioDW.getFilePointer();
32+
myStartPosition = ioDW.getFilePointer();
3233
final MasterElement tagsElem = MatroskaDocTypes.Tags.getInstance();
3334

3435
for (final MatroskaFileTagEntry tag : tags)
3536
{
3637
tagsElem.addChildElement(tag.toElement());
3738
}
3839

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

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

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

5357
// copy the rest of the original file
5458
((FileDataWriter)ioDW).copyEndOfFile(dw);
59+
myEndPosition = myStartPosition + len;
5560
}
5661
catch (IOException ex)
5762
{
@@ -62,7 +67,7 @@ public long writeTags(final DataWriter ioDW)
6267
}
6368

6469
long len = tagsElem.writeElement(ioDW);
65-
70+
myEndPosition = ioDW.getFilePointer();
6671
if (BLOCK_SIZE > tagsElem.getTotalSize() && ioDW.isSeekable())
6772
{
6873
new VoidElement(BLOCK_SIZE - tagsElem.getTotalSize()).writeElement(ioDW);
@@ -76,7 +81,7 @@ public long update(final DataWriter ioDW)
7681
{
7782
LOG.info("Updating tags list!");
7883
final long start = ioDW.getFilePointer();
79-
ioDW.seek(myPosition);
84+
ioDW.seek(myStartPosition);
8085
long len = writeTags(ioDW);
8186
ioDW.seek(start);
8287
return len;
@@ -87,7 +92,9 @@ public void propertyChange(PropertyChangeEvent evt)
8792
{
8893
if (evt.getPropertyName().equals(MatroskaFileTracks.RESIZED))
8994
{
90-
myPosition = myPosition + ((long) evt.getNewValue() - (long) evt.getOldValue());
95+
long increase = (long) evt.getNewValue() - (long) evt.getOldValue();
96+
myStartPosition += increase;
97+
myEndPosition += increase;
9198
}
9299
}
93100
}

src/main/java/org/ebml/matroska/MatroskaFileTracks.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class MatroskaFileTracks
2222

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

25-
private long myPosition;
25+
private long myStartPosition;
26+
private long myEndPosition;
2627

2728
public void addTrack(final MatroskaFileTrack track)
2829
{
@@ -31,15 +32,18 @@ public void addTrack(final MatroskaFileTrack track)
3132

3233
public long writeTracks(final DataWriter ioDW)
3334
{
34-
myPosition = ioDW.getFilePointer();
35+
myStartPosition = ioDW.getFilePointer();
3536
final MasterElement tracksElem = MatroskaDocTypes.Tracks.getInstance();
3637

3738
for (final MatroskaFileTrack track : tracks)
3839
{
3940
tracksElem.addChildElement(track.toElement());
4041
}
4142

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

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

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

5660
// copy the rest of the original file
5761
((FileDataWriter)ioDW).copyEndOfFile(dw);
62+
63+
myEndPosition = myStartPosition + len;
5864
}
5965
catch (IOException ex)
6066
{
@@ -70,6 +76,7 @@ public long writeTracks(final DataWriter ioDW)
7076

7177
long size = tracksElem.writeElement(ioDW);
7278

79+
myEndPosition = ioDW.getFilePointer();
7380
if (BLOCK_SIZE > tracksElem.getTotalSize() && ioDW.isSeekable())
7481
{
7582
new VoidElement(BLOCK_SIZE - tracksElem.getTotalSize()).writeElement(ioDW);
@@ -83,7 +90,7 @@ public long update(final DataWriter ioDW)
8390
{
8491
LOG.info("Updating tracks list!");
8592
final long start = ioDW.getFilePointer();
86-
ioDW.seek(myPosition);
93+
ioDW.seek(myStartPosition);
8794
long len = writeTracks(ioDW);
8895
ioDW.seek(start);
8996
return len;

0 commit comments

Comments
 (0)