FileSink

From Crypto++ Wiki
Jump to: navigation, search

FileSink.png

In the pipelining paradigm, FileSinks are a destination endpoint.

Examples

The following example demonstrates creation of a FileSink.

string filename;
FileSink f( filename, true /*binary*/ );

The following example demonstrates reading a file using a FileSource, and placing the contents of the file in a string. This is known as pipelining.

string s;
FileSource file( filename, true, new StringSink( s ) );

cout << s << endl;

true indicates the FileSource should propagate to all filters in the chain. A slightly more complicated example of pipelining is below. Before the FileSource is placed in the string, it is HexEncoded.

string s;
FileSource( filename, true, new HexEncoder( new StringSink( s ) ) );

cout << s << endl;

From above, note that the HexEncoder and StringSink created with new do not require explicit destruction - the FileSource will call delete on the HexEncoder, which in turns calls delete on the StringSink when it (the FileSource) is destroyed.

byte data[] = { 0x00, 0x01, 0x02, 0x03 };
string sink;

HexEncoder encoder;
encoder.Attach( new StringSink( sink ) );
    
encoder.Put( data, sizeof( data ) );
encoder.MessageEnd();

cout << sink << endl;