package com.yida.data.common.core.utils.file;

import com.yida.data.common.core.exception.FebsException;
import lombok.extern.slf4j.Slf4j;

import java.io.RandomAccessFile;

/**
 * 分片工具类
 *
 * @author ZYJ
 * @date 2023/10/20 10:35
 */
@Slf4j
public class ChunkUtil {

    /**
     * 获取分片字节信息
     */
    public static byte[] getChunk(Integer index, Integer chunkSize, String resultFileName, long offset) {
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(resultFileName, "r")) {
            // 定位到该分片的偏移量
            randomAccessFile.seek(offset);
            //读取
            byte[] buffer = new byte[chunkSize];
            randomAccessFile.read(buffer);
            return buffer;
        } catch (Exception e) {
            log.error("获取分片信息失败");
            throw new FebsException("获取分片信息失败");
        }
    }
}
