Skip to content

Commit

Permalink
NSURLSession: use mutable data for transfer state body
Browse files Browse the repository at this point in the history
  • Loading branch information
triplef committed Jan 16, 2023
1 parent 311367a commit 89587b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 42 deletions.
8 changes: 3 additions & 5 deletions Source/GSTransferState.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@class GSURLSessionTaskBodySource;
@class NSArray;
@class NSData;
@class NSMutableData;
@class NSFileHandle;
@class NSHTTPURLResponse;
@class NSURL;
Expand Down Expand Up @@ -57,22 +58,19 @@ typedef NS_ENUM(NSUInteger, GSDataDrainType) {
@interface GSDataDrain: NSObject
{
GSDataDrainType _type;
NSData *_data;
NSMutableData *_data;
NSURL *_fileURL;
NSFileHandle *_fileHandle;
}

- (GSDataDrainType) type;
- (void) setType: (GSDataDrainType)type;

- (NSData*) data;
- (void) setData: (NSData*)data;
- (NSMutableData*) data;

- (NSURL*) fileURL;
- (void) setFileURL: (NSURL*)url;

- (NSFileHandle*) fileHandle;
- (void) setFileHandle: (NSFileHandle*)handle;

@end

Expand Down
45 changes: 8 additions & 37 deletions Source/GSTransferState.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ - (void) setType: (GSDataDrainType)type
_type = type;
}

- (NSData*) data
- (NSMutableData*) data
{
return _data;
}
if (!_data)
{
_data = [[NSMutableData alloc] init];
}

- (void) setData: (NSData*)data
{
ASSIGN(_data, data);
return _data;
}

- (NSURL*) fileURL
Expand All @@ -298,11 +298,6 @@ - (NSURL*) fileURL
return _fileURL;
}

- (void) setFileURL: (NSURL*)url
{
ASSIGN(_fileURL, url);
}

- (NSFileHandle*) fileHandle
{
/* Create temporary file and open a fileHandle for writing. */
Expand All @@ -319,11 +314,6 @@ - (NSFileHandle*) fileHandle
return _fileHandle;
}

- (void) setFileHandle: (NSFileHandle*)handle
{
ASSIGN(_fileHandle, handle);
}

@end

@implementation GSTransferState
Expand Down Expand Up @@ -389,27 +379,8 @@ - (instancetype) byAppendingBodyData: (NSData*)bodyData
switch ([_bodyDataDrain type])
{
case GSDataDrainInMemory:
{
NSMutableData *data;
GSDataDrain *dataDrain;
GSTransferState *ts;

data = [_bodyDataDrain data] ?
AUTORELEASE([[_bodyDataDrain data] mutableCopy])
: [NSMutableData data];

[data appendData: bodyData];
dataDrain = AUTORELEASE([[GSDataDrain alloc] init]);
[dataDrain setType: GSDataDrainInMemory];
[dataDrain setData: data];

ts = [[GSTransferState alloc] initWithURL: _url
parsedResponseHeader: _parsedResponseHeader
response: _response
bodySource: _requestBodySource
bodyDataDrain: dataDrain];
return AUTORELEASE(ts);
}
[[_bodyDataDrain data] appendData: bodyData];
return self;
case GSDataDrainTypeToFile:
{
NSFileHandle *fileHandle;
Expand Down

0 comments on commit 89587b6

Please sign in to comment.