net.rim.device.api.media.protocol
Class ByteArrayInputStreamDataSource

java.lang.Object
  |
  +--javax.microedition.media.protocol.DataSource
        |
        +--net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource
All Implemented Interfaces:
Controllable, SourceStream

public class ByteArrayInputStreamDataSource
extends DataSource
implements SourceStream

A DataSource implementation that provides a seekable SourceStream, given a ByteArrayInputStream.

A ByteArrayInputStreamDataSource can be passed into the Manager.createPlayer() method to create a seekable Player instance.

Example

 try {
     ByteArrayInputStream stream = (ByteArrayInputStream)this.getClass().getResourceAsStream("/abc.mp3");
     ByteArrayInputStreamDataSource source = new ByteArrayInputStreamDataSource(stream, "audio/mpeg");
     Player p = Manager.createPlayer(source);
     p.start();
 } catch (IOException ioe) {
 } catch (MediaException me) {
 }
 

Since:
JDE 4.7.0

Field Summary
 
Fields inherited from interface javax.microedition.media.protocol.SourceStream
NOT_SEEKABLE, RANDOM_ACCESSIBLE, SEEKABLE_TO_START
 
Constructor Summary
ByteArrayInputStreamDataSource(ByteArrayInputStream stream, String type)
          Creates a ByteArrayInputStreamDataSource.
 
Method Summary
 void connect()
          Open a connection to the source described by the locator and initiate communication.
 void disconnect()
          Close the connection to the source described by the locator and free resources used to maintain the connection.
 ContentDescriptor getContentDescriptor()
          Get the current type for this stream.
 long getContentLength()
          Get the size in bytes of the content on this stream.
 String getContentType()
          Get a sting that describes the content-type of the media that the source is providing.
 Control getControl(String controlType)
          Obtain the object that implements the specified Control interface.
 Control[] getControls()
          Obtain the collection of Controls from the object that implements this interface.
 int getSeekType()
          Find out if the stream is seekable.
 SourceStream[] getStreams()
          Get the collection of streams that this source manages.
 int getTransferSize()
          Get the size of a "logical" chunk of media data from the source.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
 long seek(long where)
          Seek to the specified point in the stream.
 void start()
          Initiate data-transfer.
 void stop()
          Stop the data-transfer.
 long tell()
          Obtain the current position in the stream.
 
Methods inherited from class javax.microedition.media.protocol.DataSource
getLocator
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteArrayInputStreamDataSource

public ByteArrayInputStreamDataSource(ByteArrayInputStream stream,
                                      String type)
Creates a ByteArrayInputStreamDataSource.
Parameters:
stream - A ByteArrayInputStream containing the entire contents of the media file to be played by this DataSource implementation.
type - The content type of the media.
Since:
JDE 4.7.0
Method Detail

connect

public void connect()
             throws IOException
Description copied from class: DataSource
Open a connection to the source described by the locator and initiate communication.
Overrides:
connect in class DataSource
Since:
JDE 4.7.0

Following copied from class: DataSource
Throws:
IOException - Thrown if there are IO problems when connect is called.
SecurityException - Thrown if the caller does not have security permission to call connect.

disconnect

public void disconnect()
Description copied from class: DataSource
Close the connection to the source described by the locator and free resources used to maintain the connection.

If no resources are in use, disconnect is ignored. If stop hasn't already been called, callling disconnect implies a stop.

Overrides:
disconnect in class DataSource
Since:
JDE 4.7.0

getContentDescriptor

public ContentDescriptor getContentDescriptor()
Description copied from interface: SourceStream
Get the current type for this stream.
Since:
JDE 4.7.0

Following copied from interface: SourceStream
Returns:
The current ContentDescriptor for this stream.

getContentLength

public long getContentLength()
Description copied from interface: SourceStream
Get the size in bytes of the content on this stream.
Since:
JDE 4.7.0

Following copied from interface: SourceStream
Returns:
The content length in bytes. -1 is returned if the length is not known.

getContentType

public String getContentType()
Description copied from class: DataSource
Get a sting that describes the content-type of the media that the source is providing.
Overrides:
getContentType in class DataSource
Since:
JDE 4.7.0

Following copied from class: DataSource
Returns:
The name that describes the media content. Returns null if the content is unknown.
Throws:
IllegalStateException - Thrown if the source is not connected.

getControl

public Control getControl(String controlType)
Description copied from interface: Controllable
Obtain the object that implements the specified Control interface.

If the specified Control interface is not supported then null is returned.

If the Controllable supports multiple objects that implement the same specified Control interface, only one of them will be returned. To obtain all the Control's of that type, use the getControls method and check the list for the requested type.

Since:
JDE 4.7.0

Following copied from interface: Controllable
Parameters:
controlType - the class name of the Control. The class name should be given either as the fully-qualified name of the class; or if the package of the class is not given, the package javax.microedition.media.control is assumed.
Returns:
the object that implements the control, or null.
Throws:
IllegalArgumentException - Thrown if controlType is null.
IllegalStateException - Thrown if getControl is called in a wrong state. See Player for more details.

getControls

public Control[] getControls()
Description copied from interface: Controllable
Obtain the collection of Controls from the object that implements this interface.

Since a single object can implement multiple Control interfaces, it's necessary to check each object against different Control types. For example:

Controllable controllable;
    :
Control cs[];
cs = controllable.getControls();
for (int i = 0; i < cs.length; i++) {
    if (cs[i] instanceof ControlTypeA)
        doSomethingA();
    if (cs[i] instanceof ControlTypeB)
        doSomethingB();
    // etc.
}

The list of Control objects returned will not contain any duplicates. And the list will not change over time.

If no Control is supported, a zero length array is returned.

Since:
JDE 4.7.0

Following copied from interface: Controllable
Returns:
the collection of Control objects.
Throws:
IllegalStateException - Thrown if getControls is called in a wrong state. See Player for more details.

getSeekType

public int getSeekType()
Description copied from interface: SourceStream
Find out if the stream is seekable. The return value can be one of these three: NOT_SEEKABLE, SEEKABLE_TO_START and RANDOM_ACCESSIBLE. If the return value is SEEKABLE_TO_START, it means that the stream can only be repositioned to the beginning of the stream. If the return value is RANDOM_ACCESSIBLE, the stream can be seeked anywhere within the stream.
Since:
JDE 4.7.0

Following copied from interface: SourceStream
Returns:
Returns an enumerated value to indicate the level of seekability.

getStreams

public SourceStream[] getStreams()
Description copied from class: DataSource
Get the collection of streams that this source manages. The collection of streams is entirely content dependent. The MIME type of this DataSource provides the only indication of what streams may be available on this connection.
Overrides:
getStreams in class DataSource
Since:
JDE 4.7.0

Following copied from class: DataSource
Returns:
The collection of streams for this source.
Throws:
IllegalStateException - Thrown if the source is not connected.

getTransferSize

public int getTransferSize()
Description copied from interface: SourceStream
Get the size of a "logical" chunk of media data from the source. This method can be used to determine the minimum size of the buffer to use in conjunction with the read method to read data from the source.
Since:
JDE 4.7.0

Following copied from interface: SourceStream
Returns:
The minimum size of the buffer needed to read a "logical" chunk of data from the source. Returns -1 if the size cannot be determined.
See Also:
SourceStream.read(byte[], int, int)

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Description copied from interface: SourceStream
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.

Since:
JDE 4.7.0

Following copied from interface: SourceStream
Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
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.
Throws:
IOException - if an I/O error occurs.

seek

public long seek(long where)
          throws IOException
Description copied from interface: SourceStream
Seek to the specified point in the stream. The seek method may, for a variety of reasons, fail to seek to the specified position. For example, it may be asked to seek to a position beyond the size of the stream; or the stream may only be seekable to the beginning (getSeekType returns SEEKABLE_TO_START). The return value indicates whether seeking is successful. If it is successful, the value returned will be the same as the given position. Otherwise, the return value will indicate what the new position is.

If the given position is negative, seek will treat that as 0 and attempt to seek to 0.

An IOException will be thrown if an I/O error occurs, e.g. when the stream comes from a remote connection and the connection is broken.

Since:
JDE 4.7.0

Following copied from interface: SourceStream
Parameters:
where - The position to seek to.
Returns:
The new stream position.

start

public void start()
           throws IOException
Description copied from class: DataSource
Initiate data-transfer. The start method must be called before data is available for reading.
Overrides:
start in class DataSource
Since:
JDE 4.7.0

Following copied from class: DataSource
Throws:
IllegalStateException - Thrown if the DataSource is not connected.
IOException - Thrown if the DataSource cannot be started due to some IO problems.
SecurityException - Thrown if the caller does not have security permission to call start.

stop

public void stop()
          throws IOException
Description copied from class: DataSource
Stop the data-transfer. If the DataSource has not been connected and started, stop is ignored.
Overrides:
stop in class DataSource
Since:
JDE 4.7.0

Following copied from class: DataSource
Throws:
IOException - Thrown if the DataSource cannot be stopped due to some IO problems.

tell

public long tell()
Description copied from interface: SourceStream
Obtain the current position in the stream.
Since:
JDE 4.7.0

Following copied from interface: SourceStream
Returns:
The current position in the stream.



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.