コード例 #1
0
  /**
   * Constructs a DBFHeader, read the data from file <br>
   * You need to supply an open file handle to read from
   *
   * @param ff open file handle for read access
   */
  DBFHeader(RandomAccessFile ff) throws tinySQLException {
    try {
      ff.seek(FLAG_INDEX);
      file_type = Utils.fixByte(ff.readByte());

      // get the last update date
      file_update_year = Utils.fixByte(ff.readByte());
      file_update_month = Utils.fixByte(ff.readByte());
      file_update_day = Utils.fixByte(ff.readByte());

      // a byte array to hold little-endian long data
      //
      byte[] b = new byte[4];

      // read that baby in...
      //
      ff.readFully(b);

      // convert the byte array into a long (really a double)
      // 4-7 number of records
      numRecords = (int) Utils.vax_to_long(b);

      // a byte array to hold little-endian short data
      //
      b = new byte[2];

      // get the data position (where it starts in the file)
      // 8-9 Length of header
      ff.readFully(b);
      headerLength = Utils.vax_to_short(b);

      // find out the length of the data portion
      // 10-11 Length of Record
      ff.readFully(b);
      recordLength = Utils.vax_to_short(b);

      // calculate the number of fields
      //
      numFields = (headerLength - 33) / 32;

      // skip the next 20 bytes - looks like this is not needed...
      // ff.skipBytes(20);
      // 12-31 reserved

      Utils.log("HEADER=" + this.toString());

    } catch (Exception e) {
      throw new tinySQLException(e.getMessage());
    }
  }
コード例 #2
0
ファイル: Page.java プロジェクト: code4/ProjectPika
 public void loadMetaData() throws IOException {
   pageFile.seek(offset);
   nextPageId = pageFile.readInt();
   currentFill = pageFile.readInt();
   bloomfilter = pageFile.readInt();
   type = pageFile.readByte();
 }
コード例 #3
0
 private int peek() throws IOException {
   if (checkPos(this.pointer)) {
     buf.seek(pointer++);
     return b2i(buf.readByte());
   } else {
     return -1;
   }
 }
コード例 #4
0
  /*
   * gets an (uncompressed) stream representing the chunk data returns null if
   * the chunk is not found or an error occurs
   */
  public synchronized DataInputStream getChunkDataInputStream(int x, int z) {
    if (outOfBounds(x, z)) {
      debugln("READ", x, z, "out of bounds");
      return null;
    }

    try {
      int offset = getOffset(x, z);
      if (offset == 0) {
        // debugln("READ", x, z, "miss");
        return null;
      }

      int sectorNumber = offset >> 8;
      int numSectors = offset & 0xFF;

      if (sectorNumber + numSectors > sectorFree.size()) {
        debugln("READ", x, z, "invalid sector");
        return null;
      }

      file.seek(sectorNumber * SECTOR_BYTES);
      int length = file.readInt();

      if (length > SECTOR_BYTES * numSectors) {
        debugln("READ", x, z, "invalid length: " + length + " > 4096 * " + numSectors);
        return null;
      }

      byte version = file.readByte();
      if (version == VERSION_GZIP) {
        byte[] data = new byte[length - 1];
        file.read(data);
        DataInputStream ret =
            new DataInputStream(
                new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))));
        // debug("READ", x, z, " = found");
        return ret;
      } else if (version == VERSION_DEFLATE) {
        byte[] data = new byte[length - 1];
        file.read(data);
        DataInputStream ret =
            new DataInputStream(
                new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(data))));
        // debug("READ", x, z, " = found");
        return ret;
      }

      debugln("READ", x, z, "unknown version " + version);
      return null;
    } catch (IOException e) {
      debugln("READ", x, z, "exception");
      return null;
    }
  }
コード例 #5
0
  public static long checksumRandomAccessFile(Path filename) throws IOException {
    try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r")) {
      long length = file.length();
      CRC32 crc = new CRC32();

      for (long p = 0; p < length; p++) {
        file.seek(p);
        int c = file.readByte();
        crc.update(c);
      }
      return crc.getValue();
    }
  }
コード例 #6
0
  public int read(byte[] b) throws IOException {

    if (buf == null) throw new IOException("Data buffer not initialized.");

    if (pointer < 0 || pointer >= length) return -1;

    int length = this.length - (int) pointer;
    if (length > b.length) length = b.length;

    for (int i = 0; i < length; i++) {
      buf.seek(pointer++);
      b[i] = buf.readByte();
    }
    return length;
  }
コード例 #7
0
  public PSTFile(File fileName) throws FileNotFoundException, PSTException, IOException {
    // attempt to open the file.
    in = new RandomAccessFile(fileName, "r");

    // get the first 4 bytes, should be !BDN
    try {
      byte[] temp = new byte[4];
      in.read(temp);
      String strValue = new String(temp);
      if (!strValue.equals("!BDN")) {
        throw new PSTException("Invalid file header: " + strValue + ", expected: !BDN");
      }

      // make sure we are using a supported version of a PST...
      byte[] fileTypeBytes = new byte[2];
      in.seek(10);
      in.read(fileTypeBytes);
      // ANSI file types can be 14 or 15:
      if (fileTypeBytes[0] == PSTFile.PST_TYPE_ANSI_2) {
        fileTypeBytes[0] = PSTFile.PST_TYPE_ANSI;
      }
      if (fileTypeBytes[0] != PSTFile.PST_TYPE_ANSI
          && fileTypeBytes[0] != PSTFile.PST_TYPE_UNICODE) {
        throw new PSTException("Unrecognised PST File version: " + fileTypeBytes[0]);
      }
      this.pstFileType = fileTypeBytes[0];

      // make sure encryption is turned off at this stage...
      if (this.getPSTFileType() == PST_TYPE_ANSI) {
        in.seek(461);
      } else {
        in.seek(513);
      }
      encryptionType = in.readByte();
      if (encryptionType == 0x02) {
        throw new PSTException(
            "Only unencrypted and compressable PST files are supported at this time");
      }

      // build out name to id map.
      processNameToIdMap(in);

    } catch (IOException err) {
      throw new PSTException("Unable to read PST Sig", err);
    }
  }
コード例 #8
0
  // see DbFile.java for javadocs
  public Page readPage(PageId pid) {
    // some code goes here
    if (pid.pageNumber() >= numPages()) {
      throw new IllegalArgumentException("page not in file");
    }
    Page returnme = null;

    byte[] data = HeapPage.createEmptyPageData();
    long offset = (long) BufferPool.PAGE_SIZE * pid.pageNumber();
    try {
      raf.seek(offset);
      for (int i = 0; i < data.length; i++) {
        data[i] = raf.readByte();
      }
      returnme = new HeapPage((HeapPageId) pid, data);
    } catch (EOFException eofe) {
      eofe.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
    return returnme;
  }
コード例 #9
0
ファイル: WorldManager.java プロジェクト: Joooo/MyWorlds
  /**
   * Repairs the chunk region file Returns -1 if the file had to be removed Returns -2 if we had no
   * access Returns -3 if file removal failed (from -1) Returns the amount of changed chunks
   * otherwise
   *
   * @param chunkfile
   * @param backupfolder
   * @return
   */
  public static int repairRegion(File chunkfile, File backupfolder) {
    MyWorlds.log(Level.INFO, "Performing repairs on region file: " + chunkfile.getName());
    RandomAccessFile raf = null;
    try {
      raf = new RandomAccessFile(chunkfile, "rw");
      File backupfile = new File(backupfolder + File.separator + chunkfile.getName());
      int[] locations = new int[1024];
      for (int i = 0; i < 1024; i++) {
        locations[i] = raf.readInt();
      }
      // Validate the data
      int editcount = 0;
      for (int i = 0; i < locations.length; i++) {
        int location = locations[i];
        if (location == 0) continue;
        try {
          int offset = location >> 8;
          int size = location & 255;
          raf.seek((long) (offset * 4096));
          int length = raf.readInt();
          // Read and test the data
          if (length > 4096 * size) {
            editcount++;
            locations[i] = 0;
            MyWorlds.log(Level.WARNING, "Invalid length: " + length + " > 4096 * " + size);
            // Invalid length
          } else if (size > 0 && length > 0) {
            byte version = raf.readByte();
            byte[] data = new byte[length - 1];
            raf.read(data);
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            // Try to load it all...
            DataInputStream stream;
            if (version == 1) {
              stream = new DataInputStream(new GZIPInputStream(bais));
            } else if (version == 2) {
              stream = new DataInputStream(new InflaterInputStream(bais));
            } else {
              stream = null;
              // Unknown version
              MyWorlds.log(
                  Level.WARNING,
                  "Unknown region version: " + version + " (we probably need an update here!)");
            }
            if (stream != null) {
              // Validate the stream and close
              try {
                NBTBase base = NBTTagCompound.b((DataInput) stream);
                if (base == null) {
                  editcount++;
                  locations[i] = 0;
                  MyWorlds.log(Level.WARNING, "Invalid tag compount at chunk " + i);
                } else if (!(base instanceof NBTTagCompound)) {
                  editcount++;
                  locations[i] = 0;
                  MyWorlds.log(Level.WARNING, "Invalid tag compount at chunk " + i);
                }
              } catch (Exception ex) {
                // Invalid.
                editcount++;
                locations[i] = 0;
                MyWorlds.log(Level.WARNING, "Stream  " + i);
                ex.printStackTrace();
              }
              stream.close();
            }
          }
        } catch (Exception ex) {
          editcount++;
          locations[i] = 0;
          ex.printStackTrace();
        }
      }
      if (editcount > 0) {
        if (backupfolder.mkdirs() && copy(chunkfile, backupfile)) {
          // Write out the new locations
          raf.seek(0);
          for (int location : locations) {
            raf.writeInt(location);
          }
        } else {
          MyWorlds.log(Level.WARNING, "Failed to make a copy of the file, no changes are made.");
          return -2;
        }
      }
      // Done.
      raf.close();
      return editcount;
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      if (raf != null) raf.close();
    } catch (Exception ex) {
    }

    try {
      chunkfile.delete();
      return -1;
    } catch (Exception ex) {
      return -3;
    }
  }