gpt4 book ai didi

ios - 调度队列和异步 RNCryptor

转载 作者:可可西里 更新时间:2023-11-01 03:26:44 25 4
gpt4 key购买 nike

这是 Asynchronously decrypt a large file with RNCryptor on iOS 的后续行动

我已经成功地使用这篇文章中描述的方法异步解密了一个大型下载文件 (60Mb),Calman 在他的回答中更正了这一点。

基本上是这样的:

int blockSize = 32 * 1024;
NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:...];
NSOutputStream *decryptedStream = [NSOutputStream output...];

[cryptedStream open];
[decryptedStream open];

RNDecryptor *decryptor = [[RNDecryptor alloc] initWithPassword:@"blah" handler:^(RNCryptor *cryptor, NSData *data) {
NSLog("Decryptor recevied %d bytes", data.length);
[decryptedStream write:data.bytes maxLength:data.length];
if (cryptor.isFinished) {
[decryptedStream close];
// call my delegate that I'm finished with decrypting
}
}];

while (cryptedStream.hasBytesAvailable) {
uint8_t buf[blockSize];
NSUInteger bytesRead = [cryptedStream read:buf maxLength:blockSize];
NSData *data = [NSData dataWithBytes:buf length:bytesRead];

[decryptor addData:data];
NSLog("Sent %d bytes to decryptor", bytesRead);
}

[cryptedStream close];
[decryptor finish];

但是,我仍然面临一个问题:整个数据在解密之前加载到内存中。我可以看到一堆“发送 X 字节到解密器”,然后,当我想看到“发送,接收,发送,接收,.. ”。

这对于小 (2Mb) 文件或模拟器上的大 (60Mb) 文件都很好;但在真正的 iPad1 上,由于内存限制,它会崩溃,所以显然我不能为我的生产应用程序保留这个过程。

我觉得我需要使用 dispatch_async 将数据发送到解密器,而不是盲目地在 while 循环中发送数据,但是我完全迷路了。我试过:

  • while 之前创建我自己的队列,并使用 dispatch_async(myQueue, ^{ [decryptor addData:data]; });
  • 相同,但在 while 循环中分派(dispatch)整个代码
  • 相同,但分派(dispatch)整个 while 循环
  • 使用 RNCryptor 提供的 responseQueue 而不是我自己的队列

这 4 种变体之间没有任何作用。

我还没有完全理解调度队列;我觉得问题出在这里。如果有人能阐明这一点,我会很高兴。

最佳答案

如果你一次只想处理一个 block ,那么只在第一个 block 回调你时才处理一个 block 。你不需要信号量来做到这一点,你只需要在回调中执行下一次读取。您可能需要在 readStreamBlock 中使用一个 @autoreleasepool block ,但我认为您不需要它。

当我有时间的时候,我可能会将其直接包装到 RNCryptor 中。我打开了Issue#47为了它。我愿意接受拉取请求。

// Make sure that this number is larger than the header + 1 block.
// 33+16 bytes = 49 bytes. So it shouldn't be a problem.
int blockSize = 32 * 1024;

NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:@"C++ Spec.pdf"];
NSOutputStream *decryptedStream = [NSOutputStream outputStreamToFileAtPath:@"/tmp/C++.crypt" append:NO];

[cryptedStream open];
[decryptedStream open];

// We don't need to keep making new NSData objects. We can just use one repeatedly.
__block NSMutableData *data = [NSMutableData dataWithLength:blockSize];
__block RNEncryptor *decryptor = nil;

dispatch_block_t readStreamBlock = ^{
[data setLength:blockSize];
NSInteger bytesRead = [cryptedStream read:[data mutableBytes] maxLength:blockSize];
if (bytesRead < 0) {
// Throw an error
}
else if (bytesRead == 0) {
[decryptor finish];
}
else {
[data setLength:bytesRead];
[decryptor addData:data];
NSLog(@"Sent %ld bytes to decryptor", (unsigned long)bytesRead);
}
};

decryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
password:@"blah"
handler:^(RNCryptor *cryptor, NSData *data) {
NSLog(@"Decryptor recevied %ld bytes", (unsigned long)data.length);
[decryptedStream write:data.bytes maxLength:data.length];
if (cryptor.isFinished) {
[decryptedStream close];
// call my delegate that I'm finished with decrypting
}
else {
// Might want to put this in a dispatch_async(), but I don't think you need it.
readStreamBlock();
}
}];

// Read the first block to kick things off
readStreamBlock();

关于ios - 调度队列和异步 RNCryptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com