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:XMLResourceBundleControl.java

public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
        boolean reload) throws IllegalAccessException, InstantiationException, IOException {

    if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) {
        throw new NullPointerException();
    }//  w w  w.  j  a v a2 s.  co m
    ResourceBundle bundle = null;
    if (!format.equals(XML)) {
        return null;
    }

    String bundleName = toBundleName(baseName, locale);
    String resourceName = toResourceName(bundleName, format);
    URL url = loader.getResource(resourceName);
    if (url == null) {
        return null;
    }
    URLConnection connection = url.openConnection();
    if (connection == null) {
        return null;
    }
    if (reload) {
        connection.setUseCaches(false);
    }
    InputStream stream = connection.getInputStream();
    if (stream == null) {
        return null;
    }
    BufferedInputStream bis = new BufferedInputStream(stream);
    bundle = new XMLResourceBundle(bis);
    bis.close();

    return bundle;
}

From source file:edu.ucsb.eucalyptus.admin.server.EucalyptusWebBackendImpl.java

private static String readFileAsString(String filePath) throws java.io.IOException {
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try {//from www.  j a  v  a  2 s.c  o m
        f = new BufferedInputStream(new FileInputStream(filePath));
        f.read(buffer);
    } finally {
        if (f != null)
            try {
                f.close();
            } catch (IOException ignored) {
            }
    }
    return new String(buffer);
}

From source file:com.zzl.zl_app.cache.Utility.java

/**
 * /* ww  w  . j ava2 s . c o m*/
 * @param out
 * @param imgBm
 * @throws Exception
 */
private static void imageContentToUpload(OutputStream out, Bitmap imgBm) throws Exception {
    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);
        imgBm.compress(CompressFormat.JPEG, 75, out);
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new Exception(e);
    } finally {
        if (null != bis) {
            try {
                bis.close();
            } catch (IOException e) {
                throw new Exception(e);
            }
        }
    }
}

From source file:edu.stanford.epad.plugins.qifpwrapper.QIFPHandler.java

public static File generateZipFile(List<File> files, String dirPath) {

    File dir_file = new File(dirPath);
    File zip_file = new File(dirPath + "/temp_" + (fileNum++) + ".zip");
    int dir_l = dir_file.getAbsolutePath().length();

    FileOutputStream fos = null;/*from  w w w  .j  a  v a 2 s  . c o  m*/
    try {
        fos = new FileOutputStream(zip_file);
    } catch (FileNotFoundException e1) {
        log.warning("File not found", e1);
        return null;
    }

    ZipOutputStream zipout = new ZipOutputStream(fos);
    zipout.setLevel(1);
    for (int i = 0; i < files.size(); i++) {
        File f = (File) files.get(i);
        if (f.canRead()) {
            log.info("Adding file: " + f.getAbsolutePath());
            try {
                zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l + 1)));
            } catch (Exception e) {
                log.warning("Error adding to zip file", e);
                return null;
            }
            BufferedInputStream fr;
            try {
                fr = new BufferedInputStream(new FileInputStream(f));

                byte buffer[] = new byte[0xffff];
                int b;
                while ((b = fr.read(buffer)) != -1)
                    zipout.write(buffer, 0, b);

                fr.close();
                zipout.closeEntry();

            } catch (Exception e) {
                log.warning("Error closing zip file", e);
                return null;
            }
        }
    }

    try {
        zipout.finish();
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    log.info("file zipped and returning as " + zip_file.getAbsolutePath());
    return zip_file;

}

From source file:de.tu_dortmund.ub.data.util.TPUUtil.java

public static String writeResultToFile(final CloseableHttpResponse httpResponse, final Properties config,
        final String exportDataModelID, final String fileEnding) throws IOException, TPUException {

    LOG.info("try to write result to file");

    final String persistInFolderString = config.getProperty(TPUStatics.PERSIST_IN_FOLDER_IDENTIFIER);
    final boolean persistInFolder = Boolean.parseBoolean(persistInFolderString);
    final HttpEntity entity = httpResponse.getEntity();

    final String fileName;

    if (persistInFolder) {

        final InputStream responseStream = entity.getContent();
        final BufferedInputStream bis = new BufferedInputStream(responseStream, 1024);

        final String resultsFolder = config.getProperty(TPUStatics.RESULTS_FOLDER_IDENTIFIER);
        fileName = resultsFolder + File.separatorChar + EXPORT_FILE_NAME_PREFIX + exportDataModelID + DOT
                + fileEnding;//from  www.  j a va 2 s .c o m

        LOG.info(String.format("start writing result to file '%s'", fileName));

        final FileOutputStream outputStream = new FileOutputStream(fileName);
        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        IOUtils.copy(bis, bufferedOutputStream);
        bufferedOutputStream.flush();
        outputStream.flush();
        bis.close();
        responseStream.close();
        bufferedOutputStream.close();
        outputStream.close();

        checkResultForError(fileName);
    } else {

        fileName = "[no file name available]";
    }

    EntityUtils.consume(entity);

    return fileName;
}

From source file:XMLResourceBundleControl.java

public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
            boolean reload) throws IllegalAccessException, InstantiationException, IOException {

        if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) {
            throw new NullPointerException();
        }/*  w  w w  .jav  a  2  s  .c om*/
        ResourceBundle bundle = null;
        if (!format.equals(XML)) {
            return null;
        }

        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, format);
        URL url = loader.getResource(resourceName);
        if (url == null) {
            return null;
        }
        URLConnection connection = url.openConnection();
        if (connection == null) {
            return null;
        }
        if (reload) {
            connection.setUseCaches(false);
        }
        InputStream stream = connection.getInputStream();
        if (stream == null) {
            return null;
        }
        BufferedInputStream bis = new BufferedInputStream(stream);
        bundle = new XMLResourceBundle(bis);
        bis.close();

        return bundle;
    }

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws Exception {

    for (File file : folder.listFiles()) {

        if (file.isDirectory()) {
            zos.putNextEntry(new ZipEntry(parentFolder + file.getName() + "/"));
            zos.closeEntry();//from w  ww . ja v  a  2  s  .  co  m
            addFolderToZip(file, parentFolder + file.getName() + "/", zos);
            continue;
        } else {
            zos.putNextEntry(new ZipEntry(parentFolder + file.getName()));
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

            long bytesRead = 0;
            byte[] bytesIn = new byte[1024];
            int read = 0;

            while ((read = bis.read(bytesIn)) != -1) {
                zos.write(bytesIn, 0, read);
                bytesRead += read;
            }

            zos.closeEntry();
            bis.close();
        }
    }
}

From source file:com.jaspersoft.jasperserver.war.OlapGetChart.java

/**
 * Binary streams the specified file to the HTTP response in 1KB chunks
 *
 * @param file The file to be streamed./*ww  w  . j a v  a2s .  c  o  m*/
 * @param response The HTTP response object.
 * @param mimeType The mime type of the file, null allowed.
 *
 * @throws IOException  if there is an I/O problem.
 * @throws FileNotFoundException  if the file is not found.
 */
public static void sendTempFile(File file, HttpServletResponse response, String mimeType)
        throws IOException, FileNotFoundException {

    if (file.exists()) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        //  Set HTTP headers
        if (mimeType != null) {
            response.setHeader("Content-Type", mimeType);
        }
        response.setHeader("Content-Length", String.valueOf(file.length()));
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        response.setHeader("Last-Modified", sdf.format(new Date(file.lastModified())));

        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        byte[] input = new byte[1024];
        boolean eof = false;
        while (!eof) {
            int length = bis.read(input);
            if (length == -1) {
                eof = true;
            } else {
                bos.write(input, 0, length);
            }
        }
        bos.flush();
        bis.close();
        bos.close();
    } else {
        throw new FileNotFoundException(file.getAbsolutePath());
    }
    return;
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public static void unzip(ZipFile zip, ZipEntry entry, File dest) { // convenience method for unzipping from archive
    if (dest.exists())
        dest.delete();//  ww w.  ja  v a  2s  . c o  m
    dest.getParentFile().mkdirs();
    try {
        BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
        int b;
        byte buffer[] = new byte[1024];
        FileOutputStream fOs = new FileOutputStream(dest);
        BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024);
        while ((b = bIs.read(buffer, 0, 1024)) != -1)
            bOs.write(buffer, 0, b);
        bOs.flush();
        bOs.close();
        bIs.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        createExceptionLog(ex);
        progress = "Failed to unzip " + entry.getName();
        fail = "Errors occurred; see log file for details";
        launcher.paintImmediately(0, 0, width, height);
    }
}

From source file:JarMaker.java

/**
 * @param f_name : source zip file path name
 * @param dir_name : target dir file path
 *//*from  ww w  .java  2 s . c om*/
public static void unpackJar(String f_name, String dir_name) throws IOException {

    BufferedInputStream bism = new BufferedInputStream(new FileInputStream(f_name));

    ZipInputStream zism = new ZipInputStream(bism);

    for (ZipEntry z = zism.getNextEntry(); z != null; z = zism.getNextEntry()) {
        File f = new File(dir_name, z.getName());
        if (z.isDirectory()) {
            f.mkdirs();
        } else {
            f.getParentFile().mkdirs();

            BufferedOutputStream bosm = new BufferedOutputStream(new FileOutputStream(f));
            int i;
            byte[] buffer = new byte[1024 * 512];
            try {
                while ((i = zism.read(buffer, 0, buffer.length)) != -1)
                    bosm.write(buffer, 0, i);
            } catch (IOException ie) {
                throw ie;
            }
            bosm.close();
        }
    }
    zism.close();
    bism.close();
}