Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The code of the file module is aligned with the Java 20 version. #723 #724

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions jcommon/file/README.md
Original file line number Diff line number Diff line change
@@ -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(自研日志系统)的基础。
This library serves as the foundation for MiLog (self-developed logging system).
19 changes: 19 additions & 0 deletions jcommon/file/src/main/java/com/xiaomi/mone/file/ILogFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xiaomi.mone.file;

import java.io.IOException;

/**
* @author [email protected]
* @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);

}
25 changes: 20 additions & 5 deletions jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,10 +17,10 @@
* @author [email protected]
*/
@Slf4j
public class LogFile {
public class LogFile implements ILogFile{

@Getter
private final String file;
private String file;

private MoneRandomAccessFile raf;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 大行文件先临时截断
Expand Down Expand Up @@ -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();
}


Expand Down
80 changes: 41 additions & 39 deletions jcommon/file/src/main/java/com/xiaomi/mone/file/LogFileWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
Expand Down Expand Up @@ -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();
Expand All @@ -120,40 +119,40 @@ public void readLine() throws IOException {
continue;
}
List<WatchEvent<?>> 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();
}
Expand Down Expand Up @@ -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();
}


Expand Down