yangys
2025-07-04 fc588c6e5ccac038cab378931d9bac3033e28f98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
            }
 
        }
    }
}