473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem Converting Byte Array to Datasource For JMF

3
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(InputStream is){
DataSource ds=new DataSource(is);
player = Manager.createPlayer(ds);

//datasource class
import java.io.*;
import java.nio.ByteBuffer;
import javax.media.*;
import javax.media.protocol.*;

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

/** Creates new MediaViewerDataSource */
public DataSource(InputStream is) {
super();
this.is = is;
try{
int byteCount = is.available();
bytes = new byte [byteCount];
is.read(bytes);
}
catch (Exception e){
System.out.println(e);
}
}

public PullSourceStream [] getStreams() {

PullSourceStream [] streams = new PullSourceStream [1];
InputSourceStream iss = new InputSourceStream(new ByteArrayInputStream(bytes), new FileTypeDescriptor(ContentDescriptor.RAW));
streams[0] = iss;
return streams;

}

public void connect() {}

public void disconnect() {}

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

public MediaLocator getLocator() {
return null;
}

public void initCheck() {}

public void setLocator(MediaLocator source) {}

public void start() {}

public void stop() {}

public Object getControl(String s){
return null;
}


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

public Time getDuration(){
return null;
}

}

//inputsourcestream class
import java.io.InputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.PullSourceStream;
import javax.media.protocol.Seekable;
import javax.media.protocol.SourceStream;

/**
* Build a source stream out of an input stream.
*
* @see DataSource
* @see SourceStream
* @see java.io.InputStream
*
* @version %I%, %E%.
*
*/
public
class InputSourceStream implements PullSourceStream, Seekable {

protected InputStream stream;
protected boolean eosReached;
ContentDescriptor contentType;
protected ByteBuffer inputBuffer;
/**
* Construct an <CODE>InputSourceStream</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 InputSourceStream(InputStream s, ContentDescriptor type) {
stream = s;
eosReached = false;
contentType = type;

}

/**
* Get the content type for this stream.
*
* @return content descriptor for the stream.
*/
public ContentDescriptor getContentDescriptor() {
return contentType;
}

/**
* Obtain the content length
*
* @return content length for this stream.
*/
public long getContentLength() {
return SourceStream.LENGTH_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.available() == 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(buffer, 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(String controlName) {
return null;
}
public long seek(long where) {
try {
inputBuffer.position((int)(where));
return where;
}
catch (IllegalArgumentException E) {
return this.tell(); // staying at the current position
}
}
public long tell() {
return inputBuffer.position();
}

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
Feb 21 '08 #1
2 11559
r035198x
13,262 8TB
1.) Please use code tags when posting code.
2.) Read the exception trace again. Which line in your code is reported for that exception?
Feb 21 '08 #2
shahiz
3
im sorry about not using code tags
was my first post

this is error from the program

Expand|Select|Wrap|Line Numbers
  1. LoadInputStream.loadBytes() - Creating DataSource (InputStream)
  2. LoadInputStream.loadBytes() - Creating Player
  3. LoadInputStream.loadBytes() - Adding Controller Listener
  4. LoadInputStream.loadBytes() - realizing player
  5. Exception in thread "JMF thread: com.sun.media.amovie.AMController@1855af5[ com.
  6. sun.media.amovie.AMController@1855af5 ] ( realizeThread)" java.lang.NullPointerException
  7.         at InputSourceStream.tell(InputSourceStream.java:146)
  8.         at com.sun.media.amovie.AMController.seek(AMController.java:361)
  9.         at com.sun.media.amovie.ActiveMovie.seek(ActiveMovie.java:279)
  10.         at com.sun.media.amovie.ActiveMovie.<init>(ActiveMovie.java:66)
  11.         at com.sun.media.amovie.AMController.createActiveMovie(AMController.java:324)
  12.         at com.sun.media.amovie.AMController.doRealize(AMController.java:416)
  13.         at com.sun.media.RealizeWorkThread.process(BasicController.java:1400)
  14.         at com.sun.media.StateTransitionWorkThread.run(BasicController.java:1339
it seem that the read method in InputSourceStream class does not read the stream
Expand|Select|Wrap|Line Numbers
  1. public int read(byte[] buffer, int offset, int length) throws IOException {    
  2. int bytesRead = stream.read(buffer, offset, length);
  3. if( bytesRead == -1) {
  4. eosReached = true;
  5. }
  6. inputBuffer.get(buffer,offset,length);
  7. return bytesRead;
  8. }
this is the null pointer exception error
Expand|Select|Wrap|Line Numbers
  1. public long tell() {
  2. return inputBuffer.position();
  3. }
Feb 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
5
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
3
by: Joshua Russell | last post by:
Hi, I've got a program (see source below) that makes a file and fills it with random binary values (from 0 to 255). The source below works, however the program creates files at a rate of about...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.