NSOutputStream copy
NSOutputStream copy
- Subject: NSOutputStream copy
- From: Aalok <email@hidden>
- Date: Mon, 10 Jul 2006 16:12:18 +0530
Hi,
I want to copy file using NSOutputStream to a window machine. I am
using the following code to do that. but -
(void)stream:(NSOutputStream *)stream
handleEvent:(NSStreamEvent)eventCode is never get called on [oStream
open]. I am running the whole part in a thread. Basically I want to
kill the thread if user presses cancel and to terminate transfer
immediately.
Am I missing something here?
One more question. does endiness has to do here? I am copying from
mac(ppc as well as intel) to window(intel)
- (void)createOutputStream:(NSString *)destinationPath
{
NSLog(@"Creating and opening NSOutputStream...");
// oStream is an instance variable
//oStream = [[NSOutputStream alloc]
initToFileAtPath:destinationPath append:YES];
oStream = [[NSOutputStream alloc] initToFileAtPath:destinationPath append:YES];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream open];
}
- (void)stream:(NSOutputStream *)stream handleEvent:(NSStreamEvent)eventCode
{
switch(eventCode)
{
case NSStreamEventHasSpaceAvailable:
{
NSLog(@"***********inside stream NSStreamEventHasSpaceAvailable***********");
uint8_t *readBytes = (uint8_t *)[contentOfFileToTrasfer
mutableBytes];//contentOfFileToTrasfer is a NSMutableData object
readBytes += byteIndex; // instance variable to move pointer
int data_len = [contentOfFileToTrasfer length];
unsigned int len = ((data_len - byteIndex >= 1024) ?
1024 : (data_len-byteIndex));
uint8_t buf[len];
(void)memcpy(buf, readBytes, len);
len = [stream write:(const uint8_t *)buf maxLength:len];
byteIndex += len;
break;
}
case NSStreamEventEndEncountered:
{
/*NSData *newData = [oStream propertyForKey:
NSStreamDataWrittenToMemoryStreamKey];
if (!newData) {
NSLog(@"No data written to memory!");
} else {
//[self processData:newData];
}*/
NSLog(@"***********inside stream NSStreamEventEndEncountered***********");
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[stream release];
oStream = nil; // oStream is instance variable
break;
}
case NSStreamEventErrorOccurred:
{
/*NSError *theError = [stream streamError];
NSAlert *theAlert = [[NSAlert alloc] init]; // modal
delegate releases
[theAlert setMessageText:@"Error reading stream!"];
[theAlert setInformativeText:[NSString
stringWithFormat:@"Error %i: %@",
[theError code], [theError localizedDescription]]];
[theAlert addButtonWithTitle:@"OK"];
[theAlert beginSheetModalForWindow:[NSApp mainWindow]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];*/
NSLog(@"***********inside stream NSStreamEventErrorOccurred***********");
[stream close];
[stream release];
break;
}
// continued ...
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden