Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */
 package nl.cloudfarming.client.isobus.model;
 
 import java.io.*;
Class that reads binary files Reads LITTLE endian files

Author(s):
Gerben Feenstra & Merijn Zengers
public class BinaryReader {
    private InputStream inputStream = null;
        this. = inputStream;
    }

    
Method that reads from the binairyFile given to the constructor Reads the number of bytes specified in XmlField.BinaryDataType Will reverse the bytes to get Big endian

Parameters:
field The field to read
Returns:
value that has been red
    public long read(XmlField fieldthrows IOException {
        long value = 0;
        BinaryDataType datatype = field.getDataType();
        byte[] bytes = new byte[datatype.getSize()];
        .read(bytes);
        Collections.reverse(Arrays.asList(bytes));
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        DataInputStream dis = new DataInputStream(bais);
        if (dis.available() == datatype.getSize()) {
            switch (datatype) {
                case :
                    value = dis.readByte();
                    break;
                case :
                    value = dis.readUnsignedByte();
                    if (value < 0) {
                        value *= -1;
                        // Last bit: 128 = 2^7
                        value += (long) Math.pow(2, 7);
                    }
                    break;
                case :
                    Short signed16temp = dis.readShort();
                    value = (long) Short.reverseBytes(signed16temp);
                    break;
                case :
                    Short unsigned16temp = dis.readShort();
                    value = (long) Short.reverseBytes(unsigned16temp);
                    if (value < 0) {
                        value *= -1;
                        // Last bit: 32768 = 2^15
                        value += (long) Math.pow(2, 15);
                    }
                    break;
                case :
                    Integer signed32temp = dis.readInt();
                    value = (long) Integer.reverseBytes(signed32temp);
                    break;
                case :
                    Integer unsigned32temp = dis.readInt();
                    value = (long) Integer.reverseBytes(unsigned32temp);
                    if (value < 0) {
                        value *= -1; // Make positive
                        // Last bit: 2147483648 = 2^31
                        value += (long) Math.pow(2, 31);
                    }
                    break;
            }
        }
        return value;
    }

    

Returns:
True if the inputstream != null and has still bits available.
Throws:
java.io.IOException
    public boolean available() throws IOException {
        return  != null && .available() > 0;
    }
New to GrepCode? Check out our FAQ X