zyc 689fa8936b Integrate Volcengine realtime voice + Live2D mouth driving
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 15:39:23 +08:00

72 lines
2.0 KiB
Objective-C

//
// FileUtils.m
// SpeechDemo
//
// Created by fangweiwei on 2020/6/16.
// Copyright © 2020 fangweiwei. All rights reserved.
//
#import "FileUtils.h"
@implementation FileUtils
+ (NSFileHandle *)openFileForReading:(NSString *)filename inPath:(NSString *)path {
NSString *filePath = [path stringByAppendingPathComponent:filename];
return [NSFileHandle fileHandleForReadingAtPath:filePath];
}
+ (NSFileHandle *)openFileForWriting:(NSString *)filename inPath:(NSString *)path {
NSString *filePath = [path stringByAppendingPathComponent:filename];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
return [NSFileHandle fileHandleForUpdatingAtPath:filePath];
}
+ (BOOL)writeData:(NSData *)data toFileHandel:(NSFileHandle *)fileHandle {
if (fileHandle && data) {
@try {
[fileHandle writeData:data];
} @catch (NSException *exception) {
NSLog(@"FileUtils, write data failed: %@", exception.description);
return FALSE;
}
return TRUE;
}
return FALSE;
}
+ (BOOL)writeString:(NSString *)data toFileHandel:(NSFileHandle *)fileHandle {
if (fileHandle && data) {
@try {
[fileHandle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]];
} @catch (NSException *exception) {
NSLog(@"FileUtils, write data failed: %@", exception.description);
return FALSE;
}
return TRUE;
}
return FALSE;
}
+ (BOOL)readData:(NSData **)data length:(NSUInteger)length fromFileHandel:(NSFileHandle *)fileHandle {
if (fileHandle && data) {
NSError *err;
if (@available(iOS 13.0, *)) {
*data = [fileHandle readDataUpToLength:length error:&err];
if (err) {
NSLog(@"FileUtils read data failed: %@.", err);
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
+ (void)closeFile:(NSFileHandle *)filehandle {
if (filehandle) {
[filehandle closeFile];
}
}
@end