+ Start a Discussion
learnSFlearnSF 

ArrayIndexOutofBoundException in Base64 conversation in java

hi,
 
I want to decode attahcment body into normal string.I wrote code like this.
I am getting attachment body from salesforce data base.
I have checked in DB that this attachment have body content in data base.
 
byte encoded[] = Base64.decodeBase64(fullAtt.getBody());

and I am using org.apache.commons.codec.binary.Base64.java class for decode.

Here code is failing and givng ArrayIndexOutofBound exception.

Can anyone give some suggestion on it?

werewolfwerewolf
Did you put a breakpoint on that line in Java to ensure that fullAtt.getBody() actually returns something other than an empty array?
learnSFlearnSF
Yes,I did.
 
Its returning data.But not able to convert in to Byte string and fails.
SteveBowerSteveBower
Have you allocated, or just declared.  E.g. do you need a "new" in there.  Steve.

learnSFlearnSF
Here is code and I am passing attId.
 

QueryResult qrAtt=binding.query("Select a.SystemModstamp, a.ParentId, a.OwnerId, a.Name, a.LastModifiedDate, a.LastModifiedById, a.IsPrivate, a.IsDeleted, a.Id, a.CreatedDate, a.CreatedById, a.ContentType, a.BodyLength, a.Body From Attachment a where a.id="+" ' "+attId+"'");

Attachment fullAtt=(Attachment)qrAtt.getRecords(0);

byte encoded[] = Base64.decodeBase64(fullAtt.getBody());

System.out.println(encoded.toString());

Its giving me error like

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -43

I read somewhere like this for commans-codec-1.3.jar

Base64.isArrayByteBase64() throws an ArrayIndexOutOfBoundsException for negative octets. Fixes 30825 . Thanks to Piero Ottuzzi .Base64.isArrayByteBase64() throws an ArrayIndexOutOfBoundsException for negative octets. Fixes 30825 . Thanks to Piero Ottuzzi .

and this is fixed in 1.4 SNAPSHOT,But not able to get that jar.

In my case is this a reason for failing?

SuperfellSuperfell
getBody() already returns the base64 decoded data, there's nothing more to do, you get the error because you are trying to decode it twice.
learnSFlearnSF
Thanks Simon.
 
Its works for me.One more question.
How I can know form attahcment that what type of file extention is this?
 
Here this file has image,when I get that body in peronal file ,How I can know that this file has which extention ?
 
 
All this I have to do because I am not able to detach attachment from mail inside case with case detachifier.I am geeting the operation has time out error.I have lots of email with attahcment,which I nned to deatch and free some space.Can you give me some suggestion how I can overcome this problem? otherwise I have to write own functionality to do that.
 
 
 
learnSFlearnSF
I looked at data and found attchment name field ,so got filename and extention as well.