我必须编写一个程序来读取ecg
记录文件并将数据写入变量。
我现在只有inputstream
and a bytebuffer
。
我从文档中知道,前 2bytes
应该代表checksum
unint16 ,接下来的 4bytes
应该以十六进制给出一个幻数,依此类推......
但是,如果我执行下面的代码,它将无法工作,因为数字的输出是 0。
我的问题是我是否在缓冲区部分做错了什么。这很奇怪,因为如果我将整个流写入一个数组,然后指向 3 - 6 元素的位置,则输出将是:
output += String.format("0x%02X", bC[2]);
然后我把它倒过来读我得到了神奇的数字。
public class Stream {
private String fileName;
private byte[] storageArray;
private int byteLength;
private byte[] magicNumber;
public Stream(String fileName) {
this.fileName = fileName;
try {
readIt();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readIt() throws IOException {
FileInputStream fileIn = new FileInputStream("ecg-file");
//for skipping to the desired beginning of the byte stream
fileIn.skip(0);
setByteLength(fileIn.available());
storageArray = new byte[getByteLength()];
fileIn.read(getStorageArray());
fileIn.close();
}
public String getCRCNumber () {
ByteBuffer twoByte = ByteBuffer.wrap(getStorageArray());
twoByte.order(ByteOrder.LITTLE_ENDIAN);
//the missing bytes @ the beginning for the int
twoByte.put((byte)0x00);
twoByte.put((byte)0x00);
//shift the start position per 2 bytes
// and read the first 2 bytes of the inputstream into the buffer
twoByte.position(0x02);
twoByte.put(getStorageArray(), 0, 2);
twoByte.flip();
//creates the int number of the 4 bytes in the buffer
int result = twoByte.getInt();
String output = "";
String b = "\n";
return output += Integer.toString(result);
}
public int getByteLength() {
return byteLength;
}
public void setByteLength(int byteLength) {
this.byteLength = byteLength;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getMagicNumber() {
return magicNumber;
}
public void setMagicNumber(byte[] magicNumber) {
this.magicNumber = magicNumber;
}
public byte[] getStorageArray() {
return storageArray;
}
public void setStorageArray(byte[] storageArray) {
this.storageArray = storageArray;
}
}