package javafx.scene.media; import java.io.IOException; import java.io.InputStream; /** * This class is used to provide media data to a {@code Media} object. */ public class MediaInputStream { /** * Enumeration of media stream types. */ public enum StreamType { /** * MP3. */ MP3, /** * MP4. */ MP4 }; private InputStream inputStream = null; protected long length = -1; protected StreamType streamType = null; public MediaInputStream(InputStream inputStream, long length, StreamType streamType) { this.inputStream = inputStream; this.length = length; this.streamType = streamType; } /** * Get stream type. * * @return stream type of media stream. */ public final StreamType getStreamType() { return streamType; } /** * Length of entire media data. * * @return length of media data or -1 if live stream. */ public final long getLength() { return length; } /** * Read media data. * This function can block, if data is not yet available. * * @param data the buffer into which the data is read. * * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. */ public int read(byte[] data) throws IOException { return inputStream.read(data); } /** * Seek media stream to new position. * This function should not block. Instead block read if data is not yet available. * * @param position seek position. * * @return true if seek was performed or false if seek failed. */ public boolean seek(long position) throws IOException { if (length == -1 || position < 0 || position > length) { return false; } inputStream.reset(); inputStream.skip(position); return true; } /** * Called when we no longer will request data. All blocked calls such as read() should be unblocked. */ public void close() throws IOException { if (inputStream != null) { inputStream.close(); inputStream = null; } } }