mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 16:30:41 +00:00
Cope with requests to read huge length.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3848 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
3e0c0b6c43
commit
b9b162c14b
1 changed files with 39 additions and 11 deletions
|
@ -704,7 +704,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSData*)readDataToEndOfFile
|
- (NSData*) readDataToEndOfFile
|
||||||
{
|
{
|
||||||
char buf[NETBUF_SIZE];
|
char buf[NETBUF_SIZE];
|
||||||
NSMutableData* d;
|
NSMutableData* d;
|
||||||
|
@ -727,24 +727,52 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSData*)readDataOfLength: (unsigned int)len
|
- (NSData*) readDataOfLength: (unsigned)len
|
||||||
{
|
{
|
||||||
NSMutableData* d;
|
NSMutableData* d;
|
||||||
int pos;
|
int got;
|
||||||
char *buf;
|
|
||||||
|
|
||||||
[self checkRead];
|
[self checkRead];
|
||||||
if (isNonBlocking == YES)
|
if (isNonBlocking == YES)
|
||||||
[self setNonBlocking: NO];
|
[self setNonBlocking: NO];
|
||||||
buf = objc_malloc(len);
|
if (len <= 65536)
|
||||||
d = [NSMutableData dataWithBytesNoCopy: buf length: len];
|
|
||||||
if ((pos = read(descriptor, [d mutableBytes], len)) < 0)
|
|
||||||
{
|
{
|
||||||
[NSException raise: NSFileHandleOperationException
|
char *buf;
|
||||||
format: @"unable to read from descriptor - %s",
|
|
||||||
strerror(errno)];
|
buf = NSZoneMalloc(NSDefaultMallocZone(), len);
|
||||||
|
d = [NSMutableData dataWithBytesNoCopy: buf length: len];
|
||||||
|
if ((got = read(descriptor, [d mutableBytes], len)) < 0)
|
||||||
|
{
|
||||||
|
[NSException raise: NSFileHandleOperationException
|
||||||
|
format: @"unable to read from descriptor - %s",
|
||||||
|
strerror(errno)];
|
||||||
|
}
|
||||||
|
[d setLength: got];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char buf[NETBUF_SIZE];
|
||||||
|
|
||||||
|
d = [NSMutableData dataWithCapacity: 0];
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int chunk = len > sizeof(buf) ? sizeof(buf) : len;
|
||||||
|
|
||||||
|
got = read(descriptor, buf, chunk);
|
||||||
|
if (got > 0)
|
||||||
|
{
|
||||||
|
[d appendBytes: buf length: got];
|
||||||
|
len -= got;
|
||||||
|
}
|
||||||
|
else if (got < 0)
|
||||||
|
{
|
||||||
|
[NSException raise: NSFileHandleOperationException
|
||||||
|
format: @"unable to read from descriptor - %s",
|
||||||
|
strerror(errno)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (len > 0 && got > 0);
|
||||||
}
|
}
|
||||||
[d setLength: pos];
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue