Ejemplo n.º 1
0
  /**
   * 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);
  }