Problem Converting Byte Array to Datasource For JMF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shahiz
    New Member
    • Feb 2008
    • 3

    Problem Converting Byte Array to Datasource For JMF

    basically im having null pointer exception

    //read an inputstream
    is = new DataInputStream (new FileInputStream ("test.mpg") );
    loadBytes(is);

    //pass it as a datasource for the player
    public void loadBytes(Input Stream is){
    DataSource ds=new DataSource(is);
    player = Manager.createP layer(ds);

    //datasource class
    import java.io.*;
    import java.nio.ByteBu ffer;
    import javax.media.*;
    import javax.media.pro tocol.*;

    public class DataSource extends PullDataSource {
    private InputStream is;
    private byte [] bytes;

    /** Creates new MediaViewerData Source */
    public DataSource(Inpu tStream is) {
    super();
    this.is = is;
    try{
    int byteCount = is.available();
    bytes = new byte [byteCount];
    is.read(bytes);
    }
    catch (Exception e){
    System.out.prin tln(e);
    }
    }

    public PullSourceStrea m [] getStreams() {

    PullSourceStrea m [] streams = new PullSourceStrea m [1];
    InputSourceStre am iss = new InputSourceStre am(new ByteArrayInputS tream(bytes), new FileTypeDescrip tor(ContentDesc riptor.RAW));
    streams[0] = iss;
    return streams;

    }

    public void connect() {}

    public void disconnect() {}

    public String getContentType( ) {
    return new String(FileType Descriptor.MPEG );
    }

    public MediaLocator getLocator() {
    return null;
    }

    public void initCheck() {}

    public void setLocator(Medi aLocator source) {}

    public void start() {}

    public void stop() {}

    public Object getControl(Stri ng s){
    return null;
    }


    public Object [] getControls(){
    return null;
    }

    public Time getDuration(){
    return null;
    }

    }

    //inputsourcestre am class
    import java.io.InputSt ream;
    import java.io.IOExcep tion;
    import java.nio.ByteBu ffer;

    import javax.media.pro tocol.ContentDe scriptor;
    import javax.media.pro tocol.PullSourc eStream;
    import javax.media.pro tocol.Seekable;
    import javax.media.pro tocol.SourceStr eam;

    /**
    * Build a source stream out of an input stream.
    *
    * @see DataSource
    * @see SourceStream
    * @see java.io.InputSt ream
    *
    * @version %I%, %E%.
    *
    */
    public
    class InputSourceStre am implements PullSourceStrea m, Seekable {

    protected InputStream stream;
    protected boolean eosReached;
    ContentDescript or contentType;
    protected ByteBuffer inputBuffer;
    /**
    * Construct an <CODE>InputSour ceStream</CODE> from an input stream.
    *
    * @param s The input stream to build the source stream from.
    * @param type The content-type of the source stream.
    */
    public InputSourceStre am(InputStream s, ContentDescript or type) {
    stream = s;
    eosReached = false;
    contentType = type;

    }

    /**
    * Get the content type for this stream.
    *
    * @return content descriptor for the stream.
    */
    public ContentDescript or getContentDescr iptor() {
    return contentType;
    }

    /**
    * Obtain the content length
    *
    * @return content length for this stream.
    */
    public long getContentLengt h() {
    return SourceStream.LE NGTH_UNKNOWN;
    }

    /**
    * Query if the next read will block.
    *
    * @return true if a read will block.
    */
    public boolean willReadBlock() {
    if( eosReached == true) {
    return true;
    } else {
    try {
    return stream.availabl e() == 0;
    } catch (IOException e) {
    return true;
    }
    }
    }

    /**
    * Read a buffer of data.
    *
    * @param buffer The buffer to read data into.
    * @param offset The offset into the buffer for reading.
    * @param length The number of bytes to read.
    * @return The number of bytes read or -1 indicating end-of-stream.
    */
    //this where the problem lies
    //unable to read the stream into the
    // inputBuffer thus giving me the error
    public int read(byte[] buffer, int offset, int length) throws IOException { int bytesRead = stream.read(buf fer, offset, length); if( bytesRead == -1) { eosReached = true;
    }
    inputBuffer.get (buffer,offset, length);
    return bytesRead;
    }

    /**
    * Turn the stream off.
    *
    * @exception IOException Thrown if there is a problem closing the stream.
    */
    public void close() throws IOException {
    stream.close();
    }
    /**
    * Return if the end of stream has been reached.
    * @return true if the end of the stream has been reached.
    */
    // $jdr: This is a bug. Need to figure out
    // the "correct" way to determine, before a read
    // is done, if we're at EOS.
    public boolean endOfStream() {
    return eosReached;
    }

    /**
    * Returns an zero length array because no controls
    * are supported.
    *
    * @return a zero length <code>Object</code> array.
    */
    public Object[] getControls() {
    return new Object[0];
    }

    /**
    * Returns <code>null</code> because no controls are implemented.
    *
    * @return <code>null</code>.
    */
    public Object getControl(Stri ng controlName) {
    return null;
    }
    public long seek(long where) {
    try {
    inputBuffer.pos ition((int)(whe re));
    return where;
    }
    catch (IllegalArgumen tException E) {
    return this.tell(); // staying at the current position
    }
    }
    public long tell() {
    return inputBuffer.pos ition();
    }

    public void SeekableStream( ByteBuffer byteBuffer) {
    inputBuffer = byteBuffer;
    this.seek((long )(0)); // set the ByteBuffer to to beginning
    }
    public boolean isRandomAccess( ) {
    // TODO Auto-generated method stub
    return false;
    }
    }

    please take a look and i'll appreciate any assistance
    thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Read the exception trace again. Which line in your code is reported for that exception?

    Comment

    • shahiz
      New Member
      • Feb 2008
      • 3

      #3
      im sorry about not using code tags
      was my first post

      this is error from the program

      Code:
      LoadInputStream.loadBytes() - Creating DataSource (InputStream)
      LoadInputStream.loadBytes() - Creating Player
      LoadInputStream.loadBytes() - Adding Controller Listener
      LoadInputStream.loadBytes() - realizing player
      Exception in thread "JMF thread: com.sun.media.amovie.AMController@1855af5[ com.
      sun.media.amovie.AMController@1855af5 ] ( realizeThread)" java.lang.NullPointerException
              at InputSourceStream.tell(InputSourceStream.java:146)
              at com.sun.media.amovie.AMController.seek(AMController.java:361)
              at com.sun.media.amovie.ActiveMovie.seek(ActiveMovie.java:279)
              at com.sun.media.amovie.ActiveMovie.<init>(ActiveMovie.java:66)
              at com.sun.media.amovie.AMController.createActiveMovie(AMController.java:324)
              at com.sun.media.amovie.AMController.doRealize(AMController.java:416)
              at com.sun.media.RealizeWorkThread.process(BasicController.java:1400)
              at com.sun.media.StateTransitionWorkThread.run(BasicController.java:1339
      it seem that the read method in InputSourceStre am class does not read the stream
      Code:
      public int read(byte[] buffer, int offset, int length) throws IOException {	
      int bytesRead = stream.read(buffer, offset, length);
      if( bytesRead == -1) {
      eosReached = true;
      }
      inputBuffer.get(buffer,offset,length);
      return bytesRead;
      }
      this is the null pointer exception error
      Code:
      public long tell() {
      return inputBuffer.position();
      }

      Comment

      Working...