1# Raw File Development 2 3 4 5## When to Use 6 7This document describes how to use the native Rawfile APIs to manage raw file directories and files in OpenHarmony. You can use the APIs to traverse, open, search for, read, and close raw files. 8 9## Available APIs 10 11| API | Description | 12| :----------------------------------------------------------- | :--------------------------------------- | 13| NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) | Initializes the native resource manager. | 14| RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName) | Opens a raw file directory. | 15| int OH_ResourceManager_GetRawFileCount(RawDir *rawDir) | Obtains the number of raw files in the specified directory.| 16| const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index) | Obtains the name of a raw file. | 17| RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName) | Opens a raw file. | 18| long OH_ResourceManager_GetRawFileSize(RawFile *rawFile) | Obtains the size of a raw file. | 19| int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence) | Seeks a read/write position in a raw file based on the specified offset. | 20| long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile) | Obtains the offset. | 21| int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length) | Reads a raw file. | 22| void OH_ResourceManager_CloseRawFile(RawFile *rawFile) | Closes a raw file to release resources. | 23| void OH_ResourceManager_CloseRawDir(RawDir *rawDir) | Closes a raw file directory to release resources. | 24| bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor) | Obtains the file descriptor (FD) of a raw file. | 25| bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor) | Releases the FD of a raw file. | 26| void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr) | Releases the native resource manager. | 27 28## How to Develop 29 301. Add the header file. 31 32 ```c++ 33 #include "raw_file_manager.h" 34 ``` 35 36 37 382. Call **OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr)** to obtain a **NativeResourceManager** instance. 39 40 ```js 41 // Import the JS resource manager from the JS head file and pass it to the C++ file. 42 import resManager from '@ohos.resourceManager' 43 import rawfileTest from 'librawFileTest.so' 44 resManager.getResourceManager().then(resmgr => { 45 rawfileTest.testRawFile("test", resmgr, (error, value) => { 46 console.log("test rawFile"); 47 }) 48 }); 49 ``` 50 51 ```c++ 52 // Obtain and parse the parameters in the C++ file. 53 NativeResourceManager* nativeResourceManager = nullptr; 54 std::string path; 55 if (i == 0 && valueType == napi_string) { 56 // Parse the first parameter, which is the file or directory path relative to the raw file directory. 57 ...... 58 path = buf.data(); 59 } else if (i == 1 && valueType == napi_object) { 60 // Parse the second parameter, which is the JS resource manager. 61 nativeResourceManager = OH_ResourceManager_InitNativeResourceManager(env, argv[i]); 62 } 63 ``` 64 65 66 673. Call **OH_ResourceManager_OpenRawDir** to obtain a **RawDir** instance based on the **NativeResourceManager** instance. 68 69 ```c++ 70 RawDir* rawDir = OH_ResourceManager_OpenRawDir(nativeResourceManager, path.c_str()); 71 ``` 72 73 74 754. Call **OH_ResourceManager_GetRawFileCount** to obtain the total number of raw files in the directory based on the **RawDir** instance. 76 77 ```c++ 78 int count = OH_ResourceManager_GetRawFileCount(rawDir); 79 ``` 80 81 82 835. Call **OH_ResourceManager_GetRawFileName** to obtain the name of the raw file with the specified index. 84 85 ```c++ 86 for (int index = 0; index < count; index++) { 87 std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, index); 88 } 89 ``` 90 91 92 936. Call **OH_ResourceManager_OpenRawFile** to obtain a **RawFile** instance with the specified file name. 94 95 ```c++ 96 RawFile* rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName.c_str()); 97 ``` 98 99 100 1017. Call **OH_ResourceManager_GetRawFileSize** to obtain the size of the raw file. 102 103 ```c++ 104 long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile); 105 ``` 106 107 108 1098. Call **OH_ResourceManager_SeekRawFile** to seek a read/write position in the raw file based on the specified offset. 110 111 ```c++ 112 int position = OH_ResourceManager_SeekRawFile(rawFile, 10, 0); 113 int position = OH_ResourceManager_SeekRawFile(rawFile, 0 , 1); 114 int position = OH_ResourceManager_SeekRawFile(rawFile, -10, 2); 115 ``` 116 117 118 1199. Call **OH_ResourceManager_GetRawFileOffset** to obtain the raw file offset. 120 121 ```c++ 122 long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile) 123 ``` 124 125 126 12710. Call **OH_ResourceManager_ReadRawFile** to read the raw file. 128 129 ```c++ 130 std::unique_ptr<char[]> mediaData = std::make_unique<char[]>(rawFileSize); 131 long rawFileOffset = OH_ResourceManager_ReadRawFile(rawFile, mediaData.get(), rawFileSize); 132 ``` 133 134 135 13611. Call **OH_ResourceManager_CloseRawFile** to close the file to release resources. 137 138 ```c++ 139 OH_ResourceManager_CloseRawFile(rawFile); 140 ``` 141 142 143 14412. Call **OH_ResourceManager_CloseRawDir** to close the raw file directory. 145 146 ```c++ 147 OH_ResourceManager_CloseRawDir(rawDir); 148 ``` 149 150 151 15213. Call **OH_ResourceManager_GetRawFileDescriptor** to obtain the FD of the raw file. 153 154 ```c++ 155 RawFileDescriptor descriptor; 156 bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor); 157 ``` 158 159 160 16114. Call **OH_ResourceManager_ReleaseRawFileDescriptor** to release the FD of the raw file. 162 163 ```c++ 164 OH_ResourceManager_ReleaseRawFileDescriptor(descriptor); 165 ``` 166 167 168 16915. Call **OH_ResourceManager_ReleaseNativeResourceManager** to release the native resource manager. 170 171 ```c++ 172 OH_ResourceManager_ReleaseNativeResourceManager(nativeResourceManager); 173 ``` 174