From 629a99a805399db4be550029666d9edc75488937 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Mon, 7 Jul 2008 09:53:02 +0200 Subject: Properly open and close streams if necessary. --- MLKStream.h | 2 ++ MLKStream.m | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/MLKStream.h b/MLKStream.h index cc67400..733b5ee 100644 --- a/MLKStream.h +++ b/MLKStream.h @@ -28,6 +28,8 @@ NSOutputStream *_output; NSStringEncoding _encoding; BOOL _charCached; + BOOL _closeInputWhenDone; + BOOL _closeOutputWhenDone; unichar _cachedChar; } diff --git a/MLKStream.m b/MLKStream.m index d77ff9c..f1c2b32 100644 --- a/MLKStream.m +++ b/MLKStream.m @@ -57,6 +57,8 @@ _encoding = encoding; _cachedChar = 0; _charCached = NO; + _closeInputWhenDone = NO; + _closeOutputWhenDone = NO; return self; } @@ -75,6 +77,12 @@ return ch; } + if ([_input streamStatus] == NSStreamStatusNotOpen) + { + _closeInputWhenDone = YES; + [_input open]; + } + buffer = NULL; for (i = 0;; i++) { @@ -85,7 +93,7 @@ buffer = realloc (buffer, i+1); bytes_read = [_input read:(buffer+i) maxLength:1]; - NSLog (@"%d bytes read", bytes_read); + //NSLog (@"%d bytes read", bytes_read); if (bytes_read < 1) { @@ -149,4 +157,19 @@ return eofp; } + +-(void) dealloc +{ + if (_closeInputWhenDone) + { + [_input close]; + } + RELEASE (_input); + if (_closeOutputWhenDone) + { + [_output close]; + } + RELEASE (_output); + [super dealloc]; +} @end -- cgit v1.2.3