Example usage for java.io Closeable close

List of usage examples for java.io Closeable close

Introduction

In this page you can find the example usage for java.io Closeable close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:com.primitive.library.task.DownloadTask.java

@Override
protected String doInBackground(Void... params) {
    File file = null;//from   w w w .ja  va2s.c o  m
    InputStream is = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
        cli.getParams().setParameter("http.connection.timeout", Long.valueOf(timeout));
        HttpResponse res = cli.execute(request);
        int rescode = res.getStatusLine().getStatusCode();

        if (rescode == HttpStatus.SC_OK) {
            int length = (int) res.getEntity().getContentLength();
            this.progress.setMax(length);
            byte[] buffer = new byte[buffer_size];
            file = new File(this.savePath);
            is = res.getEntity().getContent();
            bis = new BufferedInputStream(is, buffer_size);
            bos = new BufferedOutputStream(new FileOutputStream(file, false), buffer_size);
            for (int size = 0; -1 < (size = bis.read(buffer));) {
                bos.write(buffer);
                this.publishProgress(size);
            }
            bos.flush();
            return this.savePath;
        } else {
            switch (rescode) {
            case HttpStatus.SC_NOT_FOUND:
                break;
            case HttpStatus.SC_REQUEST_TIMEOUT:
                break;
            default:
                break;
            }
            return null;
        }

    } catch (Throwable ex) {
        Logger.err(ex);
        return null;
    } finally {
        Closeable[] closeables = new Closeable[] { bos, bis, is };
        for (Closeable close : closeables) {
            if (close != null) {
                try {
                    close.close();
                } catch (IOException ex) {
                    Logger.warm(ex);
                }
            }
        }
    }
}

From source file:edu.umn.msi.tropix.common.io.IOUtilsTest.java

@Test(groups = "unit")
public void close() throws IOException {
    final Closeable closeable = EasyMock.createMock(Closeable.class);
    closeable.close();
    EasyMock.replay(closeable);//from w  w w  .j  a va  2s. com
    this.ioUtils.closeQuietly(closeable);
    EasyMock.verify(closeable);
}

From source file:edu.umn.msi.tropix.common.io.IOUtilsTest.java

@Test(groups = "unit")
public void closesQuietly() throws IOException {
    final Closeable closeable = EasyMock.createMock(Closeable.class);
    closeable.close();
    EasyMock.expectLastCall().andThrow(new IOException());
    EasyMock.replay(closeable);/*from   w ww .ja va2s  .  c o  m*/
    this.ioUtils.closeQuietly(closeable);
    EasyMock.verify(closeable);
}

From source file:com.epam.reportportal.apache.http.impl.client.InternalHttpClient.java

public void close() {
    this.connManager.shutdown();
    if (this.closeables != null) {
        for (final Closeable closeable : this.closeables) {
            try {
                closeable.close();
            } catch (final IOException ex) {
                this.log.error(ex.getMessage(), ex);
            }/*  w  w w  . j a  va2 s. com*/
        }
    }
}

From source file:com.openshift.internal.restclient.authorization.AuthorizationClient.java

private void close(Closeable closer) {
    if (closer == null)
        return;/*from   w w w . j a v a 2s.  co m*/
    try {
        closer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.http.HC4.impl.client.InternalHttpClient.java

@Override
public void close() {
    if (this.closeables != null) {
        for (final Closeable closeable : this.closeables) {
            try {
                closeable.close();
            } catch (final IOException ex) {
                this.log.error(ex.getMessage(), ex);
            }/*from   w w w . j a v  a  2 s .  c om*/
        }
    }
}

From source file:org.arquillian.droidium.native_.selendroid.SelendroidRebuilder.java

/**
 * Closes a stream/* w w w. ja  v a 2 s .  c om*/
 *
 * @param stream stream to be closed
 */
private void closeStream(Closeable stream) {
    if (stream == null) {
        return;
    }
    try {
        stream.close();
    } catch (IOException ignore) {
        // ignore
    } finally {
        stream = null;
    }
}

From source file:com.jkoolcloud.tnt4j.utils.Utils.java

/**
 * Close an object without exceptions/*from  www .j  a  va2s  .co  m*/
 *
 * @param obj
 *            object to close
 */
public static void close(Closeable obj) {
    try {
        if (obj != null) {
            obj.close();
        }
    } catch (Throwable e) {
    }
}

From source file:at.fhooe.mcm.webdav.WebDavInterface.java

/**
 * tries to close a closeable/* ww w . j av  a 2s  .c o  m*/
 * @param closeme
 */
private void tryClose(Closeable closeme) {
    if (closeme != null) {
        try {
            closeme.close();
        } catch (IOException e) {
        }
    }
}

From source file:bbd.basesimplenet.net.entity.MultipartEntity.java

private void closeSilently(Closeable closeable) {
    try {/*ww  w.ja v a 2 s. c om*/
        if (closeable != null) {
            closeable.close();
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
}