KTVHCDataUnitItem.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // KTVHCDataUnitItem.m
  3. // KTVHTTPCache
  4. //
  5. // Created by Single on 2017/8/11.
  6. // Copyright © 2017年 Single. All rights reserved.
  7. //
  8. #import "KTVHCDataUnitItem.h"
  9. #import "KTVHCPathTools.h"
  10. #import "KTVHCLog.h"
  11. @interface KTVHCDataUnitItem ()
  12. @property (nonatomic, strong) NSRecursiveLock * coreLock;
  13. @end
  14. @implementation KTVHCDataUnitItem
  15. - (id)copyWithZone:(NSZone *)zone
  16. {
  17. [self lock];
  18. KTVHCDataUnitItem * obj = [[KTVHCDataUnitItem alloc] initForCopy];
  19. obj->_relativePath = self.relativePath;
  20. obj->_absolutePath = self.absolutePath;
  21. obj->_createTimeInterval = self.createTimeInterval;
  22. obj->_offset = self.offset;
  23. obj->_length = self.length;
  24. [self unlock];
  25. return obj;
  26. }
  27. - (instancetype)initForCopy
  28. {
  29. if (self = [super init])
  30. {
  31. }
  32. return self;
  33. }
  34. - (instancetype)initWithPath:(NSString *)path offset:(long long)offset
  35. {
  36. if (self = [super init])
  37. {
  38. KTVHCLogAlloc(self);
  39. _createTimeInterval = [NSDate date].timeIntervalSince1970;
  40. _relativePath = [KTVHCPathTools relativePathWithAbsoultePath:path];
  41. _offset = offset;
  42. [self prepare];
  43. }
  44. return self;
  45. }
  46. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  47. {
  48. if (self = [super init])
  49. {
  50. KTVHCLogAlloc(self);
  51. _createTimeInterval = [[aDecoder decodeObjectForKey:@"createTimeInterval"] doubleValue];
  52. _relativePath = [aDecoder decodeObjectForKey:@"relativePath"];
  53. _offset = [[aDecoder decodeObjectForKey:@"offset"] longLongValue];
  54. [self prepare];
  55. }
  56. return self;
  57. }
  58. - (void)encodeWithCoder:(NSCoder *)aCoder
  59. {
  60. [aCoder encodeObject:@(self.createTimeInterval) forKey:@"createTimeInterval"];
  61. [aCoder encodeObject:self.relativePath forKey:@"relativePath"];
  62. [aCoder encodeObject:@(self.offset) forKey:@"offset"];
  63. }
  64. - (void)dealloc
  65. {
  66. KTVHCLogDealloc(self);
  67. }
  68. - (void)prepare
  69. {
  70. _absolutePath = [KTVHCPathTools absoultePathWithRelativePath:self.relativePath];
  71. self.length = [KTVHCPathTools sizeOfItemAtPath:self.absolutePath];
  72. KTVHCLogDataUnitItem(@"%p, Create Unit Item\nabsolutePath : %@\nrelativePath : %@\nOffset : %lld\nLength : %lld", self, self.absolutePath, self.relativePath, self.offset, self.length);
  73. }
  74. - (void)setLength:(long long)length
  75. {
  76. [self lock];
  77. _length = length;
  78. KTVHCLogDataUnitItem(@"%p, Set length : %lld", self, length);
  79. [self unlock];
  80. }
  81. - (void)lock
  82. {
  83. if (!self.coreLock)
  84. {
  85. self.coreLock = [[NSRecursiveLock alloc] init];
  86. }
  87. [self.coreLock lock];
  88. }
  89. - (void)unlock
  90. {
  91. [self.coreLock unlock];
  92. }
  93. @end