Stream handling in RandomAccessSourceFaction.CreateSource(Stream inp)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Stream handling in RandomAccessSourceFaction.CreateSource(Stream inp)

jimbobmcgee
The below, taken verbatim from RandomAccessSourceFactory, is not behaving well.  Please do not close streams that have been passed to you; they do not belong to you.  Doing so prevents expected usability of the stream.

This is especially the case in the PdfReader constructor.  The documentation for the Java version states "is - the InputStream containing the document. The stream is read to the end but is not closed"; thanks to the above, this does not hold true for the .NET version.

(BTW, where is the .NET documentation; there are many subtle differences between the Java and .NET versions, which are not always immediately obvious)

Cheers.



public IRandomAccessSource CreateSource(Stream inp)
{
    IRandomAccessSource randomAccessSource;
    try
    {
        randomAccessSource = this.CreateSource(StreamUtil.InputStreamToArray(inp));
    }
    finally
    {
        try
        {
            inp.Close(); // don't do this; it is not your stream to close...
        }
        catch
        {
        }
    }
    return randomAccessSource;
}