pcidescriptors.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 121 行
JAVA
121 行
/*
* $Id: PCIDescriptors.java,v 1.1 2003/11/25 11:41:21 epr Exp $
*/
package org.jnode.driver.pci;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import org.apache.log4j.Logger;
/**
* @author epr
*/
public class PCIDescriptors {
/** My logger */
private final Logger log = Logger.getLogger(getClass());
private final HashMap vendors;
private static final PCIDescriptors instance = new PCIDescriptors();
private PCIDescriptors() {
try {
vendors = readDevices();
} catch (IOException ex) {
throw new ExceptionInInitializerError(ex);
}
}
/**
* Gets my single instance
*/
public static PCIDescriptors getInstance() {
return instance;
}
/**
* Gets the descriptor of the vendor with the given ID.
* @param vendorId
*/
public VendorDescriptor findVendor(int vendorId) {
VendorDescriptor result;
result = (VendorDescriptor)vendors.get(new Integer(vendorId));
if (result == null) {
result = new VendorDescriptor(vendorId, "? (" + vendorId + ")");
}
return result;
}
private HashMap readDevices()
throws IOException {
log.debug("Loading PCI device info");
InputStream is = ClassLoader.getSystemResourceAsStream("org/jnode/driver/pci/pci.ids");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
HashMap vendors = new HashMap();
VendorDescriptor lastVendor = null;
DeviceDescriptor lastDevice = null;
String line;
while ((line = in.readLine()) != null) {
line = stripComment(line);
if (line.length() > 0) {
int tabs = countTabs(line);
line = line.trim();
if (tabs == 0) {
// Vendor ID
lastVendor = parseVendor(line);
vendors.put(new Integer(lastVendor.getId()), lastVendor);
} else if (tabs == 1) {
// Device
lastDevice = parseDevice(line);
lastVendor.addDevice(lastDevice);
} else {
// Subclass
}
}
}
return vendors;
}
private VendorDescriptor parseVendor(String line) {
int idx = line.indexOf(' ');
int id = Integer.parseInt(line.substring(0, idx), 16);
return new VendorDescriptor(id, line.substring(idx+1).trim());
}
private DeviceDescriptor parseDevice(String line) {
int idx = line.indexOf(' ');
int id = Integer.parseInt(line.substring(0, idx), 16);
return new DeviceDescriptor(id, line.substring(idx+1).trim());
}
private String stripComment(String line) {
int idx = line.indexOf('#');
if (idx >= 0) {
line = line.substring(0, idx);
}
return line;
}
private int countTabs(String line) {
final int len = line.length();
int count = 0;
for (int i = 0; i < len; i++) {
if (line.charAt(i) == '\t') {
count++;
} else {
return count;
}
}
return count;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?