users@jersey.java.net

Re: [Jersey] Multipart 1.0.3 not finding message body reader

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 22 Apr 2009 15:57:04 +0200

On Apr 22, 2009, at 3:55 PM, Ari Heino wrote:

>
> Thanks, that fixed it.
>
> I quess with JavaMail library the source.available() returned the
> buffer
> length still and that's why i didn't notice to change the reading
> style of
> the stream.
>
> Cheers :)
>
>

Great!

Paul.

> Paul Sandoz wrote:
>>
>> Hi,
>>
>> I think your issue is the following:
>>
>> http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#available()
>>
>> byte[] bytes = new byte[source.available()];
>> source.read(bytes, 0, source.available());
>> dto.setDataBytes(bytes);
>>
>> the value returned from available can be '0' and MIMEPull returns 0.
>> The value indicates the number of bytes that can be read *without
>> blocking*. It does not indicate the number of bytes that can be read
>> in one call to read or the total number of bytes the stream contains.
>>
>> You need to do something like this:
>>
>> private byte[] read(InputStream in) throws IOException {
>> System.out.println(in.available());
>> ByteArrayOutputStream baos = new ByteArrayOutputStream();
>> byte[] buffer = new byte[4096];
>> int read = -1;
>> while ((read = in.read(buffer)) != -1) {
>> baos.write(buffer, 0, read);
>> }
>>
>> return baos.toByteArray();
>> }
>>
>> Or instead just do:
>>
>> byte[] bytes =
>> multiPart.getBodyParts().get(1).getEntityAs(byte[].class);
>>
>> Paul.
>>
>> Also as of 1.0.3 you no longer need to call cleanup. It is done
>> automatically.
>> On Apr 22, 2009, at 2:23 PM, Ari Heino wrote:
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Multipart-1.0.3-not-finding-message-body-reader-tp2668407p2676327.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>