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

The following examples show how to use java.io.RandomAccessFile#readShort() . 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: 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 2
Source File: BlockManager.java    From nyzoVerifier with The Unlicense 5 votes vote down vote up
private static Block loadBlockFromIndividualFile(long blockHeight) {

        Block block = null;
        File file = individualFileForBlockHeight(blockHeight);
        if (file.exists()) {
            try {
                RandomAccessFile blockFileReader = new RandomAccessFile(file, "r");
                int numberOfBlocks = blockFileReader.readShort();
                block = Block.fromFile(blockFileReader);
                blockFileReader.close();
            } catch (Exception ignored) { }
        }

        return block;
    }
 
Example 3
Source File: Paged.java    From btree4j with Apache License 2.0 5 votes vote down vote up
protected void read(RandomAccessFile raf) throws IOException {
    this._fhSize = raf.readShort();
    this._pageSize = raf.readInt();
    this._totalPageCount = raf.readLong();
    this._firstFreePage = raf.readLong();
    this._lastFreePage = raf.readLong();
    this._pageHeaderSize = raf.readByte();
}
 
Example 4
Source File: PersistentMap.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new PersistentMap.
 * @param rootFolder root folder to read/write files to
 * @param version    tells the version of the data stored in the persistent map; if it does not equal to the version of the persistent file, it will be cleared automatically
 * @throws IOException if the persistent map could not be initialized
 */
public PersistentMap( final File rootFolder, final short version ) throws IOException {
	indexFile = new RandomAccessFile( new File( rootFolder, "index" ), "rw" );
	if ( indexFile.getChannel().tryLock() == null )
		throw new IOException( "Index file is already in use!" );
	
	dataFile = new RandomAccessFile( new File( rootFolder, "data"  ), "rw" );
	if ( dataFile.getChannel().tryLock() == null )
		throw new IOException( "Data file is already in use!" );
	
	this.version = version;
	final long indexSize = indexFile.length();
	if ( indexSize == 0 || indexFile.readShort() != version ) // New file or old version?
		clear();
	else {
		// Read the index file into memory
		String    key;
		ValueInfo valueInfo;
		while ( indexFile.getFilePointer() < indexSize ) {
			key = indexFile.readUTF();
			valueInfo = new ValueInfo();
			valueInfo.position = indexFile.readInt();
			valueInfo.size     = indexFile.readInt();
			indexMap.put( key, valueInfo );
		}
	}
}
 
Example 5
Source File: TTFParser.java    From Android_Skin_2.0 with Apache License 2.0 4 votes vote down vote up
private void parseInner(RandomAccessFile randomAccessFile) throws IOException {
	int majorVersion = randomAccessFile.readShort();
	int minorVersion = randomAccessFile.readShort();
	int numOfTables = randomAccessFile.readShort();
	if (majorVersion != 1 || minorVersion != 0) {
		return;
	}
	// jump to TableDirectory struct
	randomAccessFile.seek(12);
	boolean found = false;
	byte[] buff = new byte[4];
	TableDirectory tableDirectory = new TableDirectory();
	for (int i = 0; i < numOfTables; i++) {
		randomAccessFile.read(buff);
		tableDirectory.name = new String(buff);
		tableDirectory.checkSum = randomAccessFile.readInt();
		tableDirectory.offset = randomAccessFile.readInt();
		tableDirectory.length = randomAccessFile.readInt();
		if ("name".equalsIgnoreCase(tableDirectory.name)) {
			found = true;
			break;
		} else if (tableDirectory.name == null || tableDirectory.name.length() == 0) {
			break;
		}
	}
	// not found table of name
	if (!found) {
		return;
	}
	randomAccessFile.seek(tableDirectory.offset);
	NameTableHeader nameTableHeader = new NameTableHeader();
	nameTableHeader.fSelector = randomAccessFile.readShort();
	nameTableHeader.nRCount = randomAccessFile.readShort();
	nameTableHeader.storageOffset = randomAccessFile.readShort();
	NameRecord nameRecord = new NameRecord();
	for (int i = 0; i < nameTableHeader.nRCount; i++) {
		nameRecord.platformID = randomAccessFile.readShort();
		nameRecord.encodingID = randomAccessFile.readShort();
		nameRecord.languageID = randomAccessFile.readShort();
		nameRecord.nameID = randomAccessFile.readShort();
		nameRecord.stringLength = randomAccessFile.readShort();
		nameRecord.stringOffset = randomAccessFile.readShort();
		long pos = randomAccessFile.getFilePointer();
		byte[] bf = new byte[nameRecord.stringLength];
		long vpos = tableDirectory.offset + nameRecord.stringOffset + nameTableHeader.storageOffset;
		randomAccessFile.seek(vpos);
		randomAccessFile.read(bf);
		String temp = new String(bf, "utf-16");//new String(bf, Charset.forName("utf-16"));
		fontProperties.put(nameRecord.nameID, temp);
		randomAccessFile.seek(pos);
	}
}