Skip to content

Commit fb87ddc

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

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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

+16-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,13 @@ 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;
60+
61+
ioDW.seek(myEndPosition);
5562
}
5663
catch (IOException ex)
5764
{
@@ -62,7 +69,7 @@ public long writeTags(final DataWriter ioDW)
6269
}
6370

6471
long len = tagsElem.writeElement(ioDW);
65-
72+
myEndPosition = ioDW.getFilePointer();
6673
if (BLOCK_SIZE > tagsElem.getTotalSize() && ioDW.isSeekable())
6774
{
6875
new VoidElement(BLOCK_SIZE - tagsElem.getTotalSize()).writeElement(ioDW);
@@ -76,7 +83,7 @@ public long update(final DataWriter ioDW)
7683
{
7784
LOG.info("Updating tags list!");
7885
final long start = ioDW.getFilePointer();
79-
ioDW.seek(myPosition);
86+
ioDW.seek(myStartPosition);
8087
long len = writeTags(ioDW);
8188
ioDW.seek(start);
8289
return len;
@@ -87,7 +94,9 @@ public void propertyChange(PropertyChangeEvent evt)
8794
{
8895
if (evt.getPropertyName().equals(MatroskaFileTracks.RESIZED))
8996
{
90-
myPosition = myPosition + ((long) evt.getNewValue() - (long) evt.getOldValue());
97+
long increase = (long) evt.getNewValue() - (long) evt.getOldValue();
98+
myStartPosition += increase;
99+
myEndPosition += increase;
91100
}
92101
}
93102
}

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

+14-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,14 @@ 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;
64+
65+
ioDW.seek(myEndPosition);
5866
}
5967
catch (IOException ex)
6068
{
@@ -70,6 +78,7 @@ public long writeTracks(final DataWriter ioDW)
7078

7179
long size = tracksElem.writeElement(ioDW);
7280

81+
myEndPosition = ioDW.getFilePointer();
7382
if (BLOCK_SIZE > tracksElem.getTotalSize() && ioDW.isSeekable())
7483
{
7584
new VoidElement(BLOCK_SIZE - tracksElem.getTotalSize()).writeElement(ioDW);
@@ -83,7 +92,7 @@ public long update(final DataWriter ioDW)
8392
{
8493
LOG.info("Updating tracks list!");
8594
final long start = ioDW.getFilePointer();
86-
ioDW.seek(myPosition);
95+
ioDW.seek(myStartPosition);
8796
long len = writeTracks(ioDW);
8897
ioDW.seek(start);
8998
return len;

0 commit comments

Comments
 (0)