package ymodem;
|
|
|
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
|
public class getFilesDataBytes {
|
public static final int SOH = 0x01; // 128字节数据包
|
public static final int STX = 0x02; // 1024字节数据包
|
private static int num = 0; // 帧序号
|
|
// 获取文件的起始数据包
|
public static byte[] getStartData(File file) {
|
int index = 0;
|
byte[] dat = new byte[133];
|
byte[] nameBytes = file.getName().getBytes();
|
byte[] lengthBytes = String.valueOf(file.length()).getBytes();
|
dat[index++] = SOH;
|
dat[index++] = (byte) num++;
|
dat[index++] = (byte) ~dat[1];
|
// 组合文件名数据
|
for (int i = 0; i < nameBytes.length; i++) {
|
dat[i + index] = nameBytes[i];
|
}
|
index += nameBytes.length;
|
dat[index++] = 0x00;
|
// 组合文件长度数据
|
for (int i = 0; i < lengthBytes.length; i++) {
|
dat[i + index] = lengthBytes[i];
|
}
|
index += lengthBytes.length;
|
dat[index++] = 0x00;
|
byte[] crcDat = CRC16.getCRC16(dat, 3);
|
dat[dat.length - 2] = crcDat[0];
|
dat[dat.length - 1] = crcDat[1];
|
return dat;
|
}
|
|
// 获取结束帧数据
|
public static byte[] getEndData() {
|
byte[] dat = new byte[133];
|
dat[0] = SOH;
|
dat[1] = (byte) 0x00;
|
dat[2] = (byte) 0xFF;
|
byte[] crcDat = CRC16.getCRC16(dat, 3);
|
dat[dat.length - 2] = crcDat[0];
|
dat[dat.length - 1] = crcDat[1];
|
return dat;
|
}
|
|
/**
|
* 获取数据帧数据
|
*
|
* @param file 文件
|
* @return 数据帧数组
|
* @throws IOException
|
*/
|
public static byte[][] getData(File file) throws IOException {
|
long len = file.length();
|
int size = (int) Math.ceil(len / 1024d);
|
int size1 = (int) Math.ceil(len / 1024d);
|
double v = len % 1024d;
|
if (v==0){
|
int i = size + 1;
|
size1=i;
|
}
|
//获取数据帧个数
|
byte[][] result = new byte[size][1029];
|
FileInputStream fis = new FileInputStream(file);
|
for (int i = 0; i < size1-1 ; i++) { //生成数据帧
|
result[i][0] = STX; //1024字节数据包头
|
result[i][1] = (byte) (num++); //自动填充帧序号
|
if (num > 255) { //帧序号最大255
|
num = 0;
|
}
|
result[i][2] = (byte) ~result[i][1]; //取反帧序号
|
fis.read(result[i], 3, 1024);
|
byte[] crc16 = CRC16.getCRC16(result[i], 3); //获取crc16校验
|
result[i][result[i].length - 2] = crc16[0];
|
result[i][result[i].length - 1] = crc16[1]; //填充crc16校验
|
}
|
int endLen = (int) len % 1024;
|
//如果最后一帧不是整1024个字节
|
if (endLen > 0) {
|
//如果最后一帧长度小于等于128
|
if (endLen <= 128) {
|
//最后一帧使用128字节传输
|
result[size - 1] = new byte[133];
|
result[size - 1][0] = SOH; //128字节数据包头
|
int n = fis.read(result[size - 1], 3, 128); //填充128个字节数据
|
for (int i = n + 3; i < 131; i++) { //剩余位填充0x1A
|
result[size - 1][i] = 0x1A;
|
}
|
} else {
|
//最后一帧使用1024字节传输
|
result[size - 1][0] = STX; //1024字节数据包头
|
int n = fis.read(result[size - 1], 3, 1024); //填充1024个数据
|
for (int i = n + 3; i < 1027; i++) { //剩余位填充0x1A
|
result[size - 1][i] = 0x1A;
|
}
|
}
|
result[size - 1][1] = (byte) (num++); //自动填充帧序号
|
if (num > 255) { //帧序号最大255
|
num = 0;
|
}
|
result[size - 1][2] = (byte) ~result[size - 1][1]; //取反帧序号
|
byte[] crc16 = CRC16.getCRC16(result[size - 1], 3); //获取crc16校验
|
result[size - 1][result[size - 1].length - 2] = crc16[0];
|
result[size - 1][result[size - 1].length - 1] = crc16[1]; //填充crc16校验
|
}
|
fis.close();
|
return result;
|
}
|
|
|
|
public static int getNum() {
|
return num;
|
}
|
|
public static void setNum(int num) {
|
getFilesDataBytes.num = num;
|
}
|
}
|