Adrian Crum wrote:
> Adam Heath wrote:
>> Adrian Crum wrote:
>>> Adam Heath wrote:
>>>>
[hidden email] wrote:
>>>>> Author: adrianc
>>>>> Date: Wed Mar 10 17:11:18 2010
>>>>> New Revision: 921455
>>>>>
>>>>> URL:
http://svn.apache.org/viewvc?rev=921455&view=rev>>>>> Log:
>>>>> Revert rev 902341 - which seemed to cause problems with the Derby
>>>>> database.
>>>> To be fair, this is not a problem with the derby database, not at all.
>>>> It's a problem with the code.
>>> Right. I assumed the number of chars returned would be the same as the
>>> number of chars requested. I'll get back to it when I have time.
>>
>> What were you trying to accomplish?
>
> Eliminate the arbitrary buffer size. If the entire object can be read in
> one pass, then do so.
>
> We can tell from the bug reports that Derby will read a CLOB in 8k
> chunks. Other databases apparently support reading data in chunks larger
> than 8k.
>
int sizeToRead = db.getLength(id);
char[] buffer = new char[sizeToRead];
int r;
while (sizeToRead > 0 && (r = db.read(id, buffer, offset, sizeToRead))
!= -1) {
if (r == sizeToRead) {
return buffer; // convert to whatever
} else {
collector.append(buffer, offset, r);
offset += r;
sizeToRead -= r;
}
}
if (r == -1) {
throw new Exception("short read");
}
return collector; // convert to whatever