Skip to content

Commit 4c840da

Browse files
committed
OggPacketWriter getter for current unflushed page size
Allow access to the size, including header, of the current unflushed page. Based on 9d2be26 from @andrm
1 parent 5b21696 commit 4c840da

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

core/src/main/java/org/gagravarr/ogg/OggPacketWriter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ public int getSizePendingFlush() {
143143
return size;
144144
}
145145

146+
/**
147+
* Returns the size of the page currently being written
148+
* to, including its headers.
149+
* For a new stream, or a stream that has just been
150+
* flushed, will return zero.
151+
* @return Current page size, or 0 if no current page
152+
*/
153+
public int getCurrentPageSize() {
154+
if (buffer.isEmpty()) return 0;
155+
156+
OggPage p = buffer.get( buffer.size()-1 );
157+
return p.getPageSize();
158+
}
159+
146160
/**
147161
* Writes all pending packets to the stream,
148162
* splitting across pages as needed.

core/src/test/java/org/gagravarr/ogg/TestBasicWrite.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,16 @@ public void testEmptyPages() throws IOException {
9191
// Add an empty packet
9292
p = new OggPacket(new byte[0]);
9393
w.bufferPacket(p);
94+
assertEquals(0, w.getSizePendingFlush()); // Excludes headers
95+
assertEquals(28, w.getCurrentPageSize()); // Includes
9496
w.flush();
9597

9698
// And a packet with something in it,
9799
// and with a granule position
98100
p = new OggPacket(new byte[] {22});
99101
w.bufferPacket(p, 54321l);
102+
assertEquals(1, w.getSizePendingFlush()); // Excludes headers
103+
assertEquals(29, w.getCurrentPageSize()); // Includes
100104
w.close();
101105

102106
// Check again
@@ -111,6 +115,7 @@ public void testEmptyPages() throws IOException {
111115
assertEquals(0, p.getGranulePosition());
112116
assertEquals(0, p.getSequenceNumber());
113117
assertEquals(0, p.getData().length);
118+
assertEquals(28, p.getOverheadBytes());
114119

115120
p = r.getNextPacket();
116121
assertNotNull(p);
@@ -120,6 +125,7 @@ public void testEmptyPages() throws IOException {
120125
assertEquals(0, p.getGranulePosition());
121126
assertEquals(0, p.getSequenceNumber());
122127
assertEquals(0, p.getData().length);
128+
assertEquals(28, p.getOverheadBytes());
123129

124130
p = r.getNextPacket();
125131
assertNotNull(p);
@@ -129,6 +135,7 @@ public void testEmptyPages() throws IOException {
129135
assertEquals(54321l, p.getGranulePosition());
130136
assertEquals(1, p.getSequenceNumber());
131137
assertEquals(1, p.getData().length);
138+
assertEquals(28, p.getOverheadBytes());
132139

133140
assertNull(r.getNextPacket());
134141
}

0 commit comments

Comments
 (0)