From 2f72cb043de4aa627788084aee771cc78032e9b5 Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Mon, 19 Nov 2012 14:47:35 +0100 Subject: [PATCH] Minor improvements of coffee-manager. --- .../se/sics/coffee/CoffeeFS.java | 41 +++++++++---------- .../se/sics/coffee/CoffeeImageFile.java | 12 +++--- .../se/sics/coffee/CoffeeManager.java | 14 +++---- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/tools/coffee-manager/se/sics/coffee/CoffeeFS.java b/tools/coffee-manager/se/sics/coffee/CoffeeFS.java index 52db04949..8ed6bdb78 100644 --- a/tools/coffee-manager/se/sics/coffee/CoffeeFS.java +++ b/tools/coffee-manager/se/sics/coffee/CoffeeFS.java @@ -37,8 +37,8 @@ import java.util.Map; import java.util.TreeMap; public class CoffeeFS { - private CoffeeImage image; - private CoffeeConfiguration conf; + private final CoffeeImage image; + private final CoffeeConfiguration conf; private int currentPage; private Map files; private static final int INVALID_PAGE = -1; @@ -129,27 +129,24 @@ public class CoffeeFS { } public CoffeeFile insertFile(File file) throws IOException { - CoffeeFile coffeeFile; - try { - FileInputStream input = new FileInputStream(file); - int allocatePages = pageCount(file.length()); - int start = findFreeExtent(allocatePages); + CoffeeFile coffeeFile; + FileInputStream input = new FileInputStream(file); + int allocatePages = pageCount(file.length()); + int start = findFreeExtent(allocatePages); - if (start == INVALID_PAGE) { - return null; - } - CoffeeHeader header = new CoffeeHeader(this, start); - header.setName(file.getName()); - header.setReservedSize(allocatePages); - header.allocate(); - coffeeFile = new CoffeeFile(this, header); - writeHeader(header); - coffeeFile.insertContents(input); - input.close(); - return coffeeFile; - } catch (FileNotFoundException e) { - } - return null; + if (start == INVALID_PAGE) { + input.close(); + return null; + } + CoffeeHeader header = new CoffeeHeader(this, start); + header.setName(file.getName()); + header.setReservedSize(allocatePages); + header.allocate(); + coffeeFile = new CoffeeFile(this, header); + writeHeader(header); + coffeeFile.insertContents(input); + input.close(); + return coffeeFile; } public void removeFile(String filename) diff --git a/tools/coffee-manager/se/sics/coffee/CoffeeImageFile.java b/tools/coffee-manager/se/sics/coffee/CoffeeImageFile.java index 0bb2db789..147cea2b3 100644 --- a/tools/coffee-manager/se/sics/coffee/CoffeeImageFile.java +++ b/tools/coffee-manager/se/sics/coffee/CoffeeImageFile.java @@ -32,38 +32,40 @@ package se.sics.coffee; -import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class CoffeeImageFile implements CoffeeImage { - private RandomAccessFile imageFile; - private CoffeeConfiguration conf; + private final RandomAccessFile imageFile; + private final CoffeeConfiguration conf; public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException { this.conf = conf; - File file = new File(filename); - imageFile = new RandomAccessFile(file, "rw"); + imageFile = new RandomAccessFile(filename, "rw"); if (imageFile.length() == 0) { // Allocate a full file system image. imageFile.setLength(conf.fsSize); } } + @Override public CoffeeConfiguration getConfiguration() { return conf; } + @Override public void read(byte[] bytes, int size, int offset) throws IOException { imageFile.seek(conf.startOffset + offset); imageFile.read(bytes, 0, size); } + @Override public void write(byte[] bytes, int size, int offset) throws IOException { imageFile.seek(conf.startOffset + offset); imageFile.write(bytes, 0, size); } + @Override public void erase(int size, int offset) throws IOException { byte[] bytes = new byte[256]; int chunkSize; diff --git a/tools/coffee-manager/se/sics/coffee/CoffeeManager.java b/tools/coffee-manager/se/sics/coffee/CoffeeManager.java index fd6d20734..0f409941b 100644 --- a/tools/coffee-manager/se/sics/coffee/CoffeeManager.java +++ b/tools/coffee-manager/se/sics/coffee/CoffeeManager.java @@ -40,20 +40,18 @@ import se.sics.coffee.CoffeeFS.CoffeeException; import se.sics.coffee.CoffeeFS.CoffeeFileException; public class CoffeeManager { - private static CoffeeFS coffeeFS; public enum Command { INSERT, EXTRACT, REMOVE, LIST, STATS }; public static void main(String args[]) { String platform = "sky"; - String usage = "Usage: java -jar coffee.jar "; Command command = Command.STATS; String filename = ""; String fsImage = ""; - - usage += "[-p ] "; - usage += "[-i|e|r ] "; - usage += "[-l|s] "; - usage += ""; + String usage = "Usage: java -jar coffee.jar " + + "[-p ] " + + "[-i|e|r ] " + + "[-l|s] " + + ""; if (args.length < 2) { System.err.println(usage); @@ -98,7 +96,7 @@ public class CoffeeManager { try { CoffeeConfiguration conf = new CoffeeConfiguration(platform + ".properties"); - coffeeFS = new CoffeeFS(new CoffeeImageFile(fsImage, conf)); + CoffeeFS coffeeFS = new CoffeeFS(new CoffeeImageFile(fsImage, conf)); switch (command) { case INSERT: if (coffeeFS.getFiles().get(filename) != null) {