Stream Me a River
Recently, I have uploaded new open source unit to my web pages - GpStreams. It contains a mix of TStream descendants, enhancers and helpers I have written over the last few years. Stream WindowTGpStreamWindow is a TStream descendant which provides a window into another stream. Sometimes you want to pass just a part of a stream to some method. For example, you have a stream containing a header and data and you want to pass it to a method that expects only data. If you can't change the method in question to ignore the header (maybe it's not a method you have written), then you would normally have to copy the data part to another stream and pass the copy to that method. Alternatively, you can wrap the stream into TGpStreamWindow and set limits to exclude the header, then pass the TGpStreamWindow instance to the method. TGpStreamWindow doesn't copy data but overrides Read, Write, and Seek methods. Relevant part of the interface: TGpStreamWindow = class(TStream) Example: procedure DoSomethingWithData(dataStream: TStream); Streamed Memory BufferTGpFixedMemoryStream is a TStream descendant which provides streamed access to a memory buffer. In a way, it is similar to the TStringStream except that the underlying data is stored in a constant-size memory buffer. Interface: TGpFixedMemoryStream = class(TStream) Stream enhancersTGpStreamEnhancer class contains various small helpers for the TStream class. Because of the technology used (class helpers), it can only be used in D2005 and newer. Also because of the technology used, TGpStreamEnhancer extends functionality of TStream and all descendant classes. TGpStreamEnhancer contains:
Stream WrappersTwo stream wrappers are included. Both are implemented with interfaces and use the fact that Delphi automatically destroy interfaces when they go out of scope to achieve some 'automagic' functionality. AutoDestroyStream automatically destroys a stream, when the wrapper goes out of scope. For example, ProcessStream example above could be rewritten with AutoDestroyStream to: procedure ProcessStream(headerAndData: TStream); KeepStreamPosition wrapper automatically resets stream position to the original value when wrapper is destroyed. Other UtilitiesFew 'unclassified' utilities are also included in the GpStreams unit. SafeCreateFileStream is a wrapper around TFileStream.Create that maps exception into function result. DestroyFileStreamAndDeleteFile destroys TFileStream and deletes the file that was used as a stream source. |