Re: Cocoa-dev Digest, Vol 6, Issue 1522
Re: Cocoa-dev Digest, Vol 6, Issue 1522
- Subject: Re: Cocoa-dev Digest, Vol 6, Issue 1522
- From: Greg Guerin <email@hidden>
- Date: Thu, 22 Oct 2009 10:28:17 -0700
Ashley Perrien wrote:
uint8_t *readBuffer;
NSUInteger bufferLength;
BOOL gotBuffer = [readStream getBuffer: &readBuffer length:
&bufferLength];
if (gotBuffer) {
int len = [readStream read: readBuffer maxLength: bufferLength];
if (len <= 0)
NSLog(@"nothing read");
else
NSLog(@"Read: %@", [[[NSString alloc] initWithBytes: readBuffer
length: bufferLength encoding:NSUTF8StringEncoding] autorelease]);
}
else {
NSLog(@"Did not get a buffer");
}
}
// Output: No Bytes , Did not get a buffer
This code is mis-designed.
First, if gotBuffer is YES, then the available data is already
present in the returned-by-reference readBuffer. Calling
read:maxLength: will return the NEXT series of bytes. It will block
in doing this, unlike the non-blocking getBuffer:length: call.
Second, if you're expecting a buffer to be available immediately,
your expectation is misplaced. It takes time for the first packet to
arrive and be processed. If you call read:maxLength:, however, then
that is a blocking method, and it will suspend the calling thread
until sufficient data has arrived.
I agree with Jens Alfke: you should read the stream programming
guide, and look at a working example.
I'm not entirely sure why you mention the null character.
I misread what you were doing with the data. I thought it was being
treated as a C string in UTF8 encoding.
-- GG
_______________________________________________
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