# Filtering in Stream Mode

To use the Filtering stream-based I/O

1. Load the `kvfilter` library and obtain the KV_GetFilterInterfaceEx() entry point. The `filter` sample program contains sample code for all platforms.
2. Initialize a filter session by calling fpInit() or fpInitWithLicenseData(). This function's return value, `pContext`, is passed as the first argument to all other Filter functions.
3. Pass the context pointer from fpInit() or fpInitWithLicenseData() and the address of a structure that contains pointers to the File Extraction API functions in the call to `KVGetExtractInterface()`. See KVGetExtractInterface().
4. Create an input stream (`KVInputStream`) by calling fpFileToInputStreamCreate() or by using code similar to the example code in the `Filter` sample program.
5. Open the stream by calling fpOpenStream().
6. Declare the input stream in the KVOpenFileArg structure.

7. Open the source file by calling fpOpenFile() and passing the `KVOpenFileArg` structure. This call defines the parameters necessary to open a file for extraction.

   NOTE: With fpOpenFile(), you can only open one container at a time (per context pointer).

8. Determine whether the source file is a container file (that is, whether it contains subfiles) by calling fpGetMainFileInfo().
9. If the call to `fpGetMainFileInfo()` determined that the source file is a container file, proceed to step 10; otherwise, proceed to step 13.
10. Determine whether the subfile is a container file by calling fpGetSubFileInfo().

11. Extract the subfile to a stream by calling fpExtractSubFile().

    TIP: Alternatively you can open the subfile as a random access input stream by calling fpOpenSubFile(). If you want to pass the stream back into KeyView, you must create a new context using fpInit() or fpInitWithLicenseData() (and use the new context on the same thread).

12. If the call to `fpGetSubFileInfo()` determined that the subfile is a container file, repeat step 4 through step 11 until all subfiles are extracted; otherwise, proceed to step 13.

13. Filter the stream by calling fpFilterStream(). Call `fpFilterStream()` repeatedly until the entire output buffer is processed. After each call to `fpFilterStream()`, call fpFreeFilterOutput() to free the text buffer returned.

    NOTE: After calling fpFilterStream(), you must finish filtering the stream before calling any other interface function. You can do this by calling fpFilterStream() repeatedly, or by calling fpCloseStream() at any time.

14. Close the stream by calling fpCloseStream().
15. Free the memory allocated for the input stream by calling fpFileToInputStreamFree().
16. Close the file by calling fpCloseFile().
17. Repeat Step 4 through Step 16 as required for additional source files.
18. Terminate the filter session by calling fpShutdown().

_FT_HTML5_bannerTitle.htm