gpt4 book ai didi

objective-c - 更改 header 信息二进制并将其写回

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:13 26 4
gpt4 key购买 nike

我需要更改文档文件夹中文件的标题信息。最推荐的读写二进制数据的方法是什么?

  1. 如何从文件夹二进制读取数据到数组/流
  2. 如何将数据从数组/流写回本地 iPad 文件夹?

Binary reading / writing in objective-c

最佳答案

如果您不更具体一点,这个问题很难回答,但最有效的方法可以说是根本不更改文件。

由于我们谈论的是 iOS,因此除了您的应用程序本身之外,没有对这些文档的文件系统级访问权限。那么,为什么不将您想要与文件相关联的附加/自定义 header 数据保存在应用程序范围的元数据存储中(例如 iTunes 或 iPhoto)并将它们与实际文件交错仅在导出期间 header ?

无论如何,我真的没有看到一个令人信服的理由来使用 C 级文件函数来更改这些数据:NSInputStream 为您提供流式文件读取访问和 NSOutputStream 可用于将数据流式传输到文件。

如果您采纳我上面的建议,您最终可能会得到这样的 API:

typedef void (^DataExportHandler)(NSData *resultData, NSError *exportError);

@interface DataStore (FileExport)

/** If you wanted to abort the export, you could pass the stream into the `abort…:`-method
@param identifier Something that you use internally to manage your stored files.
@param error For good measure…
@return The export stream for the object or `nil` if an error occurred.
*/
- (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error;

/** If your data are mostly small, it may be more convenient to not consume the exports as streams but as BLOBs, if the sizes vary you could implement this as a convenience…
@param identifier Equivalent to the identifier in the method above
@param handler Callback that is invoked once some time later when the export finished or failed. **Must not** be `nil`.
*/
@return A cancellation token.
- (id)asynchronouslyExportDataForObjectWithIdentifier:(id)identifier resultHandler:(DataExportHandler)handler;

/**
@param exportToken Either a stream from the first method or a token returned from the second one.
*/
- (void)abortAsynchronousExportWithToken:(id)exportToken;

@end

假设 ARC 并且不知道您必须做什么才能将额外的元数据与原始数据交织在一起,这就是实现的样板部分可能的样子。

牛肉显然在我没有在这里展示的部分:rawDataStream 的委托(delegate)的实现,您将在其中使用原始文件中的数据,将 header 与您的附加信息。虽然这可能应该被分解到一个单独的类中,但我只是暗示数据存储相应地实现了 NSStreamDelegate 回调。

在标题之后,您只需通过文件的其余部分......

/// Scribble of another helper class that can be used whenever one needs to consume a stream for its aggregate data:
@interface _StreamConsumer : NSObject <NSStreamDelegate> {
NSInputStream *_stream;
DataExportHandler _handler;
NSMutableData *_data;
}

// initiate the data, set itself as the stream’s delegate, open and schedule the stream in a runloop.
- (id)initWithInputStream:(NSInputStream *)stream resultHandler:(DataExportHandler)handler;

// forward the close to the stream
- (void)close;

// Implementation of the stream delegate callbacks can be more or less copy-pasted from Apple’s Stream Programming Guide (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Streams.html)
@end

@implementation DataStore (FileExport)

- (id)asynchronouslyExportDataForObjectWithIdentifier:(id)someUniqueIdentifier resultHandler:(void (^)(NSData *fileData, NSError *exportError))
{
NSParameterAssert(handler);
handler = [handler copy];

NSError *setupError;
NSInputStream *exportStream = [self exportStreamForObjectWithIdentifier:someUniqueIdentifier error:&setupError];
if (!exportStream)
{
dispatch_async(dispatch_get_current_queue(), ^{
handler(nil, setupError);
});

return nil;
}

_StreamConsumer *helper = [[_StreamConsumer alloc] initWithStream:exportStream resultHandler:handler];

return helper;
}

- (void)abortAsynchronousExportWithToken:(id)exportToken
{
[exportToken close];
}

- (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error
{
// do your thing to retrieve the URL to the actual data-file and then:
NSInputStream *rawDataStream = [NSInputStream inputStreamWithURL:rawFileURL];

if (!rawDataStream)
{
// populate the error in a meaningful way
return nil;
}

CFReadStream cfExportStream;
CFWriteStream cfBuffer;
CFStreamCreateBoundPair(kCFAllocatorDefault, &cfExportStream, &cfBuffer, someValueYouHaveTuned);

if (!cfExportStream || !cfBuffer)
{
// error population
return nil;
}

NSInputStream *exportStream = (__bridge_transfer NSInputStream *)cfExportStream;

// HACKITY HACK: In reality, you’d want this stuff separated!
// For the sake of simplicity, take the responsibility for that ourselves
_exportBuffer = (__bridge_transfer NSOutputStream *)cfBuffer;

rawDataStream.delegate = self;
[rawDataStream open];
[rawDataStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunloopDefaultMode];
// END: HACKITY HACK

return exportStream;
}

@end

关于objective-c - 更改 header 信息二进制并将其写回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649525/

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