Java Code Examples for java.io.RandomAccessFile#readInt()

The following examples show how to use java.io.RandomAccessFile#readInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FSImage.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
public boolean isConversionNeeded(StorageDirectory sd) throws IOException {
  File oldImageDir = new File(sd.getRoot(), "image");
  if (!oldImageDir.exists()) {
    if(sd.getVersionFile().exists())
      throw new InconsistentFSStateException(sd.getRoot(),
          oldImageDir + " does not exist.");
    return false;
  }
  // check the layout version inside the image file
  File oldF = new File(oldImageDir, "fsimage");
  RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
  try {
    oldFile.seek(0);
    int odlVersion = oldFile.readInt();
    if (odlVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
      return false;
  } finally {
    oldFile.close();
  }
  return true;
}
 
Example 2
Source File: FileDeltaCollection.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that a file has a valid deltas header, adding the header if the
 * file is shorter than the header.
 */
private static void setOrCheckFileHeader(RandomAccessFile file) throws IOException {
  Preconditions.checkNotNull(file);
  file.seek(0);

  if (file.length() < FILE_HEADER_LENGTH) {
    // The file is new. Insert a header.
    file.write(FILE_MAGIC_BYTES);
    file.writeInt(FILE_PROTOCOL_VERSION);
  } else {
    byte[] magic = new byte[4];
    file.readFully(magic);
    if (!Arrays.equals(FILE_MAGIC_BYTES, magic)) {
      throw new IOException("Delta file magic bytes are incorrect");
    }

    int version = file.readInt();
    if (version != FILE_PROTOCOL_VERSION) {
      throw new IOException(String.format("File protocol version mismatch - expected %d got %d",
          FILE_PROTOCOL_VERSION, version));
    }
  }
}
 
Example 3
Source File: SpillableMapUtils.java    From hudi with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the given file with specific pattern(|crc|timestamp|sizeOfKey|SizeOfValue|key|value|) then
 * returns an instance of {@link FileEntry}.
 */
private static FileEntry readInternal(RandomAccessFile file, long valuePosition, int valueLength) throws IOException {
  file.seek(valuePosition);
  long crc = file.readLong();
  long timestamp = file.readLong();
  int keySize = file.readInt();
  int valueSize = file.readInt();
  byte[] key = new byte[keySize];
  file.readFully(key, 0, keySize);
  byte[] value = new byte[valueSize];
  if (valueSize != valueLength) {
    throw new HoodieCorruptedDataException("unequal size of payload written to external file, data may be corrupted");
  }
  file.readFully(value, 0, valueSize);
  long crcOfReadValue = generateChecksum(value);
  if (crc != crcOfReadValue) {
    throw new HoodieCorruptedDataException(
        "checksum of payload written to external disk does not match, data may be corrupted");
  }
  return new FileEntry(crc, keySize, valueSize, key, value, timestamp);
}
 
Example 4
Source File: DataStorage.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPreUpgradableLayout(StorageDirectory sd) throws IOException {
  File oldF = new File(sd.getRoot(), "storage");
  if (!oldF.exists())
    return false;
  // check the layout version inside the storage file
  // Lock and Read old storage file
  RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
  FileLock oldLock = oldFile.getChannel().tryLock();
  try {
    oldFile.seek(0);
    int oldVersion = oldFile.readInt();
    if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
      return false;
  } finally {
    oldLock.release();
    oldFile.close();
  }
  return true;
}
 
Example 5
Source File: RegionLoader.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void loadLocationTable() throws IOException {
    RandomAccessFile raf = this.getRandomAccessFile();
    raf.seek(0);
    this.lastSector = 1;
    int[] data = new int[1024 * 2]; //1024 records * 2 times
    for (int i = 0; i < 1024 * 2; i++) {
        data[i] = raf.readInt();
    }
    for (int i = 0; i < 1024; ++i) {
        int index = data[i];
        this.locationTable.put(i, new Integer[]{index >> 8, index & 0xff, data[1024 + i]});
        int value = this.locationTable.get(i)[0] + this.locationTable.get(i)[1] - 1;
        if (value > this.lastSector) {
            this.lastSector = value;
        }
    }
}
 
Example 6
Source File: FrameBodyASPI.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public void read(final RandomAccessFile file) throws IOException, InvalidTagException {
    final int size = readHeader(file);
    if (size == 0) {
        throw new InvalidTagException("Empty Frame");
    }
    dataStart = file.readInt();
    dataLength = file.readInt();
    indexPoints = (int) file.readShort();
    bitsPerPoint = (int) file.readByte();
    fraction = new short[indexPoints];
    for (int i = 0; i < indexPoints; i++) {
        if (bitsPerPoint == 8) {
            fraction[i] = (short) file.readByte();
        } else if (bitsPerPoint == 16) {
            fraction[i] = file.readShort();
        } else {
            throw new InvalidTagException("ASPI bits per point wasn't 8 or 16");
        }
    }
}
 
Example 7
Source File: SpillMap.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void pageIn() {
    if (!pagedIn) {
        RandomAccessFile file = spillFile.getPage(pageIndex);
        try {
        int numElements = file.readInt();
        for (int i = 0; i < numElements; i++) {
            int kvSize = file.readInt();
            byte[] data = new byte[kvSize];
            file.readFully(data);
            pageMap.put(SpillManager.getKey(data), data);
            totalResultSize += (data.length + Bytes.SIZEOF_INT);
        }
        } catch (IOException ioe) {
            // Error during key access on spilled resource
            // TODO rework error handling
            throw new RuntimeException(ioe);
        }
        pagedIn = true;
        dirtyPage = false;
    }
}
 
Example 8
Source File: RegionLoader.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void loadLocationTable() throws IOException {
    RandomAccessFile raf = this.getRandomAccessFile();
    raf.seek(0);
    this.lastSector = 1;
    int[] data = new int[1024 * 2]; //1024 records * 2 times
    for (int i = 0; i < 1024 * 2; i++) {
        data[i] = raf.readInt();
    }
    for (int i = 0; i < 1024; ++i) {
        int index = data[i];
        this.locationTable.put(i, new Integer[]{index >> 8, index & 0xff, data[1024 + i]});
        int value = this.locationTable.get(i)[0] + this.locationTable.get(i)[1] - 1;
        if (value > this.lastSector) {
            this.lastSector = value;
        }
    }
}
 
Example 9
Source File: ZipUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code true} if the provided file contains a ZIP64 End of Central Directory
 * Locator.
 *
 * @param zipEndOfCentralDirectoryPosition offset of the ZIP End of Central Directory record
 *        in the file.
 *
 * @throws IOException if an I/O error occurs while reading the file.
 */
public static final boolean isZip64EndOfCentralDirectoryLocatorPresent(
        RandomAccessFile zip, long zipEndOfCentralDirectoryPosition) throws IOException {

    // ZIP64 End of Central Directory Locator immediately precedes the ZIP End of Central
    // Directory Record.
    long locatorPosition = zipEndOfCentralDirectoryPosition - ZIP64_EOCD_LOCATOR_SIZE;
    if (locatorPosition < 0) {
        return false;
    }

    zip.seek(locatorPosition);
    // RandomAccessFile.readInt assumes big-endian byte order, but ZIP format uses
    // little-endian.
    return zip.readInt() == ZIP64_EOCD_LOCATOR_SIG_REVERSE_BYTE_ORDER;
}
 
Example 10
Source File: NNStorage.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override // Storage
public boolean isPreUpgradableLayout(StorageDirectory sd) throws IOException {
  if (disablePreUpgradableLayoutCheck) {
    return false;
  }

  File oldImageDir = new File(sd.getRoot(), "image");
  if (!oldImageDir.exists()) {
    return false;
  }
  // check the layout version inside the image file
  File oldF = new File(oldImageDir, "fsimage");
  RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
  try {
    oldFile.seek(0);
    int oldVersion = oldFile.readInt();
    oldFile.close();
    oldFile = null;
    if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
      return false;
  } finally {
    IOUtils.cleanup(LOG, oldFile);
  }
  return true;
}
 
Example 11
Source File: RandomAccessData.java    From cs601 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args)  throws IOException {
	RandomAccessFile f = new RandomAccessFile("/tmp/junk.data", "rw");
	f.writeInt(34);
	f.writeDouble(3.14159);
	f.seek(0);  // rewind and read again
	int i = f.readInt();
	double d = f.readDouble();
	f.close();
	System.out.printf("read %d and %1.5f\n", i, d);
}
 
Example 12
Source File: GeneItem.java    From megan-ce with GNU General Public License v3.0 5 votes vote down vote up
/**
 * read
 *
 * @param ins
 * @throws IOException
 */
public void read(RandomAccessFile ins) throws IOException {
    int length = ins.readInt();
    if (length == 0)
        proteinId = null;
    else {
        proteinId = new byte[length];
        if (ins.read(proteinId, 0, length) != length)
            throw new IOException("read failed");
    }
    for (int i = 0; i < creator.numberOfClassifications(); i++) {
        ids[i] = ins.readInt();
    }
    reverse = (ins.read() == 1);
}
 
Example 13
Source File: XMLToXTalk.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
static private int updateElement(RandomAccessFile raf, ArrayList counts, int index)
        throws IOException {
  skipString(raf);
  int skipCount = raf.readInt();
  // Debug.p("Skip count: " + skipCount);
  for (int i = 0; i < skipCount; i++) {
    skipString(raf);
    skipString(raf);
  }
  int childCount = ((StackEntry) counts.get(index)).childCount;
  index++;
  raf.writeInt(childCount);
  // Debug.p("Wrote: " + childCount);
  for (int i = 0; i < childCount; i++) {
    int marker = raf.read();
    switch ((byte) marker) {
      case XTalkTransporter.ELEMENT_MARKER:
        index = updateElement(raf, counts, index);
        break;
      case XTalkTransporter.STRING_MARKER:
        skipString(raf);
        break;
      default:
        throw new IOException("Unexepcted marker: " + marker);
    }
  }
  return index;
}
 
Example 14
Source File: MM5DataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Read sub header
 *
 * @param br The randomAccessFile
 * @param isSequential If if sequential
 * @return The sub header
 * @throws IOException
 */
public SubHeader readSubHeader(RandomAccessFile br, boolean isSequential) throws IOException {
    SubHeader sh = new SubHeader();
    byte[] bytes = new byte[4];
    int i;
    if (isSequential) {
        br.skipBytes(4);
    }

    sh.ndim = br.readInt();
    for (i = 0; i < 4; i++) {
        sh.start_index[i] = br.readInt();
    }
    for (i = 0; i < 4; i++) {
        sh.end_index[i] = br.readInt();
    }
    sh.xtime = br.readFloat();
    br.read(bytes);
    sh.staggering = new String(bytes).trim();
    br.read(bytes);
    sh.ordering = new String(bytes).trim();
    bytes = new byte[24];
    br.read(bytes);
    sh.current_date = new String(bytes).trim();
    bytes = new byte[9];
    br.read(bytes);
    sh.name = new String(bytes).trim();
    bytes = new byte[25];
    br.read(bytes);
    sh.unit = new String(bytes).trim();
    bytes = new byte[46];
    br.read(bytes);
    sh.description = new String(bytes).trim();

    if (isSequential) {
        br.skipBytes(4);
    }

    return sh;
}
 
Example 15
Source File: ZipUtil.java    From sbt-android-protify with Apache License 2.0 5 votes vote down vote up
static CentralDirectory findCentralDirectory(RandomAccessFile raf) throws IOException,
        ZipException {
    long scanOffset = raf.length() - ENDHDR;
    if (scanOffset < 0) {
        throw new ZipException("File too short to be a zip file: " + raf.length());
    }

    long stopOffset = scanOffset - 0x10000 /* ".ZIP file comment"'s max length */;
    if (stopOffset < 0) {
        stopOffset = 0;
    }

    int endSig = Integer.reverseBytes(ENDSIG);
    while (true) {
        raf.seek(scanOffset);
        if (raf.readInt() == endSig) {
            break;
        }

        scanOffset--;
        if (scanOffset < stopOffset) {
            throw new ZipException("End Of Central Directory signature not found");
        }
    }
    // Read the End Of Central Directory. ENDHDR includes the signature
    // bytes,
    // which we've already read.

    // Pull out the information we need.
    raf.skipBytes(2); // diskNumber
    raf.skipBytes(2); // diskWithCentralDir
    raf.skipBytes(2); // numEntries
    raf.skipBytes(2); // totalNumEntries
    CentralDirectory dir = new CentralDirectory();
    dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    return dir;
}
 
Example 16
Source File: GEMFFile.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
private void readHeader() throws IOException {
	final RandomAccessFile baseFile = mFiles.get(0);

	// Get file sizes
	for (final RandomAccessFile file : mFiles) {
		mFileSizes.add(file.length());
	}

	// Version
	final int version = baseFile.readInt();
	if (version != VERSION) {
		throw new IOException("Bad file version: " + version);
	}

	// Tile Size
	final int tile_size = baseFile.readInt();
	if (tile_size != TILE_SIZE) {
		throw new IOException("Bad tile size: " + tile_size);
	}

	// Read Source List
	final int sourceCount = baseFile.readInt();

	for (int i=0;i<sourceCount;i++) {
		final int sourceIndex = baseFile.readInt();
		final int sourceNameLength = baseFile.readInt();
		final byte[] nameData = new byte[sourceNameLength];
		baseFile.read(nameData, 0, sourceNameLength);

		final String sourceName = new String(nameData);
		mSources.put(new Integer(sourceIndex), sourceName);
	}

	// Read Ranges
	final int num_ranges = baseFile.readInt();
	for (int i=0;i<num_ranges;i++) {
		final GEMFRange rs = new GEMFRange();
		rs.zoom = baseFile.readInt();
		rs.xMin = baseFile.readInt();
		rs.xMax = baseFile.readInt();
		rs.yMin = baseFile.readInt();
		rs.yMax = baseFile.readInt();
		rs.sourceIndex = baseFile.readInt();
		rs.offset = baseFile.readLong();
		mRangeData.add(rs);
	}
}
 
Example 17
Source File: HALogReader.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
    * <strong>CAUTION: This constructor should not be used in circumstances in
    * which the {@link HALogWriter} is active since this constructor can not
    * differentiate atomically between the live HALog and a historical HALog
    * and will always provide a read-only view, even if the live HALog file is
    * opened.</strong>
    * 
    * @param file
    *            The HALog file.
    *            
    * @throws IOException
    */
public HALogReader(final File file) throws IOException {

	m_file = file;

	m_raf = new RandomAccessFile(file, "r");

	m_channel = m_raf.getChannel();

	try {
		/**
		 * Must determine whether the file has consistent open and committed
		 * rootBlocks, using the commitCounter to determine which rootBlock
		 * is which.
		 * 
		 * Note: Both root block should exist (they are both written on
		 * startup). If they are identical, then the log is empty (the
		 * closing root block has not been written and the data in the log
		 * is useless).
		 * 
		 * We figure out which root block is the opening root block based on
		 * standard logic.
		 */
		/*
		 * Read the MAGIC and VERSION.
		 */
		m_raf.seek(0L);
		try {
			/*
			 * Note: this next line will throw IOException if there is a
			 * file lock contention.
			 */
			magic = m_raf.readInt();
		} catch (IOException ex) {
               throw new RuntimeException(
                       "Can not read magic. Is file locked by another process? file="
                               + file, ex);
		}
           if (magic != HALogWriter.MAGIC)
               throw new RuntimeException("Bad HALog magic: file=" + file
                       + ", expected=" + HALogWriter.MAGIC + ", actual="
                       + magic);
		version = m_raf.readInt();
		if (version != HALogWriter.VERSION1)
               throw new RuntimeException("Bad HALog version: file=" + file
                       + ", expected=" + HALogWriter.VERSION1 + ", actual="
                       + version);

		final RootBlockUtility tmp = new RootBlockUtility(reopener, file,
				true/* validateChecksum */, false/* alternateRootBlock */,
				false/* ignoreBadRootBlock */);

		m_closeRootBlock = tmp.chooseRootBlock();

		m_openRootBlock = tmp.rootBlock0 == m_closeRootBlock ? tmp.rootBlock1
				: tmp.rootBlock0;

		final long cc0 = m_openRootBlock.getCommitCounter();

		final long cc1 = m_closeRootBlock.getCommitCounter();

		if ((cc0 + 1) != cc1 && (cc0 != cc1)) {
			/*
			 * Counters are inconsistent with either an empty log file or a
			 * single transaction scope.
			 */
               throw new IllegalStateException(
                       "Incompatible rootblocks: file=" + file + ", cc0="
                               + cc0 + ", cc1=" + cc1);
		}

		m_channel.position(HALogWriter.headerSize0);

		m_storeType = m_openRootBlock.getStoreType();

	} catch (Throwable t) {

		close();

		throw new RuntimeException(t);

	}

}
 
Example 18
Source File: LogFile.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
protected static void skipRecord(RandomAccessFile fileHandle,
  int offset) throws IOException {
  fileHandle.seek(offset);
  int length = fileHandle.readInt();
  fileHandle.skipBytes(length);
}
 
Example 19
Source File: TestEditLog.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Test
public void testEditChecksum() throws Exception {
  // start a cluster 
  Configuration conf = new HdfsConfiguration();
  MiniDFSCluster cluster = null;
  FileSystem fileSys = null;
  cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
  cluster.waitActive();
  fileSys = cluster.getFileSystem();
  final FSNamesystem namesystem = cluster.getNamesystem();

  FSImage fsimage = namesystem.getFSImage();
  final FSEditLog editLog = fsimage.getEditLog();
  fileSys.mkdirs(new Path("/tmp"));

  Iterator<StorageDirectory> iter = fsimage.getStorage().
    dirIterator(NameNodeDirType.EDITS);
  LinkedList<StorageDirectory> sds = new LinkedList<StorageDirectory>();
  while (iter.hasNext()) {
    sds.add(iter.next());
  }
  editLog.close();
  cluster.shutdown();

  for (StorageDirectory sd : sds) {
    File editFile = NNStorage.getFinalizedEditsFile(sd, 1, 3);
    assertTrue(editFile.exists());

    long fileLen = editFile.length();
    LOG.debug("Corrupting Log File: " + editFile + " len: " + fileLen);
    RandomAccessFile rwf = new RandomAccessFile(editFile, "rw");
    rwf.seek(fileLen-4); // seek to checksum bytes
    int b = rwf.readInt();
    rwf.seek(fileLen-4);
    rwf.writeInt(b+1);
    rwf.close();
  }
  
  try {
    cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).format(false).build();
    fail("should not be able to start");
  } catch (IOException e) {
    // expected
    assertNotNull("Cause of exception should be ChecksumException", e.getCause());
    assertEquals("Cause of exception should be ChecksumException",
        ChecksumException.class, e.getCause().getClass());
  }
}
 
Example 20
Source File: SMF.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reads a MIDI track without doing anything to the data
 */
private void skipATrack( RandomAccessFile dos) throws IOException{
               if(VERBOSE) System.out.println("Skipping the tempo track . . .");
	dos.readInt();
	dos.skipBytes(dos.readInt());
}