📄 harddrivestorage.java
字号:
// check first whether file name is valid or not File obj = null; if (fileName == null || fileName.length() == 0) { System.out.println(name_ + ".getFile(): Warning - invalid " + "file name."); return obj; } Iterator it = fileList_.iterator(); int size = 0; int index = 0; boolean found = false; File tempFile = null; // find the file in the disk while (it.hasNext()) { tempFile = (File) it.next(); size += tempFile.getSize(); if (tempFile.getName().equals(fileName) == true) { found = true; obj = tempFile; break; } index++; } // if the file is found, then determine the time taken to get it if (found == true) { obj = (File) fileList_.get(index); double seekTime = getSeekTime(size); double transferTime = getTransferTime(obj.getSize()); // total time for this operation obj.setTransactionTime(seekTime + transferTime); } return obj; } /** * Gets the list of file names located on this storage. * @return a LinkedList of file names */ public List getFileNameList() { return nameList_; } /** * Get the seek time for a file with the defined size. Given a file size in * MB, this method returns a seek time for the file in seconds. * * @param fileSize the size of a file in MB * @return the seek time in seconds */ private double getSeekTime(int fileSize) { double result = 0; if (gen_ != null) { result += gen_.sample(); } if (fileSize > 0 && capacity_ != 0) { result += ((double) fileSize / capacity_); } return result; } /** * Gets the transfer time of a given file * @param fileSize the size of the transferred file * @return the transfer time in seconds */ private double getTransferTime(int fileSize) { double result = 0; if (fileSize > 0 && capacity_ != 0) { result = ((double) fileSize * maxTransferRate_) / capacity_; } return result; } /** * Check if the file is valid or not. This method checks whether the given * file or the file name of the file is valid. The method name parameter is * used for debugging purposes, to output in which method an error has * occured. * * @param file the file to be checked for validity * @param methodName the name of the method in which we check for * validity of the file * @return <tt>true</tt> if the file is valid, <tt>false</tt> otherwise */ private boolean isFileValid(File file, String methodName) { if (file == null) { System.out.println(name_ + "." + methodName + ": Warning - the given file is null."); return false; } String fileName = file.getName(); if (fileName == null || fileName.length() == 0) { System.out.println(name_ + "." + methodName + ": Warning - invalid file name."); return false; } return true; } /** * Adds a file to the storage. First, the method checks if there is enough * space on the storage, then it checks if the file with the same name is * already taken to avoid duplicate filenames. <br> * The time taken (in seconds) for adding the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param file the file to be added * @return the time taken (in seconds) for adding the specified file */ public double addFile(File file) { double result = 0.0; // check if the file is valid or not if (isFileValid(file, "addFile()") == false) { return result; } // check the capacity if (file.getSize() + currentSize_ > capacity_) { System.out.println(name_ + ".addFile(): Warning - not enough space" + " to store " + file.getName()); return result; } // check if the same file name is alredy taken if (contains(file.getName()) == false) { double seekTime = getSeekTime(file.getSize()); double transferTime = getTransferTime(file.getSize()); fileList_.add(file); // add the file into the HD nameList_.add(file.getName()); // add the name to the name list currentSize_ += file.getSize(); // increment the current HD size result = seekTime + transferTime; // add total time } file.setTransactionTime(result); return result; } /** * Adds a set of files to the storage. * Runs through the list of files and save all of them. * The time taken (in seconds) for adding each file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param list the files to be added * @return the time taken (in seconds) for adding the specified files */ public double addFile(List list) { double result = 0.0; if (list == null || list.size() == 0) { System.out.println(name_ + ".addFile(): Warning - list is empty."); return result; } Iterator it = list.iterator(); File file = null; while (it.hasNext()) { file = (File) it.next(); result += addFile(file); // add each file in the list } return result; } /** * Removes a file from the storage. * The time taken (in seconds) for deleting the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param fileName the name of the file to be removed * @return the deleted file */ public File deleteFile(String fileName) { if (fileName == null || fileName.length() == 0) { return null; } Iterator it = fileList_.iterator(); File file = null; while (it.hasNext()) { file = (File) it.next(); String name = file.getName(); // if a file is found then delete if (fileName.equals(name) == true) { double result = deleteFile(file); file.setTransactionTime(result); break; } else { file = null; } } return file; } /** * Removes a file from the storage. * The time taken (in seconds) for deleting the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param fileName the name of the file to be removed * @param file the file which is removed from the storage is returned * through this parameter * @return the time taken (in seconds) for deleting the specified file */ public double deleteFile(String fileName, File file) { return deleteFile(file); } /** * Removes a file from the storage. * The time taken (in seconds) for deleting the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param file the file which is removed from the storage is returned * through this parameter * @return the time taken (in seconds) for deleting the specified file */ public double deleteFile(File file) { double result = 0.0; // check if the file is valid or not if (isFileValid(file, "deleteFile()") == false) { return result; } double seekTime = getSeekTime(file.getSize()); double transferTime = getTransferTime(file.getSize()); // check if the file is in the storage if (contains(file) == true) { fileList_.remove(file); // remove the file HD nameList_.remove(file.getName()); //remove the name from name list currentSize_ -= file.getSize(); // decrement the current HD space result = seekTime + transferTime; // total time file.setTransactionTime(result); } return result; } /** * Checks whether a certain file is on the storage or not. * * @param fileName the name of the file we are looking for * @return <tt>true</tt> if the file is in the storage, <tt>false</tt> * otherwise */ public boolean contains(String fileName) { boolean result = false; if (fileName == null || fileName.length() == 0) { System.out.println(name_ + ".contains(): Warning - invalid file name"); return result; } // check each file in the list Iterator it = nameList_.iterator(); while (it.hasNext()) { String name = (String) it.next(); if (name.equals(fileName) == true) { result = true; break; } } return result; } /** * Checks whether a certain file is on the storage or not. * * @param file the file we are looking for * @return <tt>true</tt> if the file is in the storage, <tt>false</tt> * otherwise */ public boolean contains(File file) { boolean result = false; if (isFileValid(file, "contains()") == false) { return result; } result = contains(file.getName()); return result; } /** * Renames a file on the storage. * The time taken (in seconds) for renaming the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * @param file the file we would like to rename * @param newName the new name of the file * * @return <tt>true</tt> if the renaming succeeded, <tt>false</tt> * otherwise */ public boolean renameFile(File file, String newName) { // check whether the new filename is conflicting with existing ones // or not boolean result = false; if (contains(newName) == true) { return result; } // replace the file name in the file (physical) list File obj = getFile(file.getName()); if (obj != null) { obj.setName(newName); } else { return result; } // replace the file name in the name list Iterator it = nameList_.iterator(); while (it.hasNext()) { String name = (String) it.next(); if (name.equals(file.getName()) == true) { file.setTransactionTime(0); nameList_.remove(name); nameList_.add(newName); result = true; break; } } return result; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -