Example usage for java.io BufferedInputStream close

List of usage examples for java.io BufferedInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
    File dir = new File(destDir);
    fileName = getString(fileName);/*from ww  w  .  j a  v  a2  s.  c o m*/
    if (!dir.exists()) {
        dir.mkdirs();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    try {
        File saveFile = new File(destDir, fileName);
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        int len = -1;
        while ((len = bis.read()) != -1) {
            bos.write(len);
            bos.flush();
        }
        bos.close();
        bis.close();
        return saveFile;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:downloadwolkflow.getWorkFlowList.java

private static void downloadFiles(String downloadUrl, CloseableHttpClient httpclient) {
    HttpGet httpget = new HttpGet(downloadUrl);
    HttpEntity entity = null;//from  www .jav  a2  s .c o m
    try {
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            String filename = downloadUrl.split("/")[downloadUrl.split("/").length - 1].split("\\?")[0];
            System.out.println(filename);
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File("data/" + filename)));
            int readedByte;
            while ((readedByte = bis.read()) != -1) {
                bos.write(readedByte);
            }
            bis.close();
            bos.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.icesoft.icefaces.tutorial.component.autocomplete.AutoCompleteDictionary.java

private static void init() {
    // Raw list of xml cities.
    List cityList = null;/* ww  w  . j a  va  2s  .  c o m*/

    // load the city dictionary from the compressed xml file.

    // get the path of the compressed file
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String basePath = session.getServletContext().getRealPath("/WEB-INF/resources");
    basePath += "/city.xml.zip";

    // extract the file
    ZipEntry zipEntry;
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(basePath);
        zipEntry = zipFile.getEntry("city.xml");
    } catch (Exception e) {
        log.error("Error retrieving records", e);
        return;
    }

    // get the xml stream and decode it.
    if (zipFile.size() > 0 && zipEntry != null) {
        try {
            BufferedInputStream dictionaryStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            XMLDecoder xDecoder = new XMLDecoder(dictionaryStream);
            // get the city list.
            cityList = (List) xDecoder.readObject();
            dictionaryStream.close();
            zipFile.close();
            xDecoder.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("Error getting city list, not city objects", e);
            return;
        } catch (IOException e) {
            log.error("Error getting city list", e);
            return;
        }
    }

    // Finally load the object from the xml file.
    if (cityList != null) {
        dictionary = new ArrayList(cityList.size());
        City tmpCity;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                dictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        // finally sort the list
        Collections.sort(dictionary, LABEL_COMPARATOR);
    }

}

From source file:Main.java

public static byte[] getFileBytes(File file) throws IOException {
    BufferedInputStream bis = null;
    try {/* w w  w  .  j  a  v a2s. c o  m*/
        bis = new BufferedInputStream(new FileInputStream(file));
        int bytes = (int) file.length();
        byte[] buffer = new byte[bytes];
        int readBytes = bis.read(buffer);
        if (readBytes != buffer.length) {
            throw new IOException("Entire file not read");
        }
        return buffer;
    } finally {
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:com.atlbike.etl.service.ClientFormLogin.java

private static void saveEntity(HttpEntity loginEntity) throws IOException, FileNotFoundException {
    InputStream inStream = loginEntity.getContent();
    BufferedInputStream bis = new BufferedInputStream(inStream);
    String path = "localFile.csv";
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int byteCount = 0;
    int inByte;// www  .  j a va 2  s  .c  om
    while ((inByte = bis.read()) != -1) {
        bos.write(inByte);
        byteCount++;
    }
    bis.close();
    bos.close();
    System.out.println("Byte Count: " + byteCount);
}

From source file:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

private static void downloadAndSaveImageWithPrefix(Context context, String prefix, String fileName)
        throws IOException {
    String stringUrl = BASE_URL + prefix + fileName;
    URL url = new URL(stringUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream is = urlConnection.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    FileOutputStream fos = getFileOutputStream(context, fileName);

    ByteArrayBuffer baf = new ByteArrayBuffer(65535);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }/*www  .j av  a  2 s .c om*/
    fos.write(baf.toByteArray());
    fos.close();
    bis.close();
}

From source file:Main.java

public static Bitmap loadImageFromUrl(String url) {
    ByteArrayOutputStream out = null;
    Bitmap bitmap = null;//w w w. j av  a2 s .  c  o  m
    int BUFFER_SIZE = 1024 * 8;
    try {
        BufferedInputStream in = new BufferedInputStream(new URL(url).openStream(), BUFFER_SIZE);
        out = new ByteArrayOutputStream(BUFFER_SIZE);
        int length = 0;
        byte[] tem = new byte[BUFFER_SIZE];
        length = in.read(tem);
        while (length != -1) {
            out.write(tem, 0, length);
            out.flush();
            length = in.read(tem);
        }
        in.close();
        if (out.toByteArray().length != 0) {
            bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
        } else {
            out.close();
            return null;
        }
        out.close();
    } catch (OutOfMemoryError e) {

        out.reset();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2;
        opts.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size(), opts);
        return bitmap;
    } catch (Exception e) {

        return bitmap;
    }
    return bitmap;
}

From source file:com.useekm.types.AbstractGeo.java

public static byte[] gunzip(byte[] bytes) {
    try {/* w  w  w .j  a va2s .c om*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0)
            bos.write(buf, 0, len);
        byte[] result = bos.toByteArray();
        bufis.close();
        bos.close();
        return result;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gunzip", e);
    }
}

From source file:net.orpiske.ssps.common.archive.CompressedArchiveUtils.java

/**
 * Decompress a file/*from   w w w  .  j av  a 2s . c om*/
 * @param source the source file to be uncompressed
 * @param destination the destination directory
 * @return the number of bytes read
 * @throws IOException for lower level I/O errors
 */
public static long gzDecompress(File source, File destination) throws IOException {
    FileOutputStream out;

    prepareDestination(destination);
    out = new FileOutputStream(destination);

    FileInputStream fin = null;
    BufferedInputStream bin = null;
    GzipCompressorInputStream gzIn = null;

    try {
        fin = new FileInputStream(source);
        bin = new BufferedInputStream(fin);
        gzIn = new GzipCompressorInputStream(bin);

        IOUtils.copy(gzIn, out);

        gzIn.close();

        fin.close();
        bin.close();
        out.close();
    } catch (IOException e) {
        IOUtils.closeQuietly(out);

        IOUtils.closeQuietly(fin);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(gzIn);

        throw e;
    }

    return gzIn.getBytesRead();
}

From source file:net.orpiske.ssps.common.archive.CompressedArchiveUtils.java

/**
 * Decompress a file/*from w w  w.  j  av  a 2s.c  o  m*/
 * @param source the source file to be uncompressed
 * @param destination the destination directory
 * @return the number of bytes read
 * @throws IOException for lower level I/O errors
 */
public static long bzipDecompress(File source, File destination) throws IOException {
    FileOutputStream out;

    prepareDestination(destination);

    out = new FileOutputStream(destination);

    FileInputStream fin = null;
    BufferedInputStream bin = null;
    BZip2CompressorInputStream bzIn = null;

    try {
        fin = new FileInputStream(source);
        bin = new BufferedInputStream(fin);
        bzIn = new BZip2CompressorInputStream(bin);

        IOUtils.copy(bzIn, out);

        bzIn.close();

        fin.close();
        bin.close();
        out.close();
    } catch (IOException e) {
        IOUtils.closeQuietly(out);

        IOUtils.closeQuietly(fin);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(bzIn);

        throw e;
    }

    return bzIn.getBytesRead();
}