[(NSInputStream *)stream read:buf maxLength:1024]; returns 非常巨大的价值
[(NSInputStream *)stream read:buf maxLength:1024]; returns very huge value
在代码行中
->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;
}
18446744073709551615
是 0xffffffffffff
,这是最大的无符号 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.
因此操作失败,您正在查看该值作为无符号值。
read: 方法的 return 类型是什么?是 NSU 整数吗?它不是。它是 NSInteger。那么为什么它 return 是有符号整数而不是无符号整数呢?那在 read: 方法的文档中。阅读该文档,然后您应该知道不合理的大数字实际上是 您的 代码中使用 NSUInteger 而不是 NSInteger 创建的错误。
在代码行中
->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;
}
18446744073709551615
是 0xffffffffffff
,这是最大的无符号 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.
因此操作失败,您正在查看该值作为无符号值。
read: 方法的 return 类型是什么?是 NSU 整数吗?它不是。它是 NSInteger。那么为什么它 return 是有符号整数而不是无符号整数呢?那在 read: 方法的文档中。阅读该文档,然后您应该知道不合理的大数字实际上是 您的 代码中使用 NSUInteger 而不是 NSInteger 创建的错误。