Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

cast object to Byte array

843789Feb 6 2010 — edited Feb 7 2010
Hi!

I am gettting from somewhere a List object. and there are three elements:

image(byte[])
imagename(String)
imagedescription(String)


And now i am in a big trouble because i can't convert object to byte[]. I am sending image 5kb long... but my byte[] is just 353 in length:( allways. What am i doing wrong?
public static void saveImage(List request){
		
		tracemsg(request.toString());
		
		//byte[] bytes=(byte[])request.get(0);
		
		try{
			byte[] bytes=getBytes(request.get(0));
			
			Integer b=bytes.length;
	    	        String s=b.toString();
			tracemsg(b); // 353!!!
		
    	}catch (Exception e) {
    		tracemsg(e.toString());	
    	} 
    }

-----------------------
public static byte[] getBytes(Object obj) throws java.io.IOException{
	      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
	      ObjectOutputStream oos = new ObjectOutputStream(bos); 
	      oos.writeObject(obj);
	      oos.flush(); 
	      oos.close(); 
	      bos.close();
	      byte [] data = bos.toByteArray();
	      return data;
	  }
	
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 7 2010
Added on Feb 6 2010
6 comments
724 views