Larry
Larry

Reputation: 205

Can't read custom file from NSInputStream

I am trying to read a custom file from my documents directory via NSInputStream to upload it to a FTP server. I'm using the exact same code as demonstrated in the SimpleFTPSample provided by Apple. It seems to work fine as long as the file is empty, but as soon as it contains data it fails.

Here's my code. This is how I create the input stream, I tried it both ways:

//Approach one: Init with path
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

//Approach two: Init with data
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
self.fileStream = [NSInputStream inputStreamWithData:fileData];

If I init the stream with data, I get EXC_BAD_ACCESS (code=1, address=0x0) when invoking read on the stream (code snipped below), if I use the path it jumps right to File read error.

filePath is @"/var/mobile/Applications/94292A2A-37FC-4D8E-BDA6-A26963932AE6/Documents/1395576645.cpn", NSData returns properly and has 806 bytes.

That's part of the stream:handleEvent: delegate method:

if (self.bufferOffset == self.bufferLimit) {
         NSInteger   bytesRead;
         bytesRead = [self.fileStream read: self.buffer maxLength: kSendBufferSize];

         if (bytesRead == -1) {
            if (kPrintsToConsole) NSLog(@"File read error");
         } else if (bytesRead == 0) {
            [self stopSending];
         } else {
            self.bufferOffset = 0;
            self.bufferLimit  = bytesRead;
         }
}

I'm kinda stuck. Hope you guys can help me out. Running iOS 7.1 & Xcode 5.1

Upvotes: 1

Views: 838

Answers (1)

apiashko
apiashko

Reputation: 131

you need to call [self.fileStream open] before read. For both file and data approaches.

Upvotes: 2

Related Questions