@@ -19,7 +19,8 @@ public class MatroskaFileTags
19
19
20
20
private final ArrayList <MatroskaFileTagEntry > tags = new ArrayList <>();
21
21
22
- private long myPosition ;
22
+ private long myStartPosition ;
23
+ private long myEndPosition ;
23
24
24
25
public void addTag (final MatroskaFileTagEntry tag )
25
26
{
@@ -28,15 +29,18 @@ public void addTag(final MatroskaFileTagEntry tag)
28
29
29
30
public long writeTags (final DataWriter ioDW )
30
31
{
31
- myPosition = ioDW .getFilePointer ();
32
+ myStartPosition = ioDW .getFilePointer ();
32
33
final MasterElement tagsElem = MatroskaDocTypes .Tags .getInstance ();
33
34
34
35
for (final MatroskaFileTagEntry tag : tags )
35
36
{
36
37
tagsElem .addChildElement (tag .toElement ());
37
38
}
38
39
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 ())
40
44
{
41
45
long len ;
42
46
@@ -48,10 +52,11 @@ public long writeTags(final DataWriter ioDW)
48
52
len = tagsElem .writeElement (dw );
49
53
50
54
// 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 );
52
56
53
57
// copy the rest of the original file
54
58
((FileDataWriter )ioDW ).copyEndOfFile (dw );
59
+ myEndPosition = myStartPosition + len ;
55
60
}
56
61
catch (IOException ex )
57
62
{
@@ -62,7 +67,7 @@ public long writeTags(final DataWriter ioDW)
62
67
}
63
68
64
69
long len = tagsElem .writeElement (ioDW );
65
-
70
+ myEndPosition = ioDW . getFilePointer ();
66
71
if (BLOCK_SIZE > tagsElem .getTotalSize () && ioDW .isSeekable ())
67
72
{
68
73
new VoidElement (BLOCK_SIZE - tagsElem .getTotalSize ()).writeElement (ioDW );
@@ -76,7 +81,7 @@ public long update(final DataWriter ioDW)
76
81
{
77
82
LOG .info ("Updating tags list!" );
78
83
final long start = ioDW .getFilePointer ();
79
- ioDW .seek (myPosition );
84
+ ioDW .seek (myStartPosition );
80
85
long len = writeTags (ioDW );
81
86
ioDW .seek (start );
82
87
return len ;
@@ -87,7 +92,9 @@ public void propertyChange(PropertyChangeEvent evt)
87
92
{
88
93
if (evt .getPropertyName ().equals (MatroskaFileTracks .RESIZED ))
89
94
{
90
- myPosition = myPosition + ((long ) evt .getNewValue () - (long ) evt .getOldValue ());
95
+ long increase = (long ) evt .getNewValue () - (long ) evt .getOldValue ();
96
+ myStartPosition += increase ;
97
+ myEndPosition += increase ;
91
98
}
92
99
}
93
100
}
0 commit comments