Console: Fix log clearing without wrapper (ticket #2449)

Use ABQ as buffer when reading
This commit is contained in:
zzz
2020-05-05 12:51:41 +00:00
parent 122b12fa31
commit 78effe14ad

View File

@ -6,8 +6,9 @@ import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
@ -17,7 +18,6 @@ import net.i2p.router.web.ConfigServiceHandler;
import net.i2p.router.web.CSSHelper; import net.i2p.router.web.CSSHelper;
import net.i2p.router.web.HelperBase; import net.i2p.router.web.HelperBase;
import net.i2p.router.web.RouterConsoleRunner; import net.i2p.router.web.RouterConsoleRunner;
import net.i2p.util.FileUtil;
import net.i2p.util.Translate; import net.i2p.util.Translate;
import net.i2p.util.UIMessages; import net.i2p.util.UIMessages;
@ -165,19 +165,15 @@ public class LogsHelper extends HelperBase {
if (lastMod > 0 && flastMod <= lastMod) { if (lastMod > 0 && flastMod <= lastMod) {
str = ""; str = "";
toSkip = -1; toSkip = -1;
} else if (_context.hasWrapper()) { } else {
// platform encoding // platform encoding or UTF8
boolean utf8 = !_context.hasWrapper();
StringBuilder buf = new StringBuilder(MAX_WRAPPER_LINES * 80); StringBuilder buf = new StringBuilder(MAX_WRAPPER_LINES * 80);
toSkip = readTextFile(f, MAX_WRAPPER_LINES, toSkip, buf); toSkip = readTextFile(f, utf8, MAX_WRAPPER_LINES, toSkip, buf);
if (toSkip >= 0) if (toSkip >= 0)
str = buf.toString(); str = buf.toString();
else else
str = null; str = null;
} else {
// UTF-8
// no skipping
str = FileUtil.readTextFile(f.getAbsolutePath(), MAX_WRAPPER_LINES, false);
toSkip = 0;
} }
String loc = DataHelper.escapeHTML(f.getAbsolutePath()); String loc = DataHelper.escapeHTML(f.getAbsolutePath());
if (str == null) { if (str == null) {
@ -288,30 +284,36 @@ public class LogsHelper extends HelperBase {
* so don't make it too big. * so don't make it too big.
* Warning - converts \r\n to \n * Warning - converts \r\n to \n
* *
* @param utf8 true for utf-8, false for system locale
* @param maxNumLines max number of lines (greater than zero) * @param maxNumLines max number of lines (greater than zero)
* @param skipLines number of lines to skip, or zero * @param skipLines number of lines to skip, or zero
* @param buf out parameter * @param buf out parameter
* @return -1 on failure, or number of lines in the file. Does not throw IOException. * @return -1 on failure, or number of lines in the file. Does not throw IOException.
* @since 0.9.11 modded from FileUtil.readTextFile() * @since 0.9.11 modded from FileUtil.readTextFile()
*/ */
private static long readTextFile(File f, int maxNumLines, long skipLines, StringBuilder buf) { private static long readTextFile(File f, boolean utf8, int maxNumLines, long skipLines, StringBuilder buf) {
if (!f.exists()) if (!f.exists())
return -1; return -1;
BufferedReader in = null; BufferedReader in = null;
try { try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(f))); if (utf8)
LinkedList<String> lines = new LinkedList<String>(); in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
else
in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
Queue<String> lines = new ArrayBlockingQueue<String>(maxNumLines);
long i = 0; long i = 0;
String line = null; synchronized(lines) {
while ( (line = in.readLine()) != null) { String line = null;
if (i++ < skipLines) while ( (line = in.readLine()) != null) {
continue; if (i++ < skipLines)
lines.add(line); continue;
if (lines.size() >= maxNumLines) if (lines.size() >= maxNumLines)
lines.removeFirst(); lines.poll();
} lines.offer(line);
for (String ln : lines) { }
buf.append(ln).append('\n'); for (String ln : lines) {
buf.append(ln).append('\n');
}
} }
return i; return i;
} catch (IOException ioe) { } catch (IOException ioe) {