From dea8ba05ff9faf3ce544c8bcda5ecc9fdd306f2f Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Wed, 20 Sep 2023 16:54:42 +0800 Subject: [PATCH] The code of the file module is aligned with the Java 20 version. #723 --- jcommon/file/README.md | 11 +-- .../java/com/xiaomi/mone/file/ILogFile.java | 19 +++++ .../java/com/xiaomi/mone/file/LogFile.java | 25 ++++-- .../java/com/xiaomi/mone/file/LogFileWS.java | 80 ++++++++++--------- 4 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 jcommon/file/src/main/java/com/xiaomi/mone/file/ILogFile.java diff --git a/jcommon/file/README.md b/jcommon/file/README.md index 5f63a13a8..515e0b580 100644 --- a/jcommon/file/README.md +++ b/jcommon/file/README.md @@ -1,8 +1,9 @@ -# 文件操作库 +# File Operation Library +`LogFile` is based on `MoneRandomAccessFile` to read the file content into memory and process it further +through `ReadListener`. -`LogFile`是以`MoneRandomAccessFile`为基础,将文件内容读取到内存,并通过`ReadListener`将文件内容进行后续的处理。 +`MLog` is used to handle exception stack information. It collects exception stack information into a local queue, +waiting to be retrieved. -`MLog`是处理异常栈信息。收集异常栈信息至本地队列中,等待被取走。 - -这个库是MiLog(自研日志系统)的基础。 \ No newline at end of file +This library serves as the foundation for MiLog (self-developed logging system). \ No newline at end of file diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/ILogFile.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/ILogFile.java new file mode 100644 index 000000000..d1d6f5e59 --- /dev/null +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/ILogFile.java @@ -0,0 +1,19 @@ +package com.xiaomi.mone.file; + +import java.io.IOException; + +/** + * @author goodjava@qq.com + * @date 2023/9/20 10:39 + */ +public interface ILogFile { + + void readLine() throws IOException; + + void setStop(boolean stop); + + void setReOpen(boolean reOpen); + + void initLogFile(String file, ReadListener listener, long pointer, long lineNumber); + +} diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile.java index e50257b0a..05e8fe902 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile.java @@ -6,7 +6,6 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import javax.xml.bind.DatatypeConverter; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; @@ -18,10 +17,10 @@ * @author goodjava@qq.com */ @Slf4j -public class LogFile { +public class LogFile implements ILogFile{ @Getter - private final String file; + private String file; private MoneRandomAccessFile raf; @@ -51,6 +50,10 @@ public class LogFile { private static final int LINE_MAX_LENGTH = 50000; + public LogFile() { + + } + public LogFile(String file, ReadListener listener) { this.file = file; this.md5 = md5(file); @@ -157,6 +160,15 @@ public void readLine() throws IOException { } } + @Override + public void initLogFile(String file, ReadListener listener, long pointer, long lineNumber) { + this.file = file; + this.md5 = md5(file); + this.listener = listener; + this.pointer = pointer; + this.lineNumber = lineNumber; + } + private String lineCutOff(String line) { if (null != line) { //todo 大行文件先临时截断 @@ -225,8 +237,11 @@ public String md5(String msg) { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(msg.getBytes()); byte[] digest = md.digest(); - return DatatypeConverter - .printHexBinary(digest).toUpperCase(); + StringBuilder sb = new StringBuilder(2 * digest.length); + for(byte b : digest) { + sb.append(String.format("%02x", b & 0xff)); + } + return sb.toString().toUpperCase(); } diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFileWS.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFileWS.java index f9da95e3b..08043057b 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFileWS.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFileWS.java @@ -6,14 +6,12 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import javax.xml.bind.DatatypeConverter; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.security.MessageDigest; import java.util.List; -import java.util.concurrent.TimeUnit; /** * @author goodjava@qq.com @@ -111,7 +109,8 @@ public void readLine() throws IOException { log.error("file.length() IOException, file:{}", this.file, e); } raf.seek(pointer); - readFile: while (true) { + readFile: + while (true) { WatchKey watchKey = null; try { watchKey = watchService.take(); @@ -120,40 +119,40 @@ public void readLine() throws IOException { continue; } List> watchEvents = watchKey.pollEvents(); - for(WatchEvent watchEvent : watchEvents) { - if (watchEvent.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) { - String line = raf.getNextLine(); - if (null != line) { - line = new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); - //todo 大行文件先临时截断 - if (line.length() > LINE_MAX_LENGTH) { - line = line.substring(0, LINE_MAX_LENGTH); - } - } - if (reOpen) { - pointer = 0; - lineNumber = 0; - break readFile; - } - if (stop) { - break readFile; - } - Long maxPointer = null; - try { - pointer = raf.getFilePointer(); - maxPointer = raf.length(); - } catch (IOException e) { - log.error("file.length() IOException, file:{}", this.file, e); - } - - ReadResult readResult = new ReadResult(); - readResult.setLines(Lists.newArrayList(line)); - readResult.setPointer(pointer); - readResult.setFileMaxPointer(maxPointer); - readResult.setLineNumber(++lineNumber); - ReadEvent event = new ReadEvent(readResult); - listener.onEvent(event); - } + for (WatchEvent watchEvent : watchEvents) { + if (watchEvent.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) { + String line = raf.getNextLine(); + if (null != line) { + line = new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); + //todo 大行文件先临时截断 + if (line.length() > LINE_MAX_LENGTH) { + line = line.substring(0, LINE_MAX_LENGTH); + } + } + if (reOpen) { + pointer = 0; + lineNumber = 0; + break readFile; + } + if (stop) { + break readFile; + } + Long maxPointer = null; + try { + pointer = raf.getFilePointer(); + maxPointer = raf.length(); + } catch (IOException e) { + log.error("file.length() IOException, file:{}", this.file, e); + } + + ReadResult readResult = new ReadResult(); + readResult.setLines(Lists.newArrayList(line)); + readResult.setPointer(pointer); + readResult.setFileMaxPointer(maxPointer); + readResult.setLineNumber(++lineNumber); + ReadEvent event = new ReadEvent(readResult); + listener.onEvent(event); + } } watchKey.reset(); } @@ -194,8 +193,11 @@ public String md5(String msg) { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(msg.getBytes()); byte[] digest = md.digest(); - return DatatypeConverter - .printHexBinary(digest).toUpperCase(); + StringBuilder sb = new StringBuilder(2 * digest.length); + for (byte b : digest) { + sb.append(String.format("%02x", b & 0xff)); + } + return sb.toString().toUpperCase(); }