ibmpartitiontable.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 142 行

JAVA
142
字号
/*
 * $Id: IBMPartitionTable.java,v 1.3 2004/01/25 00:28:41 gbin Exp $
 */
package org.jnode.fs.partitions.ibm;

import java.io.IOException;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.driver.ide.IDEConstants;
import org.jnode.fs.partitions.PartitionTable;
import org.jnode.fs.partitions.PartitionTableEntry;

/**
 * @author epr
 */
public class IBMPartitionTable implements PartitionTable {
	
	/** The bootsector data */
	//private final byte[] bootSector;
	/** The partition entries */
	private final IBMPartitionTableEntry[] partitions;
	
	/** The device */
	private final Device drivedDevice;
	
	/** Extended partition */
	private final Vector extendedPartitions = new Vector();
	
	/** My logger */
	private final Logger log = Logger.getLogger(getClass());
	
	/** The position of the extendedPartition in the table */ 
	private int extendedPartitionEntry = -1;
	
	/**
	 * Create a new instance
	 * @param bootSector
	 */
	public IBMPartitionTable(byte[] bootSector, Device device) {
		//this.bootSector = bootSector;
		this.partitions = new IBMPartitionTableEntry[4];
		this.drivedDevice = device;
		if(containsPartitionTable(bootSector)) {
			for (int partNr = 0; partNr < 4;  partNr++) {
				log.debug("try part "+ partNr);
				partitions[partNr] = new IBMPartitionTableEntry(bootSector, partNr);
				if(partitions[partNr].isExtended()) {
					extendedPartitionEntry = partNr;
					log.debug("Found Extended partitions");
					handleExtended(partitions[partNr]);
				}
			}
		}
	}
	
	/**
	 * Fill the extended Table
	 *  
	 */
	private void handleExtended(IBMPartitionTableEntry current) {
		
		final long startLBA  = current.getStartLba();
		final byte[] sector = new byte[IDEConstants.SECTOR_SIZE];
		try {
			log.debug("Try to read the Extended Partition Table");
			BlockDeviceAPI api = (BlockDeviceAPI)drivedDevice.getAPI(BlockDeviceAPI.class);
			api.read(startLBA * IDEConstants.SECTOR_SIZE, sector, 0 ,IDEConstants.SECTOR_SIZE);
		} catch (ApiNotFoundException e) {
			// I think we ca'nt get it 
			log.error("API Not Found Exception");
		} catch (IOException e) {
			// I think we ca'nt get it
			log.error("IOException");
		}
		IBMPartitionTableEntry entry = null;
		for(int i = 0; i < 4 ; i++) {
			entry = new IBMPartitionTableEntry(sector, i);
			if(entry.isValid() && !entry.isEmpty()) {
				//corrct the offset
				
				//log.debug(entry.dump());
				if(entry.isExtended()) {
					entry.setStartLba(entry.getStartLba() + partitions[extendedPartitionEntry].getStartLba());
					//log.debug("going recurse :"+i);
					handleExtended(entry);
				}else {
					//log.debug("adding extended :"+i);
					entry.setStartLba(entry.getStartLba() + current.getStartLba());
					extendedPartitions.add(entry);
				}
			}else {
				//log.debug("Entry is not valid or empty:");
				//log.debug(entry.dump());
			}
		}
	}
	
	public boolean hasExtended() {
		return !extendedPartitions.isEmpty();
	}
	
	/**
	 * Does the given bootsector contain an IBM partition table?
	 * @param bootSector
	 */
	public static boolean containsPartitionTable(byte[] bootSector) {
		if ((bootSector[510] & 0xFF) != 0x55) {
			return false;
		}
		if ((bootSector[511] & 0xFF) != 0xAA) {
			return false;
		}
		return true;
	}
	
	/**
	 * Gets the number of partitions in this table.
	 */
	public int getLength() {
		return partitions.length; 
	}
	
	/**
	 * Gets a single entry.
	 * @param partNr
	 */
	public PartitionTableEntry getEntry(int partNr) {
		return partitions[partNr];
	}
	
	/**
	 * @return Returns the extendedPartitions.
	 */
	public Vector getExtendedPartitions() {
		return extendedPartitions;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?