简体   繁体   English

[(NSInputStream *)stream read:buf maxLength:1024]; 回报非常巨大的价值

[英][(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. 184467440737095516150xffffffffffff ,这是最大的无符号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.

相关问题 NSInputStream读取:maxlength:返回的字节数比maxlength多 - NSInputStream read: maxlength: returning way more bytes than maxlength 当可用字节时,NSInputStream读取返回无符号整数最大值 - NSInputStream read returns unsigned integer maximum value when bytes available 当-[NSInputStream read:maxLength:]返回0时,它在Apple Document中是否模棱两可? - Is it ambiguous in Apple Document when -[NSInputStream read:maxLength:] return 0? - [NSInputStream read:maxLength:]抛出一个异常,说长度太大,但事实并非如此 - -[NSInputStream read:maxLength:] throws an exception saying length is too big, but it isn't 如何将NSInputStream转换为NSString或如何读取NSInputStream - How to convert NSInputStream to NSString or how to read NSInputStream 如何使用UTF-8读取NSInputStream? - How to read a NSInputStream with UTF-8? 如果[nsInputStream close]被另一个线程调用,是否应该返回[nsInputStream read:…]? - Should [nsInputStream read:…] return if [nsInputStream close] is called by another thread? NSInputStream不调用Delegate(stream:handleEvent :) - NSInputStream does not call Delegate (stream:handleEvent:) iOS读取流,但返回null - iOS Read a Stream but returns null 使用NSOutputstream和NSInputstream写入和读取每个间隔 - Use NSOutputstream and NSInputstream to write and read every interval
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM