Re: [Hessianobjc-user] Fwd: Which hessian jar to use?
Status: Alpha
Brought to you by:
cocoa_dood
|
From: Tom D. <tgd...@gm...> - 2007-12-10 12:23:00
|
Here's a barely-tested fix which seems to work for me -- I haven't set
SVN up yet, so it isn't a patch...
- (NSString *) decodeStringChunk {
int len = [self decodeStringLength];
if(len == 0) {
return @"";
}
//alloc enough memory for the string +1 for null terminator
// this may be twice the number of characters + 1 for a pathological
// case where each character is a two byte character in UTF-8
uint8_t * readData = malloc((2*len+1) * sizeof(uint8_t));
if(readData == NULL) {
NSLog(@"failed to alloc memory for string with len =%i",len);
return @"";
}
//set all to null.
memset(readData,0,2*len+1);
// we need to read at least 'len' bytes, but then need to check if we
need
// to read more
[dataInputStream read:readData maxLength:len];
int totalLength = len;
int multiByteCharsFound = 0;
while ([self countUTFChars:readData length:totalLength
found:&multiByteCharsFound] < len)
{
[dataInputStream read:(readData + totalLength)
maxLength:multiByteCharsFound - (len - totalLength)];
totalLength += multiByteCharsFound - (len - totalLength);
}
NSString * retString = [NSString stringWithUTF8String:(const char
*)readData] ;
free(readData);
return retString;
}
- (int) countUTFChars:(uint8_t*)data length:(int)length found:
(int*)foundp {
*foundp = 0;
int i;
int count = 0;
for (i = 0; i < length; ++i)
{
++count;
if (data[i] > 0x7f)
{
int extra = 1;
if (data[i] > 0xdf)
{
extra = 2;
if (data[i] > 0xef)
{
extra = 3;
}
}
*foundp += extra;
i += extra;
}
}
return count;
}
|