Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package fiftyone.mobile.detection.trie;
  
  import java.io.File;
  import java.nio.ByteOrder;
Reader used to create a provider from data structured in a decision tree format.

Author(s):
51Degrees.mobi
Version:
2.2.8.9
 
 public class Reader {
    
    
Creates a logger for this class
 
     private static final Logger _logger = LoggerFactory.getLogger(Reader.class);

    
Creates a new provider from the trie data file supplied.

Parameters:
dataFile Trie data file used to create the provider.
Returns:
A new provider initialised with data from the file provided or null if the file is unreadable.
 
     public static Provider create(String dataFilethrows FileNotFoundExceptionIOException {
         File fileInfo = new File(dataFile);
         if (fileInfo.exists()) {
             try {
                 // Create a new Random Access file, in readonly mode
                 RandomAccessFile data = new RandomAccessFile(dataFile"r");
                 return create(data);
             } catch (IOException ex) {
                 .error("Exception reading data file: "ex);
                 throw ex;
             }
         } else {
             .error(String.format("Could not find trie data file \"%s\"."dataFile));
             throw new FileNotFoundException();
         }
     }

    
Creates a new provider from the Randomly accessed Tri data file.

Parameters:
data File data used to create Provider.
Returns:
A new Provider initialised with data from the file provided.
Throws:
java.io.IOException
 
     private static Provider create(RandomAccessFile datathrows IOException {
 
         FileChannel reader = data.getChannel();
 
         // Skip over the version number.
         reader.position(reader.position() + 2);
 
         // Skip over the copyright notice.
         ByteBuffer temp = ByteBuffer.allocate(4);
         temp.order(.);
         reader.read(temp);
         long copy = readUInt(temp.array());
 
         reader.position(reader.position() + copy);
 
         // Create the new provider.
         return new Provider(
                 // Read Strings
                 read(reader),
                 // Read Properties
                 read(reader),
                 // Read Devices
                 read(reader),
                 // Read Lookup Lists
                 read(reader),
                 // File Channel
                 reader);
     }

    
Determines how many bytes to read from a FileChannel into a ByteBuffer then reads them.

Parameters:
reader Files channel to read from.
Returns:
Populated ByteBuffer.
Throws:
java.io.IOException
 
     private static ByteBuffer read(FileChannel readerthrows IOException {
        ByteBuffer temp = ByteBuffer.allocate(4);
        reader.read(temp);
        ByteBuffer data = readFully((intreadUInt(temp.array()), reader);
        if (data == null) {
            ..println("data buffer is null");
        }
        return data;
    }

    
Crates an unsigned integer from an array of 4 byte values and returns the result as a long.

Parameters:
value values to use.
Returns:
value
    private static long readUInt(byte[] value) {
        long l = 0;
        l |= (value[3] & 0xFF);
        l <<= 8;
        l |= (value[2] & 0xFF);
        l <<= 8;
        l |= (value[1] & 0xFF);
        l <<= 8;
        l |= (value[0] & 0xFF);
        if (l > (long) 2 * .) {
            .error("readUint: value is greater then unsigned integer.");
        }
        return l;
    }

    
Reads a set amount of bytes from a FileChannel into a ByteBuffer.

Parameters:
amount Amount to read.
reader File channel to read from.
Returns:
Bytes Read from the file channel.
Throws:
java.io.IOException
    private static ByteBuffer readFully(final int amountFileChannel readerthrows IOException {
        // Get start position
        long start = reader.position();
        // Final Buffer to return
        ByteBuffer data = ByteBuffer.allocate(amount);
        data.order(.);
        int read = reader.read(data);
        if (read != amount) {
            .error("ByteBuffer not read fully");
            throw new IOException("ByteBuffer not read fully");
        }
        reader.position(start + (longamount);
        return data;
    }
New to GrepCode? Check out our FAQ X