From 2547d94cc8696e47f2d3f22243003228f597c82a Mon Sep 17 00:00:00 2001 From: Ghost_chu Date: Tue, 31 Dec 2024 23:29:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8A=B1=E9=87=8C=E8=83=A1?= =?UTF-8?q?=E5=93=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/analyse/AnalyseService.java | 13 +-- .../btn/sparkle/util/LargeFileReader.java | 100 ------------------ 2 files changed, 7 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/com/ghostchu/btn/sparkle/util/LargeFileReader.java diff --git a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java index 87d7731..ece5d4b 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java +++ b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java @@ -7,7 +7,10 @@ import com.ghostchu.btn.sparkle.module.banhistory.internal.BanHistoryRepository; import com.ghostchu.btn.sparkle.module.clientdiscovery.ClientDiscoveryService; import com.ghostchu.btn.sparkle.module.clientdiscovery.ClientIdentity; -import com.ghostchu.btn.sparkle.util.*; +import com.ghostchu.btn.sparkle.util.IPMerger; +import com.ghostchu.btn.sparkle.util.IPUtil; +import com.ghostchu.btn.sparkle.util.PeerUtil; +import com.ghostchu.btn.sparkle.util.TimeUtil; import inet.ipaddr.IPAddress; import inet.ipaddr.format.util.DualIPv4v6Tries; import io.micrometer.core.instrument.MeterRegistry; @@ -265,13 +268,11 @@ private void scanFile(Consumer predicate, ExecutorService service log.error("Tracker dump file not found: {}", dumpFilePath); return; } - try (FileInputStream fis = new FileInputStream(file); FileLock ignored = fis.getChannel().lock(0L, Long.MAX_VALUE, true)) { - LargeFileReader reader = new LargeFileReader(fis.getChannel()); - while (reader.available() > 0) { - int sizeInIntNeedToFix = ByteBuffer.wrap(reader.read(4)).order(ByteOrder.LITTLE_ENDIAN).getInt(); + while (fis.available() > 0) { + int sizeInIntNeedToFix = ByteBuffer.wrap(fis.readNBytes(4)).order(ByteOrder.LITTLE_ENDIAN).getInt(); long remainingToRead = Integer.toUnsignedLong(sizeInIntNeedToFix); - byte[] buffer = reader.read((int) remainingToRead); + byte[] buffer = fis.readNBytes((int) remainingToRead); var peerInfo = Peer.PeerInfo.parseFrom(buffer); service.submit(() -> predicate.accept(peerInfo)); } diff --git a/src/main/java/com/ghostchu/btn/sparkle/util/LargeFileReader.java b/src/main/java/com/ghostchu/btn/sparkle/util/LargeFileReader.java deleted file mode 100644 index 91fba94..0000000 --- a/src/main/java/com/ghostchu/btn/sparkle/util/LargeFileReader.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.ghostchu.btn.sparkle.util; - -import java.io.IOException; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.util.ArrayList; -import java.util.List; - -public class LargeFileReader { - private final List buffers = new ArrayList<>(); - private final long fileSize; - private final int chunkSize = Integer.MAX_VALUE; // 每个 MappedByteBuffer 的最大大小 - private long cursor = 0; - - public LargeFileReader(FileChannel channel) throws IOException { - this.fileSize = channel.size(); - long offset = 0; - while (offset < fileSize) { - long size = Math.min(chunkSize, fileSize - offset); - buffers.add(channel.map(FileChannel.MapMode.READ_ONLY, offset, size)); - offset += size; - } - } - - // 读取指定长度的数据并移动光标 - public byte[] read(int length) throws IOException { - if (cursor >= fileSize) { - return null; // 已到文件末尾 - } - int remaining = (int) Math.min(length, fileSize - cursor); - byte[] data = new byte[remaining]; - int bytesRead = 0; - - while (bytesRead < remaining) { - MappedByteBuffer buffer = getBufferForOffset(cursor); - int bufferOffset = (int) (cursor % chunkSize); - buffer.position(bufferOffset); - int toRead = Math.min(buffer.remaining(), remaining - bytesRead); - buffer.get(data, bytesRead, toRead); - bytesRead += toRead; - cursor += toRead; - } - return data; - } - - // 从指定偏移量读取指定长度的数据,不影响光标 - public byte[] rawRead(long offset, int length) { - if (offset >= fileSize) { - throw new IllegalArgumentException("Offset exceeds file size"); - } - int remaining = (int) Math.min(length, fileSize - offset); - byte[] data = new byte[remaining]; - int bytesRead = 0; - - while (bytesRead < remaining) { - MappedByteBuffer buffer = getBufferForOffset(offset); - int bufferOffset = (int) (offset % chunkSize); - buffer.position(bufferOffset); - int toRead = Math.min(buffer.remaining(), remaining - bytesRead); - buffer.get(data, bytesRead, toRead); - bytesRead += toRead; - offset += toRead; - } - return data; - } - - // 根据偏移量获取对应的 MappedByteBuffer - private MappedByteBuffer getBufferForOffset(long offset) { - int bufferIndex = (int) (offset / chunkSize); - return buffers.get(bufferIndex); - } - - public boolean hasNext() { - return cursor < fileSize; - } - - public void close() { - buffers.clear(); // 清理所有缓冲区 - } - - public long getCursor() { - return cursor; - } - - public void setCursor(long position) { - if (position < 0 || position > fileSize) { - throw new IllegalArgumentException("Invalid cursor position"); - } - this.cursor = position; - } - - public long available() { - return fileSize - cursor; - } - - - public long getFileSize() { - return fileSize; - } -}