package org.springblade.mdm.utils;
|
|
import java.io.*;
|
import java.util.Arrays;
|
|
public class CustomBinaryReader {
|
/**
|
* 从输入流读取数据到输出流
|
* @param inputStream
|
* @param os
|
* @throws IOException
|
*/
|
public static void read(InputStream inputStream, OutputStream os) throws IOException {
|
byte[] buffer = new byte[1024];
|
try (DataInputStream in = new DataInputStream(
|
new BufferedInputStream(inputStream))) {
|
|
// 读取并验证Magic Number
|
byte[] magic = new byte[4];
|
in.readFully(magic);
|
if (!Arrays.equals(magic, CustomBinaryWriter.MAGIC_NUMBER)) {
|
throw new RuntimeException("不是有效的自定义文件格式");
|
}
|
|
// 读取版本号
|
short version = in.readShort();
|
if (version > CustomBinaryWriter.VERSION) {
|
throw new RuntimeException("不支持的版本: " + version);
|
}
|
|
while(in.read(buffer) != -1){
|
os.write(buffer);
|
}
|
|
}
|
}
|
}
|