i have log file, each of lines want use create logmessage
object. want stream lines file, , map each 1 new logmessage
. code below works, eclipse issues warning:
resource leak: 'linestream' never closed
public static stream<logmessage> streamsinglelinelogmessages(path path) { try { stream<string> linestream = files.lines(path, standardcharsets.iso_8859_1); stream<logmessage> logmessagestream = linestream.map(message -> new logmessage(path, message)); logmessagestream.onclose(linestream::close); return logmessagestream; } catch (ioexception e) { throw new runtimeexception(e); } }
if add finally
block, , close in there, stream closed when method returns (i think). in case, closed time come use it.
so best way ensure inner stream closed? or perhaps code correct is, eclipse doesn't realize it?
you shouldn't need of , have instead:
public static stream<logmessage> streamsinglelinelogmessages(path path) throws ioexception { return files.lines(path, standardcharsets.iso_8859_1) .map(message -> new logmessage(path, message)); }
the method files.lines(path, cs)
returns stream<path>
has close handler closing internal bufferedreader
. when stream mapped stream<logmessage>
, close handlers kept.
this means new stream<logmessage>
, there close handler closing bufferedreader
, don't need add yourself.
you need make sure when use method, wrap property inside try-with-resources
construct:
try (stream<logmessage> messagestream = streamsinglelinelogmessages(path)) { // stream }
Comments
Post a Comment