Re: Reading from an NSInputStream
Re: Reading from an NSInputStream
- Subject: Re: Reading from an NSInputStream
- From: Evan DiBiase <email@hidden>
- Date: Sun, 23 Nov 2003 00:56:29 -0500
On Nov 22, 2003, at 12:17 PM, Evan DiBiase wrote:
On Nov 22, 2003, at 12:03 PM, Troy Dawson wrote:
// reads up to length bytes into the supplied buffer, which must be
at least of size len. Returns the actual number of bytes read.
I could use -read, but what I really want is just to get as many bytes
as are available, which is why -getBuffer seemed appropriate.
Although, strangely enough, I re-wrote the code this evening (morning
now, I guess) and found that using -getBuffer worked, but returned NO,
so I re-wrote the method:
- (void)readBytes:(NSInputStream *)stream
{
while ([stream hasBytesAvailable]) {
int maxLength = 1024;
uint8_t buf[maxLength];
int length = [stream read:buf maxLength:maxLength];
for (int i = 0; i < length; i++) {
printf("%c", buf[i]);
}
}
}
Which seems to work. The 1024 maximum length is pretty arbitrary,
though; is there a good way to pick numbers for applications like this?
I suppose I could just go with typical IRC use cases, but that seems
awfully subjective :-)
And, of course, to attempt satisfy my own curiosity, I must ask: why
doesn't NSInputStream support -getBuffer in this situation? Getting a
buffer and its exact length seems a lot easier than allocating a buffer
of a certain length and looping.
Evan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.