void cleanupStreams() { FileUtil.cleanupStream(objectWriteStream); objectWriteStream = null; FileUtil.cleanupStream(bufferedWriteStream); bufferedWriteStream = null; FileUtil.cleanupStream(fileWriteStream); fileWriteStream = null; FileUtil.cleanupStream(objectReadStream); objectReadStream = null; FileUtil.cleanupStream(bufferedReadStream); bufferedReadStream = null; FileUtil.cleanupStream(fileReadStream); fileReadStream = null; }
private boolean isInvalidURL(URL url) { if (url != null) { InputStream is = null; try { URLConnection conn = URLJarCollector.openConnection(url); is = conn.getInputStream(); return false; } catch (IOException e) { return true; } finally { FileUtil.cleanupStream(is); } } return true; }
private static Properties loadTeamProperties(File target) { Properties team = new Properties(); if (target.exists()) { FileInputStream fis = null; try { fis = new FileInputStream(target); team.load(fis); } catch (Exception e) { Logger.logError(e); } finally { FileUtil.cleanupStream(fis); } } return team; } }
private boolean loadProperties() { if (!isPropertiesLoaded && propertiesURL != null) { InputStream ios = null; try { URLConnection con = URLJarCollector.openConnection(propertiesURL); ios = con.getInputStream(); properties.load(ios); isPropertiesLoaded = true; return true; } catch (IOException e) { return false; } finally { FileUtil.cleanupStream(ios); } } return false; }
private void visitItems(List<IRepositoryItem> repositoryItems) { String root = jarPath; InputStream is = null; BufferedInputStream bis = null; JarInputStream jarIS = null; try { URLConnection con = URLJarCollector.openConnection(rootURL); is = con.getInputStream(); bis = new BufferedInputStream(is); jarIS = new JarInputStream(bis); readJarStream(repositoryItems, root, jarIS); } catch (Exception e) { Logger.logError(rootURL + " is probably corrupted (" + e.getClass().getName() + " " + e.getMessage() + ")"); } finally { FileUtil.cleanupStream(jarIS); FileUtil.cleanupStream(bis); FileUtil.cleanupStream(is); } }
public static void extractFile(File dest, JarInputStream jarIS, JarEntry entry) throws IOException { File out = new File(dest, entry.getName()); File parentDirectory = new File(out.getParent()); if (!parentDirectory.exists() && !parentDirectory.mkdirs()) { Logger.logError("Cannot create dir: " + parentDirectory); } FileOutputStream fos = null; BufferedOutputStream bos = null; byte buf[] = new byte[2048]; try { fos = new FileOutputStream(out); bos = new BufferedOutputStream(fos); int num; while ((num = jarIS.read(buf, 0, 2048)) != -1) { bos.write(buf, 0, num); } } finally { FileUtil.cleanupStream(bos); FileUtil.cleanupStream(fos); } } }
/** * {@inheritDoc} */ public void save(OutputStream out) { Set<IRepositoryItem> uniqueItems = new HashSet<IRepositoryItem>(); Set<IRepositoryRoot> uniqueRoots = new HashSet<IRepositoryRoot>(); for (IRepositoryItem repositoryItem : repositoryItems.values()) { uniqueItems.add(repositoryItem); } for (IRepositoryRoot root : roots.values()) { uniqueRoots.add(root); } ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(out); oos.writeObject(uniqueRoots); oos.writeObject(uniqueItems); } catch (IOException e) { Logger.logError("Can't save robot database", e); } finally { FileUtil.cleanupStream(oos); } }
private void saveProperties() { File file = new File(root.getPath(), className.replaceAll("\\.", "/") + PROPERTIES_EXTENSION); FileOutputStream fos = null; try { fos = new FileOutputStream(file); saveProperties(fos); } catch (IOException e) { e.printStackTrace(); } finally { FileUtil.cleanupStream(fos); } populatePropertiesURLFromClassURL(); }
private boolean loadProperties() { if (itemUrl != null) { InputStream ios = null; try { final URLConnection connection = URLJarCollector.openConnection(itemUrl); ios = connection.getInputStream(); properties.load(ios); return true; } catch (IOException e) { Logger.logError(e); } finally { FileUtil.cleanupStream(ios); } } return false; }
private void save() { FileOutputStream fos = null; try { fos = new FileOutputStream(new File(getRobotsDirectory(), DATABASE_FILENAME)); repository.save(fos); } catch (IOException e) { Logger.logError("Can't save robot database", e); } finally { FileUtil.cleanupStream(fos); } }
copyStream(is, os); } finally { FileUtil.cleanupStream(is); FileUtil.cleanupStream(os);
private Repository load() { Repository repository = new Repository(); FileInputStream fis = null; try { File file = new File(getRobotsDirectory(), DATABASE_FILENAME); if (file.exists()) { fis = new FileInputStream(file); repository.load(fis); } } catch (IOException e) { Logger.logError("Can't load robot database", e); repository = null; } finally { FileUtil.cleanupStream(fis); } return repository; }
public void saveCompilerProperties() { FileOutputStream out = null; try { out = new FileOutputStream(FileUtil.getCompilerConfigFile()); getCompilerProperties().store(out, "Robocode Compiler Properties"); } catch (IOException e) { Logger.logError(e); } finally { FileUtil.cleanupStream(out); } }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public void load(InputStream in) { Set<IRepositoryItem> uniqueItems; Set<IRepositoryRoot> uniqueRoots; ObjectInputStream ois = null; try { ois = new ObjectInputStream(in); uniqueRoots = (Set<IRepositoryRoot>) ois.readObject(); uniqueItems = (Set<IRepositoryItem>) ois.readObject(); for (IRepositoryRoot root : uniqueRoots) { ((BaseRoot) root).setRepository(this); String key = root.getURL().toString(); key = URLDecoder.decode(key, "UTF-8"); roots.put(key, root); } for (IRepositoryItem repositoryItem : uniqueItems) { addOrUpdateItem(repositoryItem); } } catch (IOException e) { Logger.logError("Can't load robot database: " + e.getMessage()); } catch (ClassNotFoundException e) { Logger.logError("Can't load robot database: " + e.getMessage()); } finally { FileUtil.cleanupStream(ois); } }
private static void addTeamItemToJar(JarOutputStream target, Set<String> jarEntries, TeamItem teamItem, RobotProperties props) throws IOException { String robotPathWithoutFileExt = teamItem.getRelativePath() + '/' + teamItem.getShortClassName(); String fileExt = ".team"; File file = new File(FileUtil.getRobotsDir(), robotPathWithoutFileExt + fileExt); // Store .team file into local robot dir FileOutputStream fis = null; try { fis = new FileOutputStream(file); teamItem.storeProperties(fis, props); } finally { FileUtil.cleanupStream(fis); } // Store .team file into jar file addToJar(target, jarEntries, FileUtil.getRobotsDir().getPath(), robotPathWithoutFileExt, fileExt); }
public static String createPackage(File jarFile, List<RobotItem> robotItems, TeamItem teamItem, RobotProperties props) { JarOutputStream jarOut = null; FileOutputStream fos = null; Set<String> jarEntries = new HashSet<String>(); try { fos = new FileOutputStream(jarFile); jarOut = new JarOutputStream(fos, createManifest(robotItems)); String rVersion = Container.getComponent(IVersionManager.class).getVersion(); jarOut.setComment(rVersion + " - Robocode version"); boolean isTeam = teamItem != null; // Add robot items addRobotItemToJar(jarOut, jarEntries, robotItems, props, isTeam); // Add team item, if it exists if (isTeam) { addTeamItemToJar(jarOut, jarEntries, teamItem, props); } } catch (IOException e) { Logger.logError(e); } finally { FileUtil.cleanupStream(jarOut); FileUtil.cleanupStream(fos); } StringBuilder sb = new StringBuilder(); for (String entry : jarEntries) { sb.append(entry).append('\n'); } appendCodeSize(jarFile, sb); return sb.toString(); }
public void saveBattleProperties() { if (battleProperties == null) { logError("Cannot save null battle properties"); return; } if (battleFilename == null) { logError("Cannot save battle to null path, use setBattleFilename()"); return; } FileOutputStream out = null; try { out = new FileOutputStream(battleFilename); battleProperties.store(out, "Battle Properties"); } catch (IOException e) { logError("IO Exception saving battle properties: " + e); } finally { FileUtil.cleanupStream(out); } }
private static void addRobotItemToJar(JarOutputStream target, Set<String> jarEntries, List<? extends IRobotItem> robotItems, RobotProperties props, boolean isTeam) throws IOException { for (IRobotItem robotItem : robotItems) { String path = robotItem.getRelativePath() + '/' + robotItem.getShortClassName() + ".properties"; // Add the code size to the robot properties before storing them String classPath = robotItem.getClassPathURL().getPath(); // Takes the dev.path into account! File dir = new File(classPath, robotItem.getRelativePath()); Integer codeSize = CodeSizeCalculator.getDirectoryCodeSize(dir); props.setCodeSize(codeSize); if (!isTeam) { // Store local .properties file FileOutputStream fis = null; try { File file = new File(classPath, path); fis = new FileOutputStream(file); robotItem.storeProperties(fis, props); } finally { FileUtil.cleanupStream(fis); } } // Package robot files (.class, .properties, .java) into jar file addRobotFilesToJar(target, jarEntries, robotItem, props); } }
public static void createOrUpdateTeam(File target, TeamProperties props) throws IOException { FileOutputStream os = null; try { Properties team = loadTeamProperties(target); if (props.getVersion() != null) { team.setProperty(TEAM_VERSION, props.getVersion()); } if (props.getMembers() != null) { team.setProperty(TEAM_MEMBERS, props.getMembers()); } if (props.getAuthor() != null) { team.setProperty(TEAM_AUTHOR_NAME, props.getAuthor()); } if (props.getDescription() != null) { team.setProperty(TEAM_DESCRIPTION, props.getDescription()); } if (props.getWebPage() != null) { team.setProperty(TEAM_WEBPAGE, props.getWebPage().toExternalForm()); } String version = Container.getComponent(IVersionManager.class).getVersion(); team.setProperty(ROBOCODE_VERSION, version); os = new FileOutputStream(target); team.store(os, "Robocode robot team"); } finally { FileUtil.cleanupStream(os); } }
public BattleProperties loadBattleProperties() { BattleProperties res = new BattleProperties(); FileInputStream in = null; try { in = new FileInputStream(getBattleFilename()); res.load(in); } catch (FileNotFoundException e) { logError("No file " + battleFilename + " found, using defaults."); } catch (IOException e) { logError("Error while reading " + getBattleFilename() + ": " + e); } finally { FileUtil.cleanupStream(in); } return res; }