Example usage for org.apache.commons.io IOUtils closeQuietly

List of usage examples for org.apache.commons.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(OutputStream output) 

Source Link

Document

Unconditionally close an OutputStream.

Usage

From source file:fi.jumi.core.util.LocallyDefiningClassLoader.java

private byte[] getBytecode(String name) {
    String resource = name.replace('.', '/') + ".class";
    InputStream in = getResourceAsStream(resource);
    if (in == null) {
        return null;
    }//  ww w  . j  a v  a 2  s  .c o m
    try {
        return IOUtils.toByteArray(in);
    } catch (IOException e) {
        throw Boilerplate.rethrow(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:cd.education.data.collector.android.utilities.ZipUtils.java

public static File extractFirstZipEntry(File zipFile, boolean deleteAfterUnzip) throws IOException {
    ZipInputStream zipInputStream = null;
    File targetFile = null;/*from w w  w  .ja va2  s . co m*/
    try {
        zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry != null) {
            targetFile = doExtractInTheSameFolder(zipFile, zipInputStream, zipEntry);
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }

    if (deleteAfterUnzip && targetFile != null && targetFile.exists()) {
        FileUtils.deleteAndReport(zipFile);
    }

    return targetFile;
}

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Reads a file into a byte[]./*  w ww .ja  v a2 s . c o  m*/
 *
 * @param file
 * @param step
 * @return the resulting byte[]
 */
public static byte[] readFileToByteArray(final File file, final Step step) {
    String canonicalPath = null;
    byte[] result = null;
    InputStream inputStream = null;
    try {
        canonicalPath = file.getCanonicalPath();
        inputStream = new FileInputStream(file);
        result = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new StepExecutionException("Could not find/read \"" + canonicalPath + "\".", step);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return result;
}

From source file:mitm.common.mime.MimeTypesFactory.java

/**
 * Creates and returns a MimeTypes instance from the specified file.
 *///from w  ww .j  a  v a 2s . com
public static MimeTypes create(File mimeTypes) throws IOException, MimeMediaTypeException {
    Check.notNull(mimeTypes, "mimeTypes");

    FileInputStream input = new FileInputStream(mimeTypes);

    try {
        return create(input);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:com.teotigraphix.caustk.service.JsonUtils.java

static <T> T fromGson(File file, Class<T> classOfT, ICaustkController controller) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();/*from ww w  .  jav a 2s  .  c  o m*/
    FileReader reader = null;
    T result = null;
    try {
        reader = new FileReader(file);
        result = gson.fromJson(reader, classOfT);
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
    } catch (JsonIOException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(reader);
    }
    IInjectorService injectorService = controller.getComponent(IInjectorService.class);
    if (injectorService != null)
        injectorService.inject(result);
    if (result instanceof ISerialize)
        ((ISerialize) result).wakeup(controller);
    return result;
}

From source file:com.googlecode.osde.internal.utils.ResourceUtil.java

/**
 * Retrieve the content in the resource file which you specify.
 *
 * @param path The resource file path.//from   w w  w.j  av a  2 s.  c  om
 * @param encoding File encoding.
 * @return The Content.
 * @throws IOException When some errors occurred.
 */
public static String loadTextResourceFile(String path, String encoding) throws IOException {
    InputStreamReader in = null;
    StringWriter out = null;
    try {
        in = new InputStreamReader(ResourceUtil.class.getResourceAsStream(path), encoding);
        out = new StringWriter();
        IOUtils.copy(in, out);
        String result = out.toString();
        return result;
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:io.selendroid.builder.AndroidDriverAPKBuilder.java

public File extractAndroidDriverAPK() {
    InputStream is = AndroidDriverAPKBuilder.class.getResourceAsStream(
            PREBUILD_WEBVIEW_APP_PATH_PREFIX + SelendroidServerBuilder.getJarVersionNumber() + ".apk");

    try {/*from  w w  w.ja  va  2 s. com*/
        File tmpAndroidDriver = File.createTempFile("android-driver", ".apk");
        IOUtils.copy(is, new FileOutputStream(tmpAndroidDriver));
        IOUtils.closeQuietly(is);
        return tmpAndroidDriver;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.quinsoft.zeidon.android.AndroidUtils.java

/**
 * Checks to see if the targetDbName exists.  If not it will be copied from emptyDbName,
 * which must be in the assets directory.
 *
 * @param context//  www. jav  a  2  s.c  om
 * @param emptyDbName
 * @param targetDbName
 */
public static void initializeDatabase(ContextWrapper context, String emptyDbName, String targetDbName) {
    context.getExternalFilesDir(null); // Create files directory if necessary.
    File f = new File(targetDbName);
    if (f.exists()) // Does DB already exist?
        return;

    Log.i("AndroidUtils.initializeDatabase",
            "Initializing DB by copying " + emptyDbName + " to " + f.getAbsolutePath());
    InputStream ins = null;
    FileOutputStream fos = null;
    try {
        ins = context.getAssets().open(emptyDbName);
    } catch (IOException e) {
        throw ZeidonException.wrapException(e).prependFilename(emptyDbName);
    }

    try {
        fos = new FileOutputStream(f);
    } catch (Exception e) {
        IOUtils.closeQuietly(ins);
        throw ZeidonException.wrapException(e).prependFilename(targetDbName);
    }

    try {
        byte[] buffer = new byte[1024];
        int size = 0;

        while ((size = ins.read(buffer, 0, buffer.length)) >= 0)
            fos.write(buffer, 0, size);
    } catch (IOException e) {
        throw ZeidonException.wrapException(e).prependFilename(emptyDbName);
    } finally {
        IOUtils.closeQuietly(ins);
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.ikon.module.db.stuff.FsDataStore.java

/**
 * Write to data store /* w ww  .  j av  a2s .co  m*/
 */
public static File save(String uuid, InputStream is) throws IOException {
    log.debug("save({}, {})", uuid, is);
    File fs = resolveFile(uuid);
    fs.getParentFile().mkdirs();
    FileOutputStream fos = new FileOutputStream(fs);
    IOUtils.copy(is, fos);
    IOUtils.closeQuietly(fos);
    return fs;
}

From source file:com.jdom.util.PropertiesUtil.java

private static Properties readPropertiesFile(final InputStream is) throws IllegalArgumentException {

    Properties properties = new Properties();

    try {//from ww  w. jav  a 2s  . c  o m
        properties.load(is);
    } catch (IOException e) {
        throw new IllegalArgumentException("Unable to read the properties file!", e);
    } finally {
        IOUtils.closeQuietly(is);
    }

    return properties;
}