diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-07-07 02:32:01 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-07-07 02:32:01 +0200 |
commit | 6d67743bb97b9053f5979e122b3bfb2a7f957e6d (patch) | |
tree | 1db40aa6ba7348b48d5a42f7797559d3ca010898 | |
parent | d8fa1c646c7e6a8fe5d6235b3e635b30f2bfa745 (diff) |
Make MLKStream#-readChar more robust.
-rw-r--r-- | MLKStream.m | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/MLKStream.m b/MLKStream.m index cb8869c..d77ff9c 100644 --- a/MLKStream.m +++ b/MLKStream.m @@ -81,26 +81,33 @@ NSString *tmpstr; ssize_t bytes_read; - if (![_input hasBytesAvailable]) + //NSLog (@"%@", _input); + + buffer = realloc (buffer, i+1); + bytes_read = [_input read:(buffer+i) maxLength:1]; + NSLog (@"%d bytes read", bytes_read); + + if (bytes_read < 1) { + free (buffer); [NSException raise:@"MLKStreamError" format:@"Tried to read beyond end of file."]; } - buffer = realloc (buffer, i+1); - bytes_read = [_input read:(buffer+i) maxLength:1]; - // NSLog (@"%d bytes read", bytes_read); - - tmpstr = [[NSString alloc] initWithBytesNoCopy:buffer + tmpstr = [[NSString alloc] initWithBytes:buffer length:(i+1) - encoding:_encoding - freeWhenDone:NO]; + encoding:_encoding]; if ([tmpstr length] == 1) { retval = [tmpstr characterAtIndex:0]; [tmpstr release]; + //free (buffer); return retval; } + else + { + [tmpstr release]; + } } return -1; |