From 74d67625d4f03796b27bd95fd6d3c676376e0400 Mon Sep 17 00:00:00 2001 From: liucc52 Date: Wed, 18 Dec 2019 09:42:58 +0800 Subject: [PATCH] add new writeLogTo method add end parameter to avoid large log file cause memory leak --- .../stapler/framework/io/LargeText.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/core/src/main/java/org/kohsuke/stapler/framework/io/LargeText.java b/core/src/main/java/org/kohsuke/stapler/framework/io/LargeText.java index b83af09459..cac9238375 100644 --- a/core/src/main/java/org/kohsuke/stapler/framework/io/LargeText.java +++ b/core/src/main/java/org/kohsuke/stapler/framework/io/LargeText.java @@ -193,6 +193,13 @@ public void close() throws IOException { public long writeLogTo(long start, Writer w) throws IOException { return writeLogTo(start, new WriterOutputStream(w, charset)); } + + /** + * add end parameter to avoid large log file cause memory leak + **/ + public long writeLogTo(long start,long end, Writer w) throws IOException { + return writeLogTo(start, end,new WriterOutputStream(w, charset)); + } /** * Writes the tail portion of the file to the {@link OutputStream}. @@ -236,6 +243,47 @@ public long writeLogTo(long start, OutputStream out) throws IOException { return os.getByteCount()+start; } + + /** + * add end parameter to avoid large log file cause memory leak + **/ + public long writeLogTo(long start,long end, OutputStream out) throws IOException { + CountingOutputStream os = new CountingOutputStream(out); + + Session f = source.open(); + f.skip(start); + + if(completed) { + // write everything till EOF + byte[] buf = new byte[1024]; + int sz; + while((sz=f.read(buf))>=0) { + os.write(buf,0,sz); + if(os.getCount()>end) { + break; + } + } + } else { + ByteBuf buf = new ByteBuf(null,f); + HeadMark head = new HeadMark(buf); + TailMark tail = new TailMark(buf); + buf = null; + + int readLines = 0; + while(tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) { + head.moveTo(tail,os); + if(os.getCount()>end) { + break; + } + } + head.finish(os); + } + + f.close(); + os.flush(); + + return os.getByteCount()+start; + } /** * Implements the progressive text handling.