Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2012 Agrosense info@agrosense.eu Licensed under the Eclipse Public License - v 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 /*
  * 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