gpt4 book ai didi

ios - [(NSInputStream *)流读取 :buf maxLength:1024]; returns very huge value

转载 作者:行者123 更新时间:2023-11-29 02:04:29 27 4
gpt4 key购买 nike

在代码行

->NSInteger len = [(NSInputStream *)stream read:buf   maxLength:1024]; 

我从这个方法中获得了非常巨大的 len 值,例如:(18446744073709551615)

和崩溃

Terminating app due to uncaught exception 'NSMallocException', reason: -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)

case NSStreamEventHasBytesAvailable:

{



NSMutableData* lobjReadData = [[NSMutableData alloc] init];

NSNumber* lnumBytesRead;

uint8_t buf[1024];



NSUInteger lintReadingBufferLength = 0;

NSUInteger lintTotalBufferReadedlength = 0;

NSUInteger lintPreviousBufferReadedlength = 0;

NSUInteger lintSeenIndex = 0;



while ([(NSInputStream*)stream hasBytesAvailable])

{



lintReadingBufferLength = [(NSInputStream *)stream read:buf

maxLength:1024];



// some times i am getting very huge vaqlue of lintReadingBufferLength like

//18446744073709551615

//crashes here with crash log -> Terminating app due to uncaught exception 'NSMallocException', reason: '*** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)'



lintTotalBufferReadedlength += lintReadingBufferLength;



if(lintReadingBufferLength)

{

[lobjReadData appendBytes:(const void *)buf

length:lintReadingBufferLength];



// bytesRead is an instance variable of type NSNumber.

lnumBytesRead = [NSNumber numberWithInteger:

[lnumBytesRead integerValue]+lintReadingBufferLength];





NSArray* larrayOfBytes = [self arrayOfBytesFromData:lobjReadData];







for (NSInteger lintIndexCounter = lintPreviousBufferReadedlength; lintIndexCounter < lintTotalBufferReadedlength;

lintIndexCounter++)

{

NSObject* lobjByte = [larrayOfBytes objectAtIndex:lintIndexCounter];



NSString* lstrMessage = [NSString stringWithFormat:@"%@",lobjByte];



//doing some stuff here

}



lintPreviousBufferReadedlength = lintTotalBufferReadedlength;

}

else if(0 == lintReadingBufferLength)

{



}

else

{

SLog(@"no buffer!");

}



}



// SLog(@"--------------------------------------");



break;

}

最佳答案

184467440737095516150xffffffffffff,这是最大的无符号 64 位整数值,但它也相当于 -1 作为 64 位有符号整数。

如果您查看 [NSInputStream read:maxLength:] 的引用资料它说:

Return Value

A number indicating the outcome of the operation:

  • A positive number indicates the number of bytes read;

  • 0 indicates that the end of the buffer was reached;

  • A negative number means that the operation failed.

因此操作失败,您正在查看该值作为无符号值。

关于ios - [(NSInputStream *)流读取 :buf maxLength:1024]; returns very huge value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com