/** * 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); }
/** * Skips forward until the first packet with a Granule Position of equal or greater than that * specified. Call {@link #getNextPacket()} to retrieve this packet. This method advances across * all streams, but only searches the specified one. * * @param sid The ID of the stream who's packets we will search * @param granulePosition The granule position we're looking for */ public void skipToGranulePosition(int sid, long granulePosition) throws IOException { OggPacket p = null; while ((p = getNextPacket()) != null) { if (p.getSid() == sid && p.getGranulePosition() >= granulePosition) { nextPacket = p; break; } } }
/** * Skips forward until the first packet with a Sequence Number of equal or greater than that * specified. Call {@link #getNextPacket()} to retrieve this packet. This method advances across * all streams, but only searches the specified one. * * @param sid The ID of the stream who's packets we will search * @param sequenceNumber The sequence number we're looking for */ public void skipToSequenceNumber(int sid, int sequenceNumber) throws IOException { OggPacket p = null; while ((p = getNextPacket()) != null) { if (p.getSid() == sid && p.getSequenceNumber() >= sequenceNumber) { nextPacket = p; break; } } }
/** * Returns the next packet with the given SID (Stream ID), or null if no more packets remain. Any * packets from other streams will be silently discarded. */ public OggPacket getNextPacketWithSid(int sid) throws IOException { OggPacket p = null; while ((p = getNextPacket()) != null) { if (p.getSid() == sid) { return p; } } return null; }
/** * Writes all pending packets to the stream, with the last one containing the End Of Stream Flag, * and then closes down. */ public void close() throws IOException { if (buffer.size() > 0) { buffer.get(buffer.size() - 1).setIsEOS(); } else { OggPacket p = new OggPacket(new byte[0]); p.setIsEOS(); bufferPacket(p); } flush(); closed = true; }