HTTPDataResponse.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #import "HTTPDataResponse.h"
  2. #import "HTTPLogging.h"
  3. #if ! __has_feature(objc_arc)
  4. #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  5. #endif
  6. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  7. #pragma clang diagnostic ignored "-Wcast-qual"
  8. #pragma clang diagnostic ignored "-Wunused-variable"
  9. // Log levels : off, error, warn, info, verbose
  10. // Other flags: trace
  11. static const int httpLogLevel = HTTP_LOG_LEVEL_OFF; // | HTTP_LOG_FLAG_TRACE;
  12. @implementation HTTPDataResponse
  13. - (id)initWithData:(NSData *)dataParam
  14. {
  15. if((self = [super init]))
  16. {
  17. HTTPLogTrace();
  18. offset = 0;
  19. data = dataParam;
  20. }
  21. return self;
  22. }
  23. - (void)dealloc
  24. {
  25. HTTPLogTrace();
  26. }
  27. - (UInt64)contentLength
  28. {
  29. UInt64 result = (UInt64)[data length];
  30. HTTPLogTrace2(@"%@[%p]: contentLength - %llu", THIS_FILE, self, result);
  31. return result;
  32. }
  33. - (UInt64)offset
  34. {
  35. HTTPLogTrace();
  36. return offset;
  37. }
  38. - (void)setOffset:(UInt64)offsetParam
  39. {
  40. HTTPLogTrace2(@"%@[%p]: setOffset:%lu", THIS_FILE, self, (unsigned long)offset);
  41. offset = (NSUInteger)offsetParam;
  42. }
  43. - (NSData *)readDataOfLength:(NSUInteger)lengthParameter
  44. {
  45. HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)lengthParameter);
  46. NSUInteger remaining = [data length] - offset;
  47. NSUInteger length = lengthParameter < remaining ? lengthParameter : remaining;
  48. void *bytes = (void *)(((char*)[data bytes]) + offset);
  49. offset += length;
  50. return [NSData dataWithBytesNoCopy:bytes length:length freeWhenDone:NO];
  51. }
  52. - (BOOL)isDone
  53. {
  54. BOOL result = (offset == [data length]);
  55. HTTPLogTrace2(@"%@[%p]: isDone - %@", THIS_FILE, self, (result ? @"YES" : @"NO"));
  56. return result;
  57. }
  58. @end