Re: NSInputStream getBuffer:length:
Re: NSInputStream getBuffer:length:
- Subject: Re: NSInputStream getBuffer:length:
- From: p3consulting <email@hidden>
- Date: Sat, 6 Mar 2004 11:20:25 +0100
On 6 mars 2004, at 06:59, Joannou Ng wrote:
>
Hi all,
>
>
Anyone got NSInputStream's getBuffer:length: to work with sockets?
>
Following is a method defined in my category of NSInputStream:
>
>
- (NSString *)readString
>
{
>
uint8_t *buffer;
>
unsigned int bufferLength;
>
>
if ([self getBuffer:&buffer length:&bufferLength]) {
>
return [[[NSString alloc] initWithBytes:buffer
>
length:bufferLength encoding:NSUTF8StringEncoding] autorelease];
>
} else {
>
return [NSString string];
>
}
>
}
>
>
getBuffer:length: never returns YES. Should I just go back to using
>
read:maxLength: ?
>
getBuffer returns a pointer on the buffer used by the NSInputStream
object, it doesn't actually read bytes from the socket
from the doc:
Returns a pointer to the read buffer in buffer and, by reference, the
number of bytes available in len. The buffer is only valid until the
next stream operation is performed. This method will return NO if the
buffer is not available.
Subclasses of NSInputStream may return NO if this operation is not
appropriate for the stream type.
so yes you should call read:maxLength.
Note they are some wrapper Cocoa classes are available on the net to
handle read/write on sockets (OmniGroup and others)
These classes may better solve your problem than NSInputStream by
providing an higher abstraction and multi-threading to avoid your main
thread to block when reading data from the socket
Pascal Pochet
email@hidden
_______________________________________________
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.