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:emperior.Main.java

public static void updateResumeTask(String task) {
    if (!adminmode) {
        Properties properties = new Properties();
        try {/*  ww w.ja  v  a 2  s .  co  m*/
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream("Emperior.properties"));
            properties.load(stream);
            properties.setProperty("resumetask", task);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Emperior.properties"));
            properties.store(out, "");
            out.close();
            stream.close();
        } catch (Exception e) {

        }
    }
}

From source file:emperior.Main.java

public static void updateStartedWithTask(String task) {
    if (!adminmode) {
        Properties properties = new Properties();
        try {/*from   w w w. j av  a 2s .  c o  m*/
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream("Emperior.properties"));
            properties.load(stream);
            properties.setProperty("startedwith", task);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Emperior.properties"));
            properties.store(out, "");
            out.close();
            stream.close();
        } catch (Exception e) {

        }
    }
}

From source file:com.iStudy.Study.Renren.Util.java

/**
 * ???//  w  w w  . jav  a 2s  . c  om
 * 
 * @param inputStream
 * @return
 * @throws IOException
 */
public static byte[] streamToByteArray(InputStream inputStream) {
    byte[] content = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream bis = new BufferedInputStream(inputStream);

    try {
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        content = baos.toByteArray();
        if (content.length == 0) {
            content = null;
        }

        baos.close();
        bis.close();
    } catch (IOException e) {
        logger(e.getMessage());
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (IOException e) {
                logger(e.getMessage());
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
                logger(e.getMessage());
            }
        }
    }

    return content;
}

From source file:Main.java

private static void downloadResource(String from, String to) {
    OutputStream outputStream = null;
    BufferedInputStream inputStream = null;
    HttpURLConnection connection = null;
    URL url;/*from w w w . ja va  2  s .  c  om*/
    byte[] buffer = new byte[1024];

    try {
        url = new URL(from);

        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        outputStream = new FileOutputStream(to);

        inputStream = new BufferedInputStream(url.openStream());
        int read;

        while ((read = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, read);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        if (connection != null)
            connection.disconnect();
    }
}

From source file:com.nwn.NwnFileHandler.java

/**
 * Downloads file from given url//from  ww w  .j av  a  2 s.c o m
 * @param fileUrl String of url to download
 * @param dest Location on system where file should be downloaded
 * @return True if download success, False if download failed
 */
public static boolean downloadFile(String fileUrl, String dest) {
    try {
        URL url = new URL(fileUrl);
        BufferedInputStream bis = new BufferedInputStream(url.openStream());
        FileOutputStream fis = new FileOutputStream(dest);
        String fileSizeString = url.openConnection().getHeaderField("Content-Length");
        double fileSize = Double.parseDouble(fileSizeString);
        byte[] buffer = new byte[1024];
        int count;
        double bytesDownloaded = 0.0;
        while ((count = bis.read(buffer, 0, 1024)) != -1) {
            bytesDownloaded += count;
            fis.write(buffer, 0, count);

            int downloadStatus = (int) ((bytesDownloaded / fileSize) * 100);

            System.out.println("Downloading " + fileUrl + " to " + dest + " " + downloadStatus + "%");
        }
        fis.close();
        bis.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        return false;
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.miloisbadboy.net.Utility.java

/**
 * Upload image into output stream ./*  w  w w . j  a  va  2 s  . c o m*/
 * 
 * @param out
 *            : output stream for uploading weibo
 * @param imgpath
 *            : bitmap for uploading
 * @return void
 */
private static void imageContentToUpload(OutputStream out, Bitmap imgpath) throws RequestException {
    StringBuilder temp = new StringBuilder();

    temp.append(MP_BOUNDARY).append("\r\n");
    temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"").append("news_image")
            .append("\"\r\n");
    String filetype = "image/png";
    temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    BufferedInputStream bis = null;
    try {
        out.write(res);
        imgpath.compress(CompressFormat.PNG, 75, out);
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new RequestException(e);
    } finally {
        if (null != bis) {
            try {
                bis.close();
            } catch (IOException e) {
                throw new RequestException(e);
            }
        }
    }
}

From source file:com.eryansky.modules.disk.utils.DiskUtils.java

/**
 * ????//w w  w  . j  av a2  s. c o  m
 *
 * @param inFiles
 *            ?
 * @return
 * @throws Exception
 */
private static void doZipFile(ZipOutputStream zipOut, List<File> inFiles) throws Exception {
    if (Collections3.isNotEmpty(inFiles)) {
        Map<String, Integer> countMap = Maps.newHashMap();

        for (File file : inFiles) {
            String name = file.getName();
            Integer mapVal = countMap.get(name);
            String newName = name;
            if (mapVal == null) {
                mapVal = 0;
            } else {
                mapVal++;
                int index = name.lastIndexOf(".");
                if (index > -1) {
                    newName = (new StringBuffer(name).insert(index, "(" + mapVal + ")")).toString();
                } else {
                    newName = (new StringBuffer(name).append("(" + mapVal + ")")).toString();
                }
            }
            if (file.getDiskFile().isFile()) {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file.getDiskFile()));
                ZipEntry entry = new ZipEntry(newName);
                zipOut.putNextEntry(entry);
                byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
                int size;
                while ((size = bis.read(buff, 0, buff.length)) != -1) {
                    zipOut.write(buff, 0, size);
                }
                zipOut.closeEntry();
                bis.close();
            }
            countMap.put(name, mapVal);

        }
    }
}

From source file:com.DPFaragir.DPFUtils.java

public static String loadFileAsString(String filename) throws java.io.IOException {
    final int BUFLEN = 1024;
    BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN);
    try {/*ww  w  . ja  v a2 s  . c  o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN);
        byte[] bytes = new byte[BUFLEN];
        boolean isUTF8 = false;
        int read, count = 0;
        while ((read = is.read(bytes)) != -1) {
            if (count == 0 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
                isUTF8 = true;
                baos.write(bytes, 3, read - 3); // drop UTF8 bom marker
            } else {
                baos.write(bytes, 0, read);
            }
            count += read;
        }
        return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray());
    } finally {
        try {
            is.close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.seleniumtests.util.FileUtility.java

public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException {
    File firefoxProfile = new File(storeLocation);
    String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile();

    try (JarFile jar = new JarFile(location);) {
        logger.info("Extracting jar file::: " + location);
        firefoxProfile.mkdir();//from  w  w  w. ja va  2s.  co  m

        Enumeration<?> jarFiles = jar.entries();
        while (jarFiles.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) jarFiles.nextElement();
            String currentEntry = entry.getName();
            File destinationFile = new File(storeLocation, currentEntry);
            File destinationParent = destinationFile.getParentFile();

            // create the parent directory structure if required
            destinationParent.mkdirs();
            if (!entry.isDirectory()) {
                BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry));
                int currentByte;

                // buffer for writing file
                byte[] data = new byte[BUFFER];

                // write the current file to disk
                try (FileOutputStream fos = new FileOutputStream(destinationFile);) {
                    BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);

                    // read and write till last byte
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        destination.write(data, 0, currentByte);
                    }

                    destination.flush();
                    destination.close();
                    is.close();
                }
            }
        }
    }

    FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF"));
    if (OSUtility.isWindows()) {
        new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete();
    } else {
        new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete();
    }
}

From source file:marytts.util.io.FileUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;/* w w  w .j  a v a  2s.  c  om*/
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}