/** * Searches for the next MPEG frame header from the current stream position on. This method * advances the underlying input stream until it finds a valid frame header or the end of the * stream is reached. In the former case a corresponding {@code AudioFrame} object is created. In * the latter case there are no more headers, so the end of the stream is probably reached. * * @return the next {@code AudioFrame} or <b>null</b> * @throws IOException if an IO error occurs */ public AudioFrame nextFrame() throws IOException { AudioFrame frame = null; while (!endOfStream && frame == null) { findFrameSyncByte(); if (!endOfStream) { HeaderBitField headerField = createHeaderField(); if (!endOfStream) { frame = createHeader(headerField); if (frame == null) { pushBack(headerField); } else { System.arraycopy(headerField.toArray(), 0, header, 0, 3); } } } } currentHeader = frame; return frame; }
/** * Pushes the given header field back in the stream so that the bytes are read again. This method * is called if an invalid header was detected. Then search has to continue at the next byte after * the frame sync byte. * * @param field the header bit field with the invalid frame header * @throws IOException if an error occurs */ private void pushBack(HeaderBitField field) throws IOException { unread(field.toArray()); }