mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-29 16:01:38 +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;
|
||||
}
|
||||
|
||||
- (NSData*)readDataToEndOfFile
|
||||
- (NSData*) readDataToEndOfFile
|
||||
{
|
||||
char buf[NETBUF_SIZE];
|
||||
NSMutableData* d;
|
||||
|
@ -727,24 +727,52 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
return d;
|
||||
}
|
||||
|
||||
- (NSData*)readDataOfLength: (unsigned int)len
|
||||
- (NSData*) readDataOfLength: (unsigned)len
|
||||
{
|
||||
NSMutableData* d;
|
||||
int pos;
|
||||
char *buf;
|
||||
int got;
|
||||
|
||||
[self checkRead];
|
||||
if (isNonBlocking == YES)
|
||||
[self setNonBlocking: NO];
|
||||
buf = objc_malloc(len);
|
||||
d = [NSMutableData dataWithBytesNoCopy: buf length: len];
|
||||
if ((pos = read(descriptor, [d mutableBytes], len)) < 0)
|
||||
if (len <= 65536)
|
||||
{
|
||||
[NSException raise: NSFileHandleOperationException
|
||||
format: @"unable to read from descriptor - %s",
|
||||
strerror(errno)];
|
||||
char *buf;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue