杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
//  OSSInputStreamHelper.m
//  AliyunOSSSDK
//
//  Created by 怀叙 on 2017/12/7.
//  Copyright © 2017年 阿里云. All rights reserved.
//
 
#import "OSSInputStreamHelper.h"
#import "OSSLog.h"
#import "aos_crc64.h"
 
@interface OSSInputStreamHelper ()
{
    NSInputStream *_inputStream;
    CFAbsoluteTime _startTime;
    dispatch_semaphore_t _semaphore;
}
 
@end
 
@implementation OSSInputStreamHelper
 
- (instancetype)initWithFileAtPath:(nonnull NSString *)path
{
    self = [super init];
    if (self) {
        _crc64 = 0;
        _inputStream = [NSInputStream inputStreamWithFileAtPath:path];
        _semaphore = dispatch_semaphore_create(1);
    }
    return self;
}
 
- (instancetype)initWithURL:(nonnull NSURL *)URL
{
    self = [super init];
    if (self) {
        _crc64 = 0;
        _inputStream = [NSInputStream inputStreamWithURL:URL];
        _semaphore = dispatch_semaphore_create(1);
    }
    return self;
}
 
- (void)syncReadBuffers
{
    dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
 
    _startTime = CFAbsoluteTimeGetCurrent();
    [_inputStream open];
    NSInteger length = 1;
    while (length > 0)
    {
        @autoreleasepool{
            uint8_t streamData[1024 * 4];
            length = [_inputStream read:streamData maxLength:1024 * 4];
            if (length > 0) {
                _crc64 = aos_crc64(_crc64, streamData, length);
            }
        }
    }
    
    if (length < 0) {
        OSSLogError(@"there is an error when reading buffer from file!");
    }
    [_inputStream close];
    
    CFAbsoluteTime duration =  CFAbsoluteTimeGetCurrent() - _startTime;
    OSSLogDebug(@"read file cost time is :%f",duration);
    
    dispatch_semaphore_signal(_semaphore);
}
 
- (uint64_t)crc64
{
    return _crc64;
}
 
@end