/** * Creates a <code>File</code> and places it into FAT32, the <code>File</code> list. * Pre-condition: size must be greater than 0 Post-condition: creates a <code>File</code> with * appropriate number of blocks and places it into internal <code>File</code> list (FAT32) * * @param filename the name of the <code>File</code> * @param size bytes in the <code>File</code> * @return successful creation message * @throws InSufficientDiskSpaceException if there are not enough blocks to create <code>File * </code> * @throws DuplicateFileException if a <code>File</code> with given filename already exists */ public String create(String filename, int size) throws InsufficientDiskSpaceException, DuplicateFileException { Iterator<File> findDuplicates = FAT32.iterator(); // Go through file list to find duplicates first while (findDuplicates.hasNext()) { if (findDuplicates.next().getName().equalsIgnoreCase(filename)) { throw new DuplicateFileException(); } } // Check for available space if (size > (freeBlocks.size() * blockSize)) { throw new InsufficientDiskSpaceException("Not enough disk space to create new file.\n"); } else { File file = new File(size, blockSize, filename); Iterator<Block> itr = freeBlocks.iterator(); // Iterate through list of available blocks, adding them to the new file as needed for (int i = 0; i < bytesBlocks(size); i++) { if (itr.hasNext()) { file.add(itr.next()); // After block is added to file, it's removed from available blocks itr.remove(); } } // Add file to file table FAT32.add(file); return filename + " created successfully!\n"; } }
/** * Extends a <code>File</code> by the given size in bytes. Pre-condition: size must be greater * than 0 Post-condition: <code>File</code> with the given filename will be extended by passed in * amount of bytes, and additional <code>Blocks</code> will be allocated to it as needed * * @param filename the name of the <code>File</code> * @param size number of bytes to increase the <code>File</code> by * @return successful extension message * @throws InSufficientDiskSpaceException if there are not enough blocks to extend <code>File * </code> * @throws NoSuchFileException if the filename does not match any existing <code>File</code> */ public String extend(String filename, int size) throws InsufficientDiskSpaceException, NoSuchFileException { Iterator<File> findFile = FAT32.iterator(); boolean fileFound = false; // Check if file exists while (findFile.hasNext()) { if (findFile.next().getName().equalsIgnoreCase(filename)) { fileFound = true; } } // Throw exception if it doesn't if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } // Check there is enough space to extend file if (size > (freeBlocks.size() * blockSize)) { throw new InsufficientDiskSpaceException( "Not enough disk space to extend " + filename + " \n"); } else { Iterator<File> itr = FAT32.iterator(); // Iterate through list of files to find filename while (itr.hasNext()) { File file = itr.next(); if (file.getName().equals(filename)) { // Calculate the extended size and how many blocks need to be added int newSize = file.getBytes() + size; int blocksToAdd = bytesBlocks(newSize) - bytesBlocks(file.getBytes()); // Take blocks from available blocks and add them to the file for (int i = 0; i < blocksToAdd; i++) { file.add(freeBlocks.remove(0)); } file.addBytes(size); } } // Throw exception or return appropriate message if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } else { return filename + " extended successfully!\n"; } } }