📄 treeblockdevice.java
字号:
int sum; for (int j = 2; j < FAT.length/4; j++) { //convert FAT entry to an int sum = 0; for (int k = 0; k<4; k++) { sum += (int) ((FAT[4*j + k] & 0xFF) << k*8); } //check it isn't empty if (sum != 0) { hashFAT.put(new Integer(j), new Integer(sum)); } } readToWrite(2, root); } } } //method to read a directory or file from this TBD and write it out to a new physical one private void readToWrite(int startingCluster, File file) //make this return an int to signify success { int cluster = startingCluster; byte[] buffer = new byte[512]; if (file.isDirectory()) { byte[] acluster = new byte[512*8]; byte[] dir = new byte[0]; //read directory into byte[] while (true) { //read cluster of dir sector by sector and store in array for (int i=0; i<8; i++) { if (read(relativeSectors + reservedSectors + 2*sectorsPerFAT + (cluster-2)*8 + i, buffer, 1) == 0) { System.arraycopy(buffer, 0, acluster, i*512, 512); } } byte[] newdir = new byte[dir.length + acluster.length]; System.arraycopy(dir, 0, newdir, 0, dir.length); System.arraycopy(acluster, 0, newdir, dir.length, acluster.length); dir = newdir; //check for more clusters cluster = ((Integer) hashFAT.get(new Integer(cluster))).intValue(); if (cluster == 268435455)//251592447) //endmark break; } //for each directory entry create a new file/dir and call this method on it with its start cluster int newStartCluster; String name; String ext; boolean isDirectory; for (int k = 0; k < dir.length/32; k++) { if ((dir[32*k]==0) || dir[32*k +11] == 0xF) continue; name = ""; ext = ""; for(int m =0; m<8;m++) {name += (char) dir[32*k + m];} for(int m =0; m<3;m++) {ext += (char) dir[32*k + 8 + m];} name = name.trim(); ext = ext.trim(); newStartCluster = (dir[32*k + 26] & 0xFF) + ((dir[32*k + 27] & 0xFF) << 8) + ((dir[32*k + 20] & 0xFF) << 16) + ((dir[32*k + 21] & 0xFF) << 24); isDirectory = (dir[32*k + 11] & 0x10) == 0x10; //add in other attributes here like readonly, hidden etc. File next; if (isDirectory) { next = new File(file.getPath(), name + ext); next.mkdir(); } else { next = new File(file.getPath(), name + "." + ext); try { next.createNewFile(); } catch (Exception e) { System.out.println("IO Exception - Can't create new file"); e.printStackTrace(); } } readToWrite(newStartCluster, next); } } if (file.isFile()) { //open stream to write to file try { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); while (true) { //read cluster of file sector by sector and write to disk int data = 0; for (int i=0; i<8; i++) { for (int h = 0; h < 512; h++) { buffer[h] = (byte) 0x00;} if (read(relativeSectors + reservedSectors + 2*sectorsPerFAT + (cluster-2)*8 + i, buffer, 1) == 0) { //if the first byte is 0 check if it is a blank sector if (buffer[0] == 0) for ( int j = 0; j<512; j++) { if (buffer[j]==0) data += 1; } //if it is a blank sector don't write it if (data == 512) break; //if sector ends in a 0 and we are in the last cluster, then trim the zeroes from the end if ((((Integer) hashFAT.get(new Integer(cluster))).intValue() == 268435455) && (buffer[511] == 0)) { int index = 511; while (buffer[index]==0) { index--; if (index == -1) break; } out.write(buffer,0,index+1); break; } out.write(buffer); } } //check for more clusters cluster = ((Integer) hashFAT.get(new Integer(cluster))).intValue(); if (cluster == 268435455) //EOF marker break; } out.close(); } catch (IOException e) { System.out.println("IO Exception in writing new directory tree"); e.printStackTrace(); } } } //write image of disk private void writeImage(DataOutput dout) throws IOException { byte[] buffer=new byte[512]; long i = 0; while (read(i, buffer, 1) != -1) { dout.write(buffer); i++; } } //***********************************************************************************// //build directory structure private MyDir buildTree(File root, long startcluster) throws IOException { if (!root.exists()) return null; if (!root.isDirectory()) return null;//change this to work for a single file eventually String path = root.getPath(); String name = root.getName(); MyDir myDir = new MyDir(path,name,startcluster); long subClusters; String[] files = root.list(); //figure out size of directory entry int size =0; int hiddenFiles = 1; for (int i=0; i<files.length; i++) { String longext="",longname=""; if(files[i].lastIndexOf(".") != -1) { longname = files[i].substring(0, files[i].lastIndexOf(".")); longext = files[i].substring(files[i].lastIndexOf(".")+1); if (longname.length() == 0) longname = "~" + hiddenFiles; hiddenFiles++; } else { longname = files[i]; longext = ""; } if ((longname.length() < 9) && (longext.length()<4)) size += 1; else { if (longext.length() == 0) size += (int) 2+(longname.length()-1)/13; else size += (int) 2+(longname.length()+longext.length())/13; } } //set cluster size of this directory myDir.setSize(size+2); // + 2 for the . and .. entries myDir.setSizeSectors((long) (size+2)*32/bytesPerSector+1); subClusters=0; for (int i=0; i<files.length;i++) { File fnew = new File(path, files[i]); if (!fnew.isFile()) continue; MyFile file = new MyFile(path, files[i], myDir.getStartCluster() + myDir.getSizeClusters() + subClusters); //generate cluster list file.makeClusterList(); myDir.addFile(file); subClusters += file.getSizeClusters(); //add entry to FAT map FATMap.put(new Long(file.getStartCluster()), file); } myDir.setdirSubClusters(myDir.getdirSubClusters() + subClusters); for (int i=0; i<files.length;i++) { File fnew = new File(path,files[i]); if (!fnew.isDirectory()) continue; File fnewdir=new File(path,files[i]); MyDir m = buildTree(fnewdir, myDir.getStartCluster() + myDir.getSizeClusters() + subClusters); //the cool call subClusters+=m.getSizeClusters()+m.getdirSubClusters(); if (m!= null) myDir.addDir(m); } myDir.setdirSubClusters(myDir.getdirSubClusters() + subClusters); myDir.makedirEntry(); //generate cluster list myDir.makeClusterList(); //add entry to FAT map FATMap.put(new Long(myDir.getStartCluster()), myDir); return myDir; } private abstract class MyFATEntry { private String path, name; private long startCluster, sizeSectors, sizeClusters; private File thisFile; public Map clusterList = new HashMap(); // list of clusters in object private long lastSector; MyFATEntry(File file, long startCluster) { thisFile = file; this.path = file.getPath(); this.name = file.getName(); this.startCluster = startCluster; } abstract int getSector(long sectorNumber, byte[] buffer) throws IOException; public void setSizeSectors(long i) { sizeSectors = i; sizeClusters = (long) (sizeSectors-1)/sectorsPerCluster+1; } public void makeClusterList() { long cluster = getStartCluster(); for (long counter = 0; counter< getSizeClusters(); counter++ ) { clusterList.put(new Long(cluster), new Long(counter)); cluster++; } setLastSector(getSizeSectors() + (getStartCluster()-2)*sectorsPerCluster + 95 + 2*sectorsPerFAT); } public void setLastSector(long sector) { this.lastSector = sector; } public long getLastSector() { return lastSector; } public long getStartCluster() { return startCluster; } long getSizeSectors() { return sizeSectors; } long getSizeClusters() { return sizeClusters; } public void setSizeClusters(long length) { this.sizeClusters = length; } public File getFile() { return thisFile; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -