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:com.ms.commons.test.tool.exportdata.DatabasePropertiesLoader.java

private static Properties loadProperties(File file) {
    Properties properties = new Properties();
    FileInputStream fis = null;/*from   w  ww . ja v a2s  .  co  m*/
    try {
        fis = new FileInputStream(file);
        properties.load(fis);
    } catch (Exception e) {
        throw ExceptionUtil.wrapToRuntimeException(e);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    return properties;
}

From source file:net.gbmb.collector.impl.TestTempStorage.java

@Override
public String store(String cid, String contentId, InputStream content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(content, bos);//  www.  j  a  v  a2s . co m
    IOUtils.closeQuietly(content);
    IOUtils.closeQuietly(bos);
    getCidContents(cid).put(contentId, bos.toByteArray());
    return contentId;
}

From source file:de.fhg.iais.commons.stream.AutoFileOutputStreamTest.java

@Test
public void testWritingAndCloseAsCompleted() throws IOException {
    AutoFileOutputStream os = new AutoFileOutputStream(FILE_STRING, false);
    os.write(CONTENT);//from   w  w  w .  j  a va  2  s.c o  m
    os.closeAsCompleted();
    IOUtils.closeQuietly(os);

    checkContentAndRemoveFile(CONTENT);
}

From source file:costumetrade.common.util.PdfUtils.java

public static PDDocument createImagePdf(File file) {

    PDPageContentStream contentStream = null;
    try {/*w w w  . j av  a  2s  .c  o  m*/
        BufferedImage bimg = ImageIO.read(file);
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(new PDRectangle(bimg.getWidth(), bimg.getHeight()));
        document.addPage(page);
        PDImageXObject image = PDImageXObject.createFromFileByExtension(file, document);
        contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(image, 0, 0);
        return document;
    } catch (IOException e) {
        throw new RuntimeException("?PDF", e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }

}

From source file:com.lightboxtechnologies.nsrl.RecordLoader.java

public void load(InputStream in, LineHandler lh) throws IOException {
    Reader r = null;/*from w  ww.  j  a  v  a  2s  . com*/
    try {
        r = new InputStreamReader(in);
        load(r, lh);
        r.close();
    } finally {
        IOUtils.closeQuietly(r);
    }
}

From source file:com.s2g.pst.resume.importer.UnZipHelper.java

/**
 * Unzip it//from   w w w . j a v a  2  s .c o  m
 *
 * @param fileName
 * @param outputFolder
 *
 * @throws java.io.FileNotFoundException
 */
public void unZipFolder(String fileName, String outputFolder) throws IOException {

    ZipFile zipFile = new ZipFile(fileName);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(outputFolder, entry.getName());
        entryDestination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            //FileHelper fileHelper = new FileHelper();
            //String filename = fileHelper.getUniqueFileName(outputFolder, FilenameUtils.removeExtension(entry.getName()));
            //System.out.println("zip :" + filename);
            //new File(filename).mkdirs();

            entryDestination.mkdirs();

        } else {
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    zipFile.close();
    this.deleteZipFile(fileName);
    System.out.println("Done");

}

From source file:com.alibaba.simpleimage.RobustnessTest.java

public void testReadWrite() throws Exception {
    for (File imgFile : imgDir.listFiles()) {
        if (imgFile.getName().indexOf("jpg") < 0) {
            continue;
        }/*w  w w .java  2 s.co m*/
        if (imgFile.getName().indexOf("result") > 0) {
            continue;
        }

        String filename = imgFile.getName().substring(0, imgFile.getName().lastIndexOf("."));
        InputStream in = new FileInputStream(imgFile);
        OutputStream out = new FileOutputStream(new File(resultDir, "MALFORMED_" + filename + ".jpg"));
        WriteRender wr = null;
        try {
            ReadRender rr = new ReadRender(in, true);
            wr = new WriteRender(rr, out);

            wr.render();
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
}

From source file:com.discursive.jccook.net.FTPExample.java

public void start() throws IOException {

    FTPClient client = new FTPClient();
    OutputStream outStream = null;

    try {/*w w  w.j  a  va 2 s  .c  om*/
        client.connect("ftp.ibiblio.org");
        client.login("anonymous", "");

        String remoteFile = "/pub/micro/commodore/schematics/computers/c64/c64bus.gif";
        outStream = new FileOutputStream("c64bus.gif");

        client.retrieveFile(remoteFile, outStream);
    } catch (IOException ioe) {
        System.out.println("Error communicating with FTP server.");
    } finally {
        IOUtils.closeQuietly(outStream);
        try {
            client.disconnect();
        } catch (IOException e) {
            System.out.println("Problem disconnecting from FTP server");
        }
    }

    secondExample();

}

From source file:com.exzogeni.dk.graphics.Bitmaps.java

@Nullable
public static Bitmap decodeStream(@NonNull InputStream stream, Rect outPadding, int hwSize) {
    if (hwSize > 0) {
        final InputStream localIn = new BufferPoolInputStream(stream);
        try {/*  www  .  j  ava  2  s.co m*/
            final BitmapFactory.Options ops = new BitmapFactory.Options();
            ops.inJustDecodeBounds = true;
            localIn.mark(BITMAP_HEAD);
            BitmapFactory.decodeStream(localIn, outPadding, ops);
            ops.inSampleSize = calculateInSampleSize(ops, hwSize);
            ops.inJustDecodeBounds = false;
            localIn.reset();
            return BitmapFactory.decodeStream(localIn, outPadding, ops);
        } catch (IOException e) {
            Logger.error(e);
        } finally {
            IOUtils.closeQuietly(localIn);
        }
        return null;
    }
    return BitmapFactory.decodeStream(stream);
}

From source file:jeeves.utils.IO.java

/**
 * Loads a text file, handling the exceptions
 * @param name/*from  w  w  w . jav  a2s. c om*/
 * @return
 */
public static String loadFile(String name) {
    StringBuffer sb = new StringBuffer();

    FileInputStream in = null;
    BufferedReader rdr = null;
    try {
        in = new FileInputStream(name);
        rdr = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));

        String inputLine;

        while ((inputLine = rdr.readLine()) != null) {
            sb.append(inputLine);
            sb.append('\n');
        }

        return sb.toString();
    } catch (IOException e) {
        return null;
    } finally {
        if (in != null) {
            IOUtils.closeQuietly(in);
        }
        if (rdr != null) {
            IOUtils.closeQuietly(rdr);
        }
    }
}