Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions core/src/main/java/org/kohsuke/stapler/framework/io/LargeText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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.
Expand Down