/** * Buffers the given packet up ready for writing to the stream, but doesn't write it to disk yet. * The granule position is updated on the page. If writing the packet requires a new page, then * the updated granule position only applies to the new page */ public void bufferPacket(OggPacket packet, long granulePosition) { if (closed) { throw new IllegalStateException("Can't buffer packets on a closed stream!"); } if (!doneFirstPacket) { packet.setIsBOS(); doneFirstPacket = true; } int size = packet.getData().length; boolean emptyPacket = (size == 0); // Add to pages in turn OggPage page = getCurrentPage(false); int pos = 0; while (pos < size || emptyPacket) { pos = page.addPacket(packet, pos); if (pos < size) { page = getCurrentPage(true); page.setIsContinuation(); } page.setGranulePosition(granulePosition); emptyPacket = false; } currentGranulePosition = granulePosition; packet.setParent(page); }
private OggPage getCurrentPage(boolean forceNew) { if (buffer.size() == 0 || forceNew) { OggPage page = new OggPage(sid, sequenceNumber++); if (currentGranulePosition > 0) { page.setGranulePosition(currentGranulePosition); } buffer.add(page); return page; } return buffer.get(buffer.size() - 1); }
/** * Sets the current granule position. The granule position will be applied to all un-flushed * packets, and all future packets. As such, you should normally either call a flush just before * or just after this call. */ public void setGranulePosition(long position) { currentGranulePosition = position; for (OggPage p : buffer) { p.setGranulePosition(position); } }