Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Getting wrong output from getResourceAsStream() method.

 

Hello everyone, I'm getting the wrong output from the following code when I try to use getResourceAsStream() method. Following is my code: 

 

public class MainTest extends Application
{
  public static void main(String[] args)
  {
	try {    	
    	Class clas = null;				
		clas = Class.forName("com.test.MainTest");		
    	
		ByteArrayInputStream stream = (ByteArrayInputStream)clas.getClass().getResourceAsStream("/hey_beautiful.mp3");
    	InputStreamReader isr = new InputStreamReader(stream);
        
    	byte[] data = new byte[1024];
      	int len = 0;                	
      	while ((len = isr.read()) != -1)
      	{
      		db.write(data, 0, len);
      	}     
		fis = new ByteArrayInputStream(db.getArray());
		this.valid = loadHeader(fis);
		
		//other code		
	} catch (FileNotFoundException e) {
    	System.out.println("====FileNotFoundException: "+e.getMessage());
    }
    catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
    	System.out.println("====ClassNotFoundException: "+e.getMessage());
	}
     catch (IOException e) {
		// TODO Auto-generated catch block
    	 System.out.println("====IOException: "+e.getMessage());
	}
  }
	
	
	 private boolean loadHeader(InputStream inputStream)
  {
     byte[] headerBuffer = new byte[44];
    try {
		   System.out.println(inputStream.read(headerBuffer));

		   int pointer = 0;		  
		   this.chunkId = new String(new byte[] { headerBuffer[(pointer++)],   //line 64
			 headerBuffer[(pointer++)], headerBuffer[(pointer++)], 
			 headerBuffer[(pointer++)] });

		   this.chunkSize = 
			 (headerBuffer[(pointer++)] & 0xFF | 
			 (headerBuffer[(pointer++)] & 0xFF) << 8 | 
			 (headerBuffer[(pointer++)] & 0xFF) << 16 | 
			 headerBuffer[(pointer++)] & 0xFF000000);
		   this.format = new String(new byte[] { headerBuffer[(pointer++)], 
			 headerBuffer[(pointer++)], headerBuffer[(pointer++)], 
			 headerBuffer[(pointer++)] });
		   this.subChunk1Id = new String(new byte[] { headerBuffer[(pointer++)], 
			 headerBuffer[(pointer++)], headerBuffer[(pointer++)], 
			headerBuffer[(pointer++)] });
	  } catch (IOException e)
      {
		   e.printStackTrace();
		   return false;
      }
  }
}

 

In the line 6 above, for every headerBuffer[(pointer++)], I'm getting "0" as the output. Can anybody point out where I'm wrong. Thanks.

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting wrong output from getResourceAsStream() method.

Check the debug messages when your code executes the line:

getResourceAsStream(...)

 

From memory, you should see messages indicating if the BB found the file or not.

 

But in fact, I think you are confusing the issue with the multitude of Input streams you are using.  Why not just open the file as an InputStream, then read it into a byte array using

IOUtilities.streamToBytes(..).

 

Then you can convert these bytes to a ByteArrayInputStream if you want.