When the triangled shape doesn’t fit in the round hole


I’m writing some iPhony stuff while thinking in Java. I wanna stream an HTTP request over the wire. In Java I’d inject an OutputStream into my class or method and just start writing to it byte by byte. Objective C Cocoa wants to set up RunLoops, setup delegates, and rely on callbacks that pass buffers into the NSOutputStream API. I set on fire and jumped through all of these hoops (after dousing my briefs in Kerosene) but I’m still not getting any data on my stream! Is it because I’m trying to force things to work like Java? Help?

Here’s my test method and impl logic:


-(void) testStreamWriting {
	NSOutputStream *stream = [[NSOutputStream alloc] initToMemory];
	[stream setDelegate:myStreamDelegate];
	[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
	[stream open];
	NSData *_data = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
	STAssertNotNil(_data, @"Stream delegate should have written data");
	[stream close];
	[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
	[stream release];
	[data release];
}

@implementation MyStreamDelegate
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
	switch (streamEvent) {
		case NSStreamEventHasSpaceAvailable:
		{
			const uint8_t stuff[2] = {1,2};
			[theStream write:stuff maxLength:2];
			break;
		}
		default:
			break;
	}
}

@end