[英][(NSInputStream *)stream read:buf maxLength:1024]; returns very huge value
In code line 在代码行中
->NSInteger len = [(NSInputStream *)stream read:buf maxLength:1024];
I am getting very huge value of len from this method like:(18446744073709551615) 我从这样的方法中得到len的巨大价值:(18446744073709551615)
and crashes 并崩溃
Terminating app due to uncaught exception 'NSMallocException', reason: -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615) 由于未捕获的异常“ NSMallocException”而终止应用程序,原因:-[NSConcreteMutableData appendBytes: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
is 0xffffffffffff
which is the maximum unsigned 64-bit integer value but it's also the equivalent of -1
as a 64-bit signed integer. 18446744073709551615
为0xffffffffffff
,这是最大的无符号64位整数值,但也等于-1
作为64位有符号整数。
If you look at the reference for [NSInputStream read:maxLength:]
it says: 如果查看[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; 0表示到达缓冲区末尾;
A negative number means that the operation failed. 负数表示操作失败。
So the operation failed and you are viewing the value as an unsigned value. 因此操作失败,您正在将值视为无符号值。
What is the return type of the read: method? read:方法的返回类型是什么? Is it NSUInteger? 是NSUInteger吗? It isn't. 不是。 It is NSInteger. 它是NSInteger。 So why does it return a signed integer and not an unsigned integer? 那么,为什么它返回有符号整数而不是无符号整数? That's in the documentation of the read: method. 那是在read:方法的文档中。 Read that documentation, then you should know that the unreasonably large number is actually a bug in your code created by using NSUInteger instead of NSInteger. 阅读文档,那么你应该知道的是,不合理的大数量实际上是使用,而不是NSInteger的NSUInteger建立在你的代码中的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.