开发者

Close InputStream after renderBinary() call in Play Framework

开发者 https://www.devze.com 2023-03-20 02:17 出处:网络
In the Play framework there is a render method called renderBinary where it is possible to render an InputStream.

In the Play framework there is a render method called renderBinary where it is possible to render an InputStream.

It was my understanding that no code is run after the renderBinary() method call, so how do we go about closing the InputStream?

Take the following example:

    FileInputStream inputStream = null;
    try{
        //create temp file
        File tempFile = File.createTempFile( "foobar", ".txt" );

        inputStream = new FileInputStream( tempFile );

        //delete temp file
        tempFile.delete();

  开发者_运维知识库      //download the file as held in the inputstream
        renderBinary( inputStream, foobar + ".txt" );
    }
    catch( Exception e ){
        throw e;
    }
    finally {
        if( inputStream != null )
            inputStream.close();
    }

Would this be a suitable way of closing the InputStream or would finally never get called?

I know there will be a 'try it and see' answer, and I have tried it and I do not get any 'out of memory' errors when I repeatedly download big files in quick sucession - but I do not know how I could go about checking whether the InputStream has actually closed.


You can check the source code of the class that implements that code, it is closed. I really recommend downloading the source and linking it to your project, Play does some "magic" behind the scenes which is good to see :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号