Re: unrecognized selector sent to instance
Re: unrecognized selector sent to instance
- Subject: Re: unrecognized selector sent to instance
- From: Jason Stephenson <email@hidden>
- Date: Wed, 15 Apr 2009 11:34:48 -0400
Thanks to Bill and BJ.
Don't I feel silly. I have a new implementation of the output stream
class that works. (See attachement.)
Now, I have five other NSInputStream/NSOutputStream subclasses to modify
and test.
Y'know, when I first started coding these classes the other day, I heard
a little voice in the back of my mind telling me to include an ivar of
the "parent" class and implement these classes that way, 'cause I knew
it was a class cluster implemented on top of CFStream, but I didn't
listen to that little voice. Guess, I'll need to pay more attention in
the future. ;)
BTW, the Bzip2 compression works properly on the output stream, too.
Guess I got lucky on that one. :)
Cheers,
Jason
// -*- Mode: ObjC; -*-
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2009 by Jason J. A. Stephenson
*
* SIGIOBzip2OutputStream.h - A NSOutputStream subclass that Bzip2
* compresses as it writes output.
*
* SIGIOBzip2OutputStream.h is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License version 3 only, as published by the Free Software
* Foundation.
*
* SIGIOBzip2OutputStream.h is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License version 3 for more details (a copy is
* avaliable at <http://www.gnu.org/copyleft/lgpl.html>).
*
************************************************************************/
#import <Foundation/Foundation.h>
#import <bzlib.h>
@interface SIGIOBzip2OutputStream : NSOutputStream
{
bz_stream *m_strm;
uint8_t *m_buffer;
NSInteger m_bzip2Error;
NSOutputStream *out;
}
-(id) initToMemory;
-(id) initToBuffer: (uint8_t *) buffer capacity: (NSUInteger) capacity;
//-(id) initToFileAtPath: (NSString *) path append: (BOOL) shouldAppend;
-(void) open;
-(void) close;
-(BOOL) hasSpaceAvailable;
-(NSInteger) write: (const uint8_t *) buffer maxLength: (NSUInteger) length;
-(NSStreamStatus) streamStatus;
-(NSError *) streamError;
@end
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2009 by Jason J. A. Stephenson
*
* SIGIOBzip2OutputStream.m - A NSOutputStream subclass that Bzip2
* compresses its output.
*
* SIGIOBzip2OutputStream.m is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License version 3 only, as published by the Free Software
* Foundation.
*
* SIGIOBzip2OutputStream.m is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License version 3 for more details (a copy is
* avaliable at <http://www.gnu.org/copyleft/lgpl.html>).
*
************************************************************************/
#import "SIGIOBzip2OutputStream.h"
#import "SIGIOArchiveKitErrors.h"
#define SIGIO_ALLOC_CHUNK 4096
@implementation SIGIOBzip2OutputStream
-(id) initToMemory
{
self = [super init];
if (self) {
m_strm = malloc(sizeof(bz_stream));
if (m_strm != NULL) {
m_strm->next_in = NULL;
m_strm->avail_in = 0;
m_strm->next_out = NULL;
m_strm->avail_out = 0;
m_strm->bzalloc = NULL;
m_strm->bzfree = NULL;
m_strm->opaque = NULL;
}
else {
[self release];
return nil;
}
m_buffer = malloc(SIGIO_ALLOC_CHUNK);
if (m_buffer == NULL) {
[self release];
return nil;
}
m_bzip2Error = BZ_OK;
out = [[NSOutputStream alloc] initToMemory];
if (out == nil) {
[self release];
return nil;
}
}
return self;
}
-(id) initToBuffer: (uint8_t *) buffer capacity: (NSUInteger) capacity
{
self = [super init];
if (self) {
m_strm = malloc(sizeof(bz_stream));
if (m_strm != NULL) {
m_strm->next_in = NULL;
m_strm->avail_in = 0;
m_strm->next_out = NULL;
m_strm->avail_out = 0;
m_strm->bzalloc = NULL;
m_strm->bzfree = NULL;
m_strm->opaque = NULL;
}
else {
[self release];
return nil;
}
m_buffer = malloc(SIGIO_ALLOC_CHUNK);
if (m_buffer == NULL) {
[self release];
return nil;
}
m_bzip2Error = BZ_OK;
out = [[NSOutputStream alloc] initToBuffer: buffer capacity: capacity];
if (out == nil) {
[self release];
return nil;
}
}
return self;
}
-(id) initToFileAtPath: (NSString *) path append: (BOOL) shouldAppend
{
self = [super init];
if (self) {
m_strm = malloc(sizeof(bz_stream));
if (m_strm != NULL) {
m_strm->next_in = NULL;
m_strm->avail_in = 0;
m_strm->next_out = NULL;
m_strm->avail_out = 0;
m_strm->bzalloc = NULL;
m_strm->bzfree = NULL;
m_strm->opaque = NULL;
}
else {
[self release];
return nil;
}
m_buffer = malloc(SIGIO_ALLOC_CHUNK);
if (m_buffer == NULL) {
[self release];
return nil;
}
m_bzip2Error = BZ_OK;
out = [[NSOutputStream alloc] initToFileAtPath: path append: shouldAppend];
if (out == nil) {
[self release];
return nil;
}
}
return self;
}
-(void) open
{
// Initialize the compression stream
int bzReturn;
m_strm->next_out = (char *)m_buffer;
m_strm->avail_out = SIGIO_ALLOC_CHUNK;
bzReturn = BZ2_bzCompressInit(m_strm, 5, 0, 0);
if (bzReturn != BZ_OK) {
m_bzip2Error = bzReturn;
return;
}
[out open];
}
-(void) close
{
/* Flush remaining compressed output. */
int bzReturn;
NSInteger result;
do {
bzReturn = BZ2_bzCompress(m_strm, BZ_FINISH);
if (m_strm->avail_out == 0 || bzReturn == BZ_STREAM_END) {
result = [out write: m_buffer maxLength: SIGIO_ALLOC_CHUNK - m_strm->avail_out];
m_strm->next_out = (char *)m_buffer;
m_strm->avail_out = SIGIO_ALLOC_CHUNK;
}
} while (bzReturn == BZ_FINISH_OK && result != -1);
// Done with compression.
(void) BZ2_bzCompressEnd(m_strm);
// Close the parent stream.
[out close];
}
-(BOOL) hasSpaceAvailable
{
if (m_strm->avail_out)
return YES;
else
return [out hasSpaceAvailable];
}
-(NSInteger) write: (const uint8_t *) buffer maxLength: (NSUInteger) length
{
// Return value
NSInteger result, outResult;
// Return value from bzip2 functions.
int bzReturn;
// We need to reset these on every call, anyway.
m_strm->next_out = (char *)m_buffer;
m_strm->avail_out = SIGIO_ALLOC_CHUNK;
m_strm->next_in = (char *)buffer;
m_strm->avail_in = length;
result = 0;
do {
bzReturn = BZ2_bzCompress(m_strm, BZ_RUN);
if (bzReturn == BZ_RUN_OK) {
if (m_strm->avail_out == 0) {
outResult = [out write: m_buffer maxLength: SIGIO_ALLOC_CHUNK - m_strm->avail_out];
if (outResult > 0) {
if (m_strm->avail_out == 0) {
m_strm->avail_out = SIGIO_ALLOC_CHUNK;
m_strm->next_out = (char *)m_buffer;
}
}
else {
result = outResult;
break;
}
}
result = length - m_strm->avail_in;
}
else
m_bzip2Error = bzReturn;
} while (bzReturn == BZ_RUN_OK && m_strm->avail_in > 0);
return result;
}
-(NSStreamStatus) streamStatus
{
if (m_bzip2Error != BZ_OK)
return NSStreamStatusError;
else
return [out streamStatus];
}
-(NSError *) streamError
{
NSError *error;
if (m_bzip2Error != BZ_OK) {
error = newBzip2Error(m_bzip2Error);
[error release];
}
else
error = [out streamError];
return error;
}
-(void) dealloc
{
if (m_strm != NULL)
free(m_strm);
if (m_buffer != NULL)
free(m_buffer);
if (out != nil)
[out release];
[super dealloc];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden