Example #1
0
  /**
   * This is the entry point to the whole file version function set. It takes the filename as a
   * parameter and returns the version. If the version can't be returned for whatever reason, an
   * IOException is thrown.
   *
   * @param filename The filename to get the version of.
   * @return The version of the file, as an integer.
   * @throws IOException If the version can't be retrieved for some reason.
   */
  public static int getVersion(String filename, boolean byteorder) throws IOException {
    /* psStart is the pointer to the first byte in the PE data.  The byte is pointed to at 0x3c */
    int peStart;
    /* The signature of the pe file, PE\0\0 */
    int peSignature;
    /* The number of sections (.data, .text, .rsrc, etc) */
    short numberOfSections;
    /* A pointer to the "optional" header (which will always be present in .exe files */
    int ptrOptionalHeader;

    /* The file we're reading from.*/
    MappedByteBuffer file =
        new FileInputStream(filename)
            .getChannel()
            .map(FileChannel.MapMode.READ_ONLY, 0, new File(filename).length());
    /* Set the file ordering to little endian */
    file.order(ByteOrder.LITTLE_ENDIAN);

    /* The start of the PE is pointed at by the 0x3c'th byte */
    peStart = file.getInt(PE_START);

    /* The first 4 bytes are the signature */
    peSignature = file.getInt(peStart + 0);

    /* Verify that it's a valid pe file.  IF not, throw an exception */
    if (peSignature != 0x00004550) throw new IOException("Invalid PE file!");

    /* The number of sections is the short starting at the 6th byte */
    numberOfSections = file.getShort(peStart + 6);

    /* Get a pointer to the optional header */
    ptrOptionalHeader = peStart + 24;

    return processOptionalHeader(file, ptrOptionalHeader, numberOfSections, byteorder);
  }
 MemoryMappedFileBuffer(final File file) {
   try {
     // Open the file stream.
     final FileInputStream fileStream = new FileInputStream(file);
     final FileChannel fileChannel = fileStream.getChannel();
     mFileBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0L, fileChannel.size());
     mFileBuffer.order(ByteOrder.LITTLE_ENDIAN);
     fileChannel.close();
     fileStream.close();
   } catch (final IOException exc) {
     throw new RuntimeIOException(exc.getMessage(), exc);
   }
 }
 // returns byteoffset of first IFD
 private long readHeader() throws IOException {
   MappedByteBuffer buffer = fileChannel_.map(FileChannel.MapMode.READ_ONLY, 0, 8);
   short zeroOne = buffer.getShort();
   if (zeroOne == 0x4949) {
     bigEndian_ = false;
   } else if (zeroOne == 0x4d4d) {
     bigEndian_ = true;
   } else {
     throw new IOException("Error reading Tiff header");
   }
   buffer.order(bigEndian_ ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
   short twoThree = buffer.getShort();
   if (twoThree != 42) {
     throw new IOException("Tiff identifier code incorrect");
   }
   return unsignInt(buffer.getInt());
 }
Example #4
0
  /**
   * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
   *
   * @param s 地点子串
   * @return 包含IPEntry类型的List
   */
  public List<IPEntry> getIPEntries(String s) {
    List<IPEntry> ret = new ArrayList<IPEntry>();
    try {
      // 映射IP信息文件到内存中
      if (mbb == null) {
        FileChannel fc = ipFile.getChannel();
        mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFile.length());
        mbb.order(ByteOrder.LITTLE_ENDIAN);
      }

      int endOffset = (int) ipEnd;
      for (int offset = (int) ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
        int temp = readInt3(offset);
        if (temp != -1) {
          IPLocation ipLoc = getIPLocation(temp);
          // 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
          if (ipLoc.getCountry().indexOf(s) != -1 || ipLoc.getArea().indexOf(s) != -1) {
            IPEntry entry = new IPEntry();
            entry.country = ipLoc.getCountry();
            entry.area = ipLoc.getArea();
            // 得到起始IP
            readIP(offset - 4, b4);
            entry.beginIp = Util.getIpStringFromBytes(b4);
            // 得到结束IP
            readIP(temp, b4);
            entry.endIp = Util.getIpStringFromBytes(b4);
            // 添加该记录
            ret.add(entry);
          }
        }
      }
    } catch (IOException e) {
      LogFactory.log("", Level.ERROR, e);
    }
    return ret;
  }
Example #5
0
  /**
   * Loads geodata from a file. When diagonal strategy is enabled, precalculates diagonal flags for
   * whole file. When file does not exist, is corrupted or not consistent, loads none geodata.
   *
   * @param regionX : Geodata file region X coordinate.
   * @param regionY : Geodata file region Y coordinate.
   * @return boolean : True, when geodata file was loaded without problem.
   */
  private final boolean loadGeoBlocks(int regionX, int regionY) {
    final String filename = String.format(Config.GEODATA_FORMAT.getFilename(), regionX, regionY);

    // standard load
    try (RandomAccessFile raf = new RandomAccessFile(Config.GEODATA_PATH + filename, "r");
        FileChannel fc = raf.getChannel()) {
      // initialize file buffer
      MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
      buffer.order(ByteOrder.LITTLE_ENDIAN);

      // load 18B header for L2off geodata (1st and 2nd byte...region X and Y)
      if (Config.GEODATA_FORMAT == GeoFormat.L2OFF) {
        for (int i = 0; i < 18; i++) buffer.get();
      }

      // get block indexes
      final int blockX = (regionX - L2World.TILE_X_MIN) * GeoStructure.REGION_BLOCKS_X;
      final int blockY = (regionY - L2World.TILE_Y_MIN) * GeoStructure.REGION_BLOCKS_Y;

      // loop over region blocks
      for (int ix = 0; ix < GeoStructure.REGION_BLOCKS_X; ix++) {
        for (int iy = 0; iy < GeoStructure.REGION_BLOCKS_Y; iy++) {
          if (Config.GEODATA_FORMAT != GeoFormat.L2OFF) {
            // get block type
            final byte type = buffer.get();

            // load block according to block type
            switch (type) {
              case GeoStructure.TYPE_FLAT_L2J_L2OFF:
              case GeoStructure.TYPE_FLAT_L2D:
                _blocks[blockX + ix][blockY + iy] = loadFlatBlock(buffer);
                break;

              case GeoStructure.TYPE_COMPLEX_L2J:
              case GeoStructure.TYPE_COMPLEX_L2D:
                _blocks[blockX + ix][blockY + iy] = loadComplexBlock(buffer);
                break;

              case GeoStructure.TYPE_MULTILAYER_L2J:
              case GeoStructure.TYPE_MULTILAYER_L2D:
                _blocks[blockX + ix][blockY + iy] = loadMultilayerBlock(buffer);
                break;

              default:
                throw new IllegalArgumentException("Unknown block type: " + type);
            }
          } else {
            // get block type
            final short type = buffer.getShort();

            // load block according to block type
            switch (type) {
              case GeoStructure.TYPE_FLAT_L2J_L2OFF:
                _blocks[blockX + ix][blockY + iy] = loadFlatBlock(buffer);
                break;

              case GeoStructure.TYPE_COMPLEX_L2OFF:
                _blocks[blockX + ix][blockY + iy] = loadComplexBlock(buffer);
                break;

              default:
                _blocks[blockX + ix][blockY + iy] = loadMultilayerBlock(buffer);
                break;
            }
          }
        }
      }

      // check data consistency
      if (buffer.remaining() > 0)
        _log.warning(
            "GeoDriverArray: Region file "
                + filename
                + " can be corrupted, remaining "
                + buffer.remaining()
                + " bytes to read.");

      // loading was successful
      return true;
    } catch (Exception e) {
      // an error occured while loading, load null blocks
      _log.warning("GeoDriverArray: Error while loading " + filename + " region file.");
      _log.warning(e.getMessage());

      // replace whole region file with null blocks
      loadNullBlocks(regionX, regionY);

      // loading was not successful
      return false;
    }
  }
 private MappedByteBuffer makeBuffer(FileChannel.MapMode mode, long byteOffset, long numBytes)
     throws IOException {
   MappedByteBuffer buffer = fileChannel_.map(mode, byteOffset, numBytes);
   buffer.order(bigEndian_ ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
   return buffer;
 }