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 (Cocoa-dev@lists.apple.com)
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to arch...@mail-archive.com