Question

I've to write a programm which reads an ecg recording file and writes the data into variables.

All I've at the moment is the inputstream and a bytebuffer.

I know from the documentation, that the first 2 bytes should represent a checksum of unint16,The next following 4 bytes should give a magic number in hex and so on...

But if I execute the code beneath it's wont work because the output for the number is 0.

My question is if I had done something wrong in the buffer section. It's strange because If I write the whole stream into an array and then point to the position of the 3 - 6 element the out put will be :

output += String.format("0x%02X", bC[2]); and then i read it upside down i get the magic number.

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;
}

}

Was it helpful?

Solution

Your first problem is the use of available(). All it does is tell you how much data is available to be read without blocking, which is rarely of much interest, and there is a specific warning in the Javadoc against treating it as the length of the entire input. What you're really interested in is how much data you need.

Fortunately there is an easy solution. Wrap the input stream in a DataInputStream, and read it as follows:

short crc16 = in.readShort();
// check the CRC
String hexString = "0x"+(Integer.toString(in.readInt(), 16));
// ...

etc. See the Javadoc.

skip(0) does nothing in your code, as that's where you already are when you open the file.

I don't know what you mean by 'read it upside down'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top