for (int i=0; i<framesPerPacket; i++) { encoder.processData(prebuf, prepos, frameSize);
while (packetCount < packetsPerOggPage) { for (int i=0; i<framesPerPacket; i++) { encoder.processData(prebuf, prepos, frameSize); prepos += frameSize; } int size = encoder.getProcessedDataByteSize(); while ((buf.length - oggCount) < size) { int nsz = buf.length * 2; byte[] nbuf = new byte[nsz]; System.arraycopy(buf, 0, nbuf, 0, oggCount); buf = nbuf; } buf[count + 27 + packetCount] = (byte)(0xff & size); encoder.getProcessedData(buf, oggCount); oggCount += size; packetCount++;
  protected void fill()    throws IOException  {    makeSpace();    if (first) {      writeHeaderFrame();      writeCommentFrame();      first = false;    }    while (true) {      int n = in.read(prebuf, precount, prebuf.length - precount);      if (n < 0) { // inputstream has ended        if ((precount-prepos) % 2 != 0) { // we don't have a complete last PCM sample          throw new StreamCorruptedException("Incompleted last PCM sample when stream ended");        }        while (prepos < precount) { // still data to encode          if ((precount - prepos) < framesPerPacket*frameSize) {            // fill end of frame with zeros            if ((prebuf.length - prepos) < framesPerPacket*frameSize) {              // grow prebuf              int nsz = prepos + framesPerPacket*frameSize;              byte[] nbuf = new byte[nsz];              System.arraycopy(prebuf, 0, nbuf, 0, precount);              prebuf = nbuf;            }            for (;precount < (prepos+framesPerPacket*frameSize); precount++) {              prebuf[precount] = 0;            }          }          if (packetCount == 0) {            writeOggPageHeader(packetsPerOggPage, 0);          }          for (int i=0; i<framesPerPacket; i++) {            encoder.processData(prebuf, prepos, frameSize);          }          prepos += framesPerPacket*frameSize;          int size = encoder.getProcessedDataByteSize();          while ((buf.length - oggCount) < size) { // grow buffer            int nsz = buf.length * 2;            byte[] nbuf = new byte[nsz];            System.arraycopy(buf, 0, nbuf, 0, oggCount);            buf = nbuf;          }          buf[count + 27 + packetCount] = (byte)(0xff & size);          encoder.getProcessedData(buf, oggCount);          oggCount += size;          packetCount++;          if (packetCount >= packetsPerOggPage) {            writeOggPageChecksum();            return;          }        }        if (packetCount > 0) {          // we have less than the normal number of packets in this page.          buf[count+5] = (byte)(0xff & 4); // set page header type to end of stream          buf[count+26] = (byte)(0xff & packetCount);          System.arraycopy(buf, count+27+packetsPerOggPage,                           buf, count+27+packetCount,                           oggCount-(count+27+packetsPerOggPage));          oggCount -= packetsPerOggPage-packetCount;          writeOggPageChecksum();        }        return;      }      else if (n > 0) {        precount += n;        if ((precount - prepos) >= framesPerPacket*frameSize) { // enough data to encode frame          while ((precount - prepos) >= framesPerPacket*frameSize) { // lets encode all we can            if (packetCount == 0) {              writeOggPageHeader(packetsPerOggPage, 0);            }            for (int i=0; i<framesPerPacket; i++) {              encoder.processData(prebuf, prepos, frameSize);            }            prepos += framesPerPacket*frameSize;            int size = encoder.getProcessedDataByteSize();            while ((buf.length - oggCount) < size) { // grow buffer              int nsz = buf.length * 2;              byte[] nbuf = new byte[nsz];              System.arraycopy(buf, 0, nbuf, 0, oggCount);              buf = nbuf;            }            buf[count + 27 + packetCount] = (byte)(0xff & size);            encoder.getProcessedData(buf, oggCount);            oggCount += size;            packetCount++;            if (packetCount >= packetsPerOggPage) {              writeOggPageChecksum();            }          }          System.arraycopy(prebuf, prepos, prebuf, 0, precount-prepos);          precount -= prepos;          prepos = 0;          if (packetCount >= packetsPerOggPage) {            writeOggPageChecksum();            // we have encoded some data (all that we could),            // so we can leave now, otherwise we return to a potentially            // blocking read of the underlying inputstream.            return;          }        }      }      else { // n == 0        // read 0 bytes from underlying stream yet it is not finished.        if (precount >= prebuf.length) {          // no more room in buffer          if (prepos > 0) {            // free some space            System.arraycopy(prebuf, prepos, prebuf, 0, precount-prepos);            precount -= prepos;            prepos = 0;          }          else {            // we could grow the pre-buffer but that risks in turn growing the            // buffer which could lead sooner or later to an            // OutOfMemoryException.             return;          }        }        else {          return;        }      }    }  }