HTTPDataResponse.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Log levels : off, error, warn, info, verbose
  7. // Other flags: trace
  8. static const int httpLogLevel = HTTP_LOG_LEVEL_OFF; // | HTTP_LOG_FLAG_TRACE;
  9. @implementation HTTPDataResponse
  10. - (id)initWithData:(NSData *)dataParam
  11. {
  12. if((self = [super init]))
  13. {
  14. HTTPLogTrace();
  15. offset = 0;
  16. data = dataParam;
  17. }
  18. return self;
  19. }
  20. - (void)dealloc
  21. {
  22. HTTPLogTrace();
  23. }
  24. - (UInt64)contentLength
  25. {
  26. UInt64 result = (UInt64)[data length];
  27. HTTPLogTrace2(@"%@[%p]: contentLength - %llu", THIS_FILE, self, result);
  28. return result;
  29. }
  30. - (UInt64)offset
  31. {
  32. HTTPLogTrace();
  33. return offset;
  34. }
  35. - (void)setOffset:(UInt64)offsetParam
  36. {
  37. HTTPLogTrace2(@"%@[%p]: setOffset:%lu", THIS_FILE, self, (unsigned long)offset);
  38. offset = (NSUInteger)offsetParam;
  39. }
  40. - (NSData *)readDataOfLength:(NSUInteger)lengthParameter
  41. {
  42. HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)lengthParameter);
  43. NSUInteger remaining = [data length] - offset;
  44. NSUInteger length = lengthParameter < remaining ? lengthParameter : remaining;
  45. void *bytes = (void *)([data bytes] + offset);
  46. offset += length;
  47. return [NSData dataWithBytesNoCopy:bytes length:length freeWhenDone:NO];
  48. }
  49. - (BOOL)isDone
  50. {
  51. BOOL result = (offset == [data length]);
  52. HTTPLogTrace2(@"%@[%p]: isDone - %@", THIS_FILE, self, (result ? @"YES" : @"NO"));
  53. return result;
  54. }
  55. @end