Skip to content

Commit

Permalink
Added factory methods for file-based stream, and cleaned up some warn…
Browse files Browse the repository at this point in the history
…ings.
  • Loading branch information
AlanQuatermain committed Apr 21, 2009
1 parent 7c82f3a commit 46fafb3
Showing 1 changed file with 66 additions and 30 deletions.
96 changes: 66 additions & 30 deletions Compression/AQGzipFileStream.m
Expand Up @@ -43,6 +43,65 @@
#import <mach/mach_port.h>
#import <mach/mach_init.h>

@interface AQGzipFileInputStream : NSInputStream
{
NSString * _path;
gzFile _file;
CFRunLoopSourceRef _rls;
mach_port_t _port;
id __weak _delegate;
NSError * _error;
NSStreamStatus _status;
}
@end

@interface AQGzipFileOutputStream : NSOutputStream <AQGzipOutputCompressor>
{
NSString * _path;
gzFile _file;
CFRunLoopSourceRef _rls;
mach_port_t _port;
id __weak _delegate;
NSError * _error;
NSStreamStatus _status;
AQGzipCompressionLevel _level;
}
@end

@implementation AQGzipInputStream (GzipFileInput)

+ (id) gzipStreamWithFileAtPath: (NSString *) path
{
return ( [[[AQGzipFileInputStream alloc] initWithPath: path] autorelease] );
}

- (id) initWithGzipFileAtPath: (NSString *) path
{
id result = [[AQGzipFileInputStream alloc] initWithPath: path];
[self release];
return ( result );
}

@end

@implementation AQGzipOutputStream (GzipFileOutput)

+ (id<AQGzipOutputCompressor>) gzipStreamToFileAtPath: (NSString *) path
{
return ( [[[AQGzipFileOutputStream alloc] initWithPath: path] autorelease] );
}

- (id<AQGzipOutputCompressor>) initToGzipFileAtPath: (NSString *) path
{
id result = [[AQGzipFileOutputStream alloc] initWithPath: path];
[self release];
return ( result );
}

@end

#pragma mark -

NSError * CreateGZFileError( gzFile file )
{
int err = 0;
Expand Down Expand Up @@ -96,7 +155,7 @@ static CFRunLoopSourceRef RunloopSourceForStream( NSStream * stream, mach_port_t
// allocate with receive right
kern_return_t kr = mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_RECEIVE, port );
if ( kr != KERN_SUCCESS )
return;
return ( NULL );

// insert a make-send right so others can post to us
kr = mach_port_insert_right( mach_task_self(), *port, *port, MACH_MSG_TYPE_MAKE_SEND );
Expand All @@ -123,18 +182,6 @@ static CFRunLoopSourceRef RunloopSourceForStream( NSStream * stream, mach_port_t

#pragma mark -

@interface AQGzipFileInputStream : NSInputStream
{
NSString * _path;
gzFile _file;
CFRunLoopSourceRef _rls;
mach_port_t _port;
id __weak _delegate;
NSError * _error;
NSStreamStatus _status;
}
@end

@implementation AQGzipFileInputStream

- (id) initWithPath: (NSString *) path
Expand Down Expand Up @@ -202,7 +249,7 @@ - (NSStreamStatus) streamStatus
- (void) scheduleInRunLoop: (NSRunLoop *) aRunLoop forMode: (NSString *) mode
{
if ( _rls == NULL )
_rls = (CFRunLoopRef) CFMakeCollectable( RunloopSourceForStream(self, &_port) );
_rls = (CFRunLoopSourceRef) CFMakeCollectable( RunloopSourceForStream(self, &_port) );
CFRunLoopAddSource( [aRunLoop getCFRunLoop], _rls, (CFStringRef)mode );
}

Expand Down Expand Up @@ -267,7 +314,7 @@ - (void) close
_status = NSStreamStatusClosed;
}

- (void) _runloopPort
- (mach_port_t) _runloopPort
{
return ( _port );
}
Expand Down Expand Up @@ -316,21 +363,10 @@ - (BOOL) hasBytesAvailable

#pragma mark -

@interface AQGzipFileOutputStream : NSOutputStream <AQGzipOutputCompressor>
{
NSString * _path;
gzFile _file;
CFRunLoopSourceRef _rls;
mach_port_t _port;
id __weak _delegate;
NSError * _error;
NSStreamStatus _status;
AQGzipCompressionLevel _level;
}
@end

@implementation AQGzipFileOutputStream

@synthesize compressionLevel=_level;

- (id) initWithPath: (NSString *) path
{
if ( [super init] == nil )
Expand Down Expand Up @@ -397,7 +433,7 @@ - (NSStreamStatus) streamStatus
- (void) scheduleInRunLoop: (NSRunLoop *) aRunLoop forMode: (NSString *) mode
{
if ( _rls == NULL )
_rls = (CFRunLoopRef) CFMakeCollectable( RunloopSourceForStream(self, &_port) );
_rls = (CFRunLoopSourceRef) CFMakeCollectable( RunloopSourceForStream(self, &_port) );
CFRunLoopAddSource( [aRunLoop getCFRunLoop], _rls, (CFStringRef)mode );
}

Expand Down Expand Up @@ -466,7 +502,7 @@ - (void) close
_status = NSStreamStatusClosed;
}

- (void) _runloopPort
- (mach_port_t) _runloopPort
{
return ( _port );
}
Expand Down

0 comments on commit 46fafb3

Please sign in to comment.