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 { /** * Auto detect stream type. */ AUTO_DETECT, /** * MPEG-1, 2, 2.5 raw audio stream possibly with ID3 metadata v2.3 or v2.4. */ MP3, /** * MPEG-4 Part 14. */ MP4 }; private InputStream inputStream = null; private StreamType streamType = StreamType.AUTO_DETECT; private long length = -1; private boolean enableProgressBuffer = true; /** * Constructs a MediaInputStream instance. */ public MediaInputStream() { } /** * Constructs a MediaInputStream instance with media data read from the specified input stream. * * @param inputStream the stream from which to read media data. * @param streamType the type of media data. Use StreamType.AUTO_DETECT or null for auto detect stream type. * @param length the length of entire media data; * * @throws NullPointerException if the InputStream is null. */ public MediaInputStream(InputStream inputStream, StreamType streamType, long length) { if (inputStream == null) { throw new NullPointerException(); } this.inputStream = inputStream; this.streamType = streamType; this.length = length; } /** * Get stream type. Default value is StreamType.AUTO_DETECT. * * @return the stream type of media data. */ public final StreamType getStreamType() { return streamType; } /** * Get length of entire media data. Default value is -1. * * @return the length of entire media data. */ public final long getLength() { return length; } /** * Get whether to enable progress buffer internally or not. Default value is true. * {@link MediaInputStream.seek} should be implemented if enableProgressBuffer() will return false. */ public final boolean enableProgressBuffer() { return enableProgressBuffer; } /** * 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); } /** * Read media data from specified position. * This function can block, if data is not yet available. * Implement this method if you need advanced functionality such as metadata support. * * @param data the buffer into which the data is read. * @param position the byte position from which the data should be 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 synchronized int read(byte[] data, long position) throws IOException { return -1; } /** * Seek media stream to new position. * This function should not block. Instead block read if media 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 { return false; } /** * 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; } } /** * Set stream type. * * @param streamType the stream type of media data. */ protected final void setStreamType(StreamType streamType) { this.streamType = streamType; } /** * Set whether to enable progress buffer internally or not. * * @param enableProgressBuffer indicates whether to enable progress buffer or not. */ protected final void setEnableProgressBuffer(boolean enableProgressBuffer) { this.enableProgressBuffer = enableProgressBuffer; } /** * Set length of entire media data. * * @param length the length of entire media data. */ protected final void setLength(long length) { this.length = length; } }