Need to close OutputStream when write file in GridFSDBFile.java(java driver, version 2.7.2)

318 views
Skip to first unread message

gen liu

unread,
Sep 5, 2012, 9:58:36 PM9/5/12
to mongod...@googlegroups.com
In java driver, I saw it has not close the OutputStream before return, here is the code(GridFSDBFile.java, line 66, version 2.7.2):

    public long writeTo( File f ) throws IOException {
        return writeTo( new FileOutputStream( f ) );
    }

    /**
     * Writes the file's data to an OutputStream
     * @param out the OutputStream
     * @return
     * @throws IOException
     */
    public long writeTo( OutputStream out )
        throws IOException {
        final int nc = numChunks();
        for ( int i=0; i<nc; i++ ){
            out.write( getChunk( i ) );
        }
        return _length; // is need to close out before return ? or the destructor function will close it?
    }

Scott Hernandez

unread,
Sep 5, 2012, 10:24:48 PM9/5/12
to mongod...@googlegroups.com
You can use the writeTo(OutputStream stream) method if you want
control over this:
http://api.mongodb.org/java/2.8.0/com/mongodb/gridfs/GridFSDBFile.html#writeTo(java.io.OutputStream)

The garbage collector will clean it up, but it is better to do it
sooner than later on high volume servers.
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb

Jeff Yemin

unread,
Sep 6, 2012, 7:12:58 AM9/6/12
to mongod...@googlegroups.com

gavinliu

unread,
Sep 6, 2012, 9:00:46 PM9/6/12
to mongod...@googlegroups.com
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages