+
+ /**
+ * Copies an input stream to an output stream.
+ * @param aIs Input stream.
+ * @param aOs Output stream.
+ */
+ public static void copyStream(InputStream aIs, OutputStream aOs) {
+ try {
+ int c;
+ while ((c = aIs.read()) != -1) {
+ aOs.write(c);
+ }
+ aIs.close();
+ aOs.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Recursively copy a directory.
+ *
+ * @param aSrc
+ * Source directory
+ * @param aTarget
+ * Target directory.
+ */
+ public static void copyDir(File aSrc, File aTarget) {
+ Assert.assertTrue(aSrc.isDirectory());
+ Assert.assertTrue(!aTarget.exists());
+
+ aTarget.mkdirs();
+
+ File[] files = aSrc.listFiles();
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ if (file.isDirectory()) {
+ if (!file.getName().equals(".svn")) {
+ copyDir(new File(aSrc, file.getName()), new File(aTarget,
+ file.getName()));
+ }
+ } else {
+ copyFile(file, new File(aTarget, file.getName()));
+ }
+ }
+ }
+
+ /**
+ * Copy a file. If copying fails then the testcase will fail.
+ *
+ * @param aSrc
+ * Source file.
+ * @param aTarget
+ * Target file.
+ */
+ public static void copyFile(File aSrc, File aTarget) {
+
+ try {
+ FileInputStream fis = new FileInputStream(aSrc);
+ FileOutputStream fos = new FileOutputStream(aTarget);
+ FileChannel fcin = fis.getChannel();
+ FileChannel fcout = fos.getChannel();
+
+ // map input file
+
+ MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
+ fcin.size());
+
+ // do the file copy
+ fcout.write(mbb);
+
+ // finish up
+
+ fcin.close();
+ fcout.close();
+ fis.close();
+ fos.close();
+ } catch (IOException e) {
+ Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
+ + aTarget.getPath() + " failed.", false);
+ }
+ }
+
+ /**
+ * Remove all files within a given directory including the directory itself.
+ * This only attempts to remove regular files and not directories within the
+ * directory. If the directory contains a nested directory, the deletion
+ * will fail. The test case will fail if this fails.
+ *
+ * @param aDir
+ * Directory to remove.
+ */
+ public static void deleteDir(File aDir) {
+ cleanDir(aDir);
+ delete(aDir);
+ }
+
+ /**
+ * Remove all regular files within a given directory.
+ *
+ * @param outputDirName
+ */
+ public static void cleanDir(File aDir) {
+ if (!aDir.exists()) {
+ return; // nothing to do.
+ }
+ File[] entries = aDir.listFiles();
+ for (int i = 0; i < entries.length; i++) {
+ File file = entries[i];
+ if (file.isFile()) {
+ Assert.assertTrue("Could not delete " + entries[i].getPath(),
+ entries[i].delete());
+ }
+ }
+ }