Link to home
Start Free TrialLog in
Avatar of amai
amai

asked on

Transferring info from FileInputStream to ByteArrayOutputStream

Hi,

can anyone tell me how can I read a file from the file system, and transfer it into a ByteArrayOutputStream object?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
>>while((i = f.read(bb)) > 0)

should be

while((i = f.read(bb)) >= 0)
Avatar of sciuriware
sciuriware

CEHJ, if i == 0, then write 0 bytes and go on?

;JOOP!
EOF is signified by -1 and nothing else
And what will you do with a return of 0?

;JOOP!
Write it ;-)
(Otherwise your stream copy is going to be truncated before the first 0x00 that's encountered)
CEHJ, read it again carefully: I'm processing arrays NOT bytes.

;JOOP!
>>I'm processing arrays NOT bytes.

It's still preferable to check EOF IMHO, then the same idiom can be used in either case
So, my code was right and you prefer yours; not to confuse the questioner.

;JOOP!
OK -