提交 e34a541b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract method DataUtils.parseChecksummedMap()

上级 edf2e544
......@@ -10,6 +10,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
......@@ -672,6 +673,26 @@ public final class DataUtils {
return map;
}
/**
* Parse a key-value pair list and checks its checksum.
*
* @param s the list
* @return the map without mapping for {@code "fletcher"}, or {@code null} if checksum is wrong
* @throws IllegalStateException if parsing failed
*/
public static HashMap<String, String> parseChecksummedMap(String s) {
HashMap<String, String> m = DataUtils.parseMap(s);
int check = DataUtils.readHexInt(m, "fletcher", 0);
m.remove("fletcher");
s = s.substring(0, s.lastIndexOf("fletcher") - 1);
byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes, bytes.length);
if (check == checksum) {
return m;
}
return null;
}
/**
* Parse a name from key-value pair list.
*
......
......@@ -577,7 +577,9 @@ public final class MVStore {
try {
String s = new String(buff, 0, BLOCK_SIZE,
StandardCharsets.ISO_8859_1).trim();
HashMap<String, String> m = DataUtils.parseMap(s);
HashMap<String, String> m = DataUtils.parseChecksummedMap(s);
if (m == null)
continue;
int blockSize = DataUtils.readHexInt(
m, "blockSize", BLOCK_SIZE);
if (blockSize != BLOCK_SIZE) {
......@@ -586,15 +588,6 @@ public final class MVStore {
"Block size {0} is currently not supported",
blockSize);
}
int check = DataUtils.readHexInt(m, "fletcher", 0);
m.remove("fletcher");
s = s.substring(0, s.lastIndexOf("fletcher") - 1);
byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes,
bytes.length);
if (check != checksum) {
continue;
}
long version = DataUtils.readHexLong(m, "version", 0);
if (newest == null || version > newest.version) {
validStoreHeader = true;
......@@ -805,13 +798,8 @@ public final class MVStore {
byte[] buff = new byte[Chunk.FOOTER_LENGTH];
lastBlock.get(buff);
String s = new String(buff, StandardCharsets.ISO_8859_1).trim();
HashMap<String, String> m = DataUtils.parseMap(s);
int check = DataUtils.readHexInt(m, "fletcher", 0);
m.remove("fletcher");
s = s.substring(0, s.lastIndexOf("fletcher") - 1);
byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes, bytes.length);
if (check == checksum) {
HashMap<String, String> m = DataUtils.parseChecksummedMap(s);
if (m != null) {
int chunk = DataUtils.readHexInt(m, "chunk", 0);
Chunk c = new Chunk(chunk);
c.version = DataUtils.readHexLong(m, "version", 0);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论