📄 treeblockdevice.java
字号:
public void setFile(File file) { thisFile = file; } String getName() { return name; } String getPath() { return path; } } //**************************// //File Class private class MyFile extends MyFATEntry { private RandomAccessFile thisRAF; private long fileSize; MyFile(String path, String name, long start) throws IOException { super(new File(path, name), start); //Get rid of this!! numberOfOpenFiles++; if (numberOfOpenFiles % 100==0) System.out.println("Opened " + numberOfOpenFiles + " files."); //check if too many files have been loaded if (numberOfOpenFiles > maxNumberOfFiles) throw new IndexOutOfBoundsException("Too many files loaded: try a directory with fewer files."); fileSize = getFile().length(); setSizeSectors((long) fileSize/512+1); } public int getSector(long sectorNumber, byte[] buffer) throws IOException { try { long oddsectors = (sectorNumber-95-2*sectorsPerFAT) % sectorsPerCluster; long offset = ((Long) clusterList.get(new Long(clusterNumber(sectorNumber)))).longValue()*sectorsPerCluster*512; offset = offset + oddsectors*512; //see if file is already open boolean openAlready = false; if (thisRAF != null) { for (int i = 0; i< openFiles.length; i++) { if (openFiles[i] != null) if (thisRAF == openFiles[i].RAF()) { openAlready = true; //update read counter fileReads++; openFiles[i].setReads(fileReads); break; } } } if (!openAlready) { Arrays.sort(openFiles, new rafComparator()); //if 10 or more files have been opened, then close the last file if (openFiles[openFiles.length-1] != null) { openFiles[openFiles.length-1].RAF().close(); } //open the new file and add it to the open list thisRAF = new RandomAccessFile(getFile(), "r"); openFiles[openFiles.length-1] = new rafReads(fileReads, thisRAF); fileReads++; } thisRAF.seek(offset); int len = Math.min(512, (int) (thisRAF.length() - offset)); thisRAF.readFully(buffer, 0, len); return 0; } catch (Exception e) { e.printStackTrace(); return -1; } } public void setfileSize(long size) { this.fileSize = size; } public long getfileSize() {return fileSize;} } //*********************// //Directory Class public class MyDir extends MyFATEntry { // All files in this directory: private Vector files = new Vector(); // All directories in this directory: private Vector dirs = new Vector(); private long dirSubClusters; private Vector shortnames = new Vector(); private byte[] dirEntry = new byte[0]; private byte[] lfn = new byte[0]; private int size; public MyDir(String path, String name, long startCluster) { super(new File(path), startCluster); this.dirSubClusters=0; } public byte[] getdirEntry() { return dirEntry; } //get set of directory entries for this directory public int getSector(long sectorNumber, byte[] buffer) { long oddsectors = (sectorNumber-95-2*sectorsPerFAT) % sectorsPerCluster; long offset = ((Long) clusterList.get(new Long(clusterNumber(sectorNumber)))).longValue()*sectorsPerCluster*512; offset = offset + oddsectors*512; // long offset = (sectorNumber - 95-2*sectorsPerFAT -(super.getStartCluster()-2)*sectorsPerCluster) * 512; int len; if ( size*32 - offset > 512) len = 512; else len = (int) (size*32 - offset); for (int i=0; i<512; i++) buffer[i] = 0x00; System.arraycopy(dirEntry, (int) offset, buffer, 0, len); //needs some error catching return 0; } public void setSize(int i) {this.size=i;} public void setdirSubClusters(long i) {dirSubClusters = i;} public long getdirSubClusters() {return dirSubClusters;} public void addFile(MyFile f) { files.addElement(f); } public void addDir(MyDir d) { dirs.addElement(d); } public void changeDirEntry(byte[] buffer, long sectorOffset, long sectorNumber) { //update clusterlist clusterList.put(new Long(clusterNumber(sectorNumber)), new Long(sectorOffset/sectorsPerCluster)); if (dirEntry.length < 512 + sectorOffset*512) { byte[] temp = new byte[(int) (sectorOffset+1)*512]; System.arraycopy(dirEntry, 0, temp, 0, dirEntry.length); dirEntry = temp; } size = dirEntry.length/32; System.arraycopy(buffer, 0, dirEntry, (int) sectorOffset*bytesPerSector, 512); } public Vector getFiles() { return files; } public Vector getDirs() { return dirs; } public void makedirEntry() { byte[] middle; //add . and .. entries to direntry byte[] dotdot = new byte[64]; dotdot[0]=(byte) 0x2E; for (int i = 1; i<11; i++) dotdot[i]=(byte) 0x20; dotdot[11]=(byte) 0x10; dotdot[32] = (byte) 0x2E; dotdot[33] = (byte) 0x2E; for (int i = 34; i<43; i++) dotdot[i]=(byte) 0x20; dotdot[43]=(byte) 0x10; this.dirEntry = dotdot; for (int i=0; i<dirs.size();i++) { middle = makeDirEntry((MyDir) dirs.get(i)); byte[] temp = new byte[this.dirEntry.length + middle.length]; System.arraycopy(this.dirEntry, 0, temp, 0, this.dirEntry.length); System.arraycopy(middle, 0, temp, this.dirEntry.length, middle.length); this.dirEntry = temp; } for (int i=0; i<files.size();i++) { middle = makeDirEntry((MyFile) files.get(i)); byte[] temp = new byte[this.dirEntry.length + middle.length]; System.arraycopy(this.dirEntry, 0, temp, 0, this.dirEntry.length); System.arraycopy(middle, 0, temp, this.dirEntry.length, middle.length); this.dirEntry = temp; } } private byte[] makeDirEntry(MyFile file) { byte[] entry = new byte[32]; for (int i=0;i<32;i++) entry[i]= (byte) 0x00; byte[] fullEntry; String filename = file.getName(); String name = filename; String ext= " "; if (filename.lastIndexOf(".") != -1) { name = filename.substring(0,filename.lastIndexOf(".")); ext = filename.substring(filename.lastIndexOf(".")+1); while (ext.length() < 3) ext = ext+" "; } //put filename into file entry if ((name.length()<9) && (ext.length()<4)) { //add shortname to shortnames list shortnames.add(name); //put dir name into dir entry System.arraycopy(name.toUpperCase().getBytes(),0,entry,0,name.length()); //put in blank space after filename if it is less than 8 characters for (int i =name.length(); i< 8;i++) entry[i]=(byte) 0x20; //put in extension System.arraycopy(ext.toUpperCase().getBytes(),0,entry,8,ext.length()); //pad ext with spaces for (int i =ext.length(); i< 3;i++) entry[8+i]=(byte) 0x20; } else { String shortname = makeFullLFNDirEntry(name, this.lfn, ext); System.arraycopy(shortname.toUpperCase().getBytes(), 0, entry, 0, 8); System.arraycopy(ext.toUpperCase().getBytes(), 0, entry, 8, 3); } entry[11]=(byte) 0x20; //Attrib Byte if(file.getFile().isHidden()) entry[11]+=(byte) 0x02; //hidden if(!file.getFile().canWrite()) entry[11]+=(byte) 0x01;//read only //put in starting cluster (high 2 bytes) int startCluster = (int) file.getStartCluster(); entry[20] = (byte) (startCluster >>> 16); entry[21] = (byte) (startCluster >>> 24); //put in starting cluster (low 2 bytes) entry[26] = (byte) startCluster; entry[27] = (byte) (startCluster >>> 8); //put in file size in bytes int fileSize = (int) file.getfileSize(); entry[28] = (byte) fileSize; entry[29] = (byte) (fileSize >>> 8); entry[30] = (byte) (fileSize >>> 16); entry[31] = (byte) (fileSize >>> 24); //time and date stuff GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis(file.getFile().lastModified()); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); int hour24 = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); int time = ((int) second/2) + (minute << 5) + (hour24 << 11); int date = day+ (month << 5) + ((year -1980) << 9); entry[22] = (byte) time; entry[23] = (byte) (time >>> 8); entry[24] = (byte) date; entry[25] = (byte) (date >>> 8); entry[16] = entry[24]; entry[17] = entry[25]; entry[18] = entry[24]; entry[19] = entry[25]; entry[14] = entry[22]; entry[15] = entry[23]; fullEntry = new byte[this.lfn.length + 32]; System.arraycopy(this.lfn, 0, fullEntry, 0, this.lfn.length); System.arraycopy(entry, 0, fullEntry, this.lfn.length, 32); this.lfn=new byte[0]; return fullEntry; } private byte[] makeLFNDirEntry(byte[] unicodeName, int checksum, int ordinal, boolean last) { int length = unicodeName.length; byte[] entry = new byte[32]; for (int i=0;i<32;i++) entry[i]= (byte) 0x00; entry[11]=0xF; byte ordinalb; if (!last) ordinalb = (byte) ordinal; else ordinalb = (byt
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -