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:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param basePath//  w  w  w .j a  v  a  2  s. c o m
 * @param zipPath
 * @param filePaths
 * @throws java.io.IOException
 */
public static void zip(File basePath, File zipPath, Map<String, String> filePaths) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = null;
    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(zipPath);
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        //out.setMethod(ZipOutputStream.DEFLATED);
        byte data[] = new byte[BUFFER];
        for (Map.Entry<String, String> file : filePaths.entrySet()) {
            String filename = file.getKey();
            String filePath = file.getValue();
            System.out.println("Adding: " + basePath.getPath() + filePath + " => " + filename);
            File f = new File(basePath, filePath);
            if (f.exists()) {
                FileInputStream fi = new FileInputStream(f);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(filename);
                entry.setCrc(FileUtils.checksumCRC32(new File(basePath, filePath)));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
        }
    } finally {
        if (origin != null)
            origin.close();
        if (out != null)
            out.close();
        if (dest != null)
            dest.close();
    }
}

From source file:Main.java

public static String unzip(String filename) throws IOException {
    BufferedOutputStream origin = null;
    String path = null;//from  w  ww.ja v  a2 s . c  o m
    Integer BUFFER_SIZE = 20480;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String compressedFile = flockedFilesFolder.toString() + "/" + filename;
        System.out.println("compressed File name:" + compressedFile);
        //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip";
        File purgeFile = new File(compressedFile);

        try {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile));
            BufferedInputStream bis = new BufferedInputStream(zin);
            try {
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {
                    byte data[] = new byte[BUFFER_SIZE];
                    path = flockedFilesFolder.toString() + "/" + ze.getName();
                    System.out.println("path is:" + path);
                    if (ze.isDirectory()) {
                        File unzipFile = new File(path);
                        if (!unzipFile.isDirectory()) {
                            unzipFile.mkdirs();
                        }
                    } else {
                        FileOutputStream fout = new FileOutputStream(path, false);
                        origin = new BufferedOutputStream(fout, BUFFER_SIZE);
                        try {
                            /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) {
                            origin.write(c);
                             }
                             */
                            int count;
                            while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                                origin.write(data, 0, count);
                            }
                            zin.closeEntry();
                        } finally {
                            origin.close();
                            fout.close();

                        }
                    }
                }
            } finally {
                bis.close();
                zin.close();
                purgeFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
    return null;
}

From source file:com.isotrol.impe3.connectors.httpclient.AbstractHttpClientConnector.java

private Response http(HttpResponse response, boolean bytes) {
    final StatusLine status = response.getStatusLine();

    final int code = status.getStatusCode();
    final Response.Builder builder = Response.builder();
    builder.setCode(code);//from   w ww  .  ja v a2s .  c o  m

    if (code >= 200 && code < 300) {
        final HttpEntity entity = response.getEntity();

        if (entity != null) {

            //final long length = entity.getContentLength();

            final Header contentType = entity.getContentType();
            if (contentType != null) {
                String value = contentType.getValue();
                if (value != null) {
                    int pos = value.indexOf(";");

                    if (pos > 0) {
                        builder.setType(value.substring(0, pos).trim());
                        String charset = value.substring(pos + 1).trim();
                        builder.setEnc(charset.substring(charset.indexOf("=") + 1).trim());
                    } else {
                        builder.setType(value.trim());
                    }
                }
            }

            if (bytes) {

                try {
                    BufferedInputStream in = new BufferedInputStream(entity.getContent());
                    ByteArrayOutputStream out = new ByteArrayOutputStream();

                    byte buffer[] = new byte[1024];
                    int leidos;
                    while ((leidos = in.read(buffer, 0, 1024)) != -1) {
                        out.write(buffer, 0, leidos);
                    }

                    builder.setContent(out.toByteArray());
                    in.close();
                    out.close();

                } catch (IllegalStateException e) {
                    logger.warn("IllegalStateException");
                    logger.trace("Error trace, ", e);
                } catch (IOException e) {
                    logger.warn("IOException");
                    logger.trace("Error trace, ", e);
                }
            }

            try {
                entity.consumeContent();
            } catch (IOException e) {
                logger.warn("IOException consumeContent");
                logger.trace("Error trace, ", e);
            }
        }
    }

    return builder.get();
}

From source file:com.dimdim.conference.application.portal.PortalServerAdapter.java

/**
 * @param url/*from   www  .  j av  a 2  s . co m*/
 * @param arguments
 * @return
 */
protected synchronized String getURL_String(HttpClient client, String url) {
    GetMethod method = null;
    String responseBody = null;
    try {

        System.out.println("Getting URL:" + url);
        //         System.out.println("Posting data:"+args);
        method = new GetMethod(url);
        //        method.setRequestBody(args);
        method.setFollowRedirects(true);

        //execute the method
        client.setTimeout(2000);
        System.out.println("Calling url:" + url);
        StringBuffer buf = new StringBuffer();
        client.executeMethod(method);
        InputStream is = method.getResponseBodyAsStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        byte[] ary = new byte[256];
        int len = 0;
        while ((len = bis.read(ary, 0, 256)) > 0) {
            String str = new String(ary, 0, len);
            //               System.out.println("Received buffer:"+str);
            buf.append(str);
        }
        try {
            bis.close();
            is.close();
        } catch (Exception e) {
        }
        responseBody = buf.toString();
        System.out.println("Called ----:" + url);
    } catch (HttpException he) {
        he.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    //clean up the connection resources
    try {
        method.releaseConnection();
    } catch (Exception ee) {
        //   Ignore the exceptions in cleanup.
    }
    return responseBody;
}

From source file:com.flagleader.builder.BuilderConfig.java

public static void jarExtracting(String paramString1, String paramString2) {
    int i1 = 2048;
    BufferedOutputStream localBufferedOutputStream = null;
    BufferedInputStream localBufferedInputStream = null;
    JarEntry localJarEntry = null;
    JarFile localJarFile = null;/*from  w ww .  ja va2 s  .c  om*/
    Enumeration localEnumeration = null;
    try {
        localJarFile = new JarFile(paramString2);
        localEnumeration = localJarFile.entries();
        while (localEnumeration.hasMoreElements()) {
            localJarEntry = (JarEntry) localEnumeration.nextElement();
            if (localJarEntry.isDirectory()) {
                new File(paramString1 + localJarEntry.getName()).mkdirs();
            } else {
                localBufferedInputStream = new BufferedInputStream(localJarFile.getInputStream(localJarEntry));
                byte[] arrayOfByte = new byte[i1];
                FileOutputStream localFileOutputStream = new FileOutputStream(
                        paramString1 + localJarEntry.getName());
                localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, i1);
                int i2;
                while ((i2 = localBufferedInputStream.read(arrayOfByte, 0, i1)) != -1)
                    localBufferedOutputStream.write(arrayOfByte, 0, i2);
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
                localBufferedInputStream.close();
            }
        }
    } catch (Exception localException1) {
        localException1.printStackTrace();
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException2) {
            localException2.printStackTrace();
        }
    } finally {
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException3) {
            localException3.printStackTrace();
        }
    }
}

From source file:br.gov.demoiselle.escola.business.AlunoBC.java

public void salvarFoto(Aluno aluno, Foto foto) {
    if (foto != null) {
        try {/*  w  w  w. j a va 2 s .  com*/

            aluno.setFoto(aluno.getId() + "." + FilenameUtils.getExtension(foto.getNome()));
            FacesContext aFacesContext = FacesContext.getCurrentInstance();
            ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();

            String path = context.getRealPath(escolaConfig.getUploadPath() + aluno.getFoto());

            File file = new File(path);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(foto.getInputStream());
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[1024];
                int count;
                while ((count = bufferedInputStream.read(buffer)) > 0)
                    fileOutputStream.write(buffer, 0, count);
            } finally {
                bufferedInputStream.close();
                fileOutputStream.close();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}

From source file:net.sf.jvifm.model.FileModelManager.java

private void putEntry(ZipOutputStream zo, File file) throws Exception {

    String name = file.getPath().substring(prefix.length());

    ZipEntry entry = new ZipEntry(name);
    zo.putNextEntry(entry);//from   ww  w. j  a  va  2  s.c o  m
    BufferedInputStream bi = new BufferedInputStream(new FileInputStream(file));
    while (true) {
        int n = bi.read(buffer);
        if (n < 0)
            break;
        zo.write(buffer, 0, n);
    }
    zo.closeEntry();
    bi.close();

}

From source file:net.sf.jvifm.model.FileModelManager.java

private void putEntry(TarOutputStream to, File file) throws Exception {

    String name = file.getPath().substring(prefix.length());

    TarEntry entry = new TarEntry(file);
    entry.setName(name);//from   w  w  w  .  j  a va 2 s .co  m

    to.putNextEntry(entry);
    BufferedInputStream bi = new BufferedInputStream(new FileInputStream(file));
    while (true) {
        int n = bi.read(buffer);
        if (n < 0)
            break;
        to.write(buffer, 0, n);
    }
    to.closeEntry();
    bi.close();

}

From source file:com.siberhus.web.ckeditor.servlet.OpenFileManagerConnectorServlet.java

private String download(HttpServletRequest request, HttpServletResponse response, String baseDir)
        throws IOException {
    String path = request.getParameter("path");
    File file = new File(baseDir + PathUtils.checkSlashes(path, "L-", false));
    response.setHeader("Content-Type", "application/force-download");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Transfer-Encoding", "Binary");

    OutputStream os = response.getOutputStream();
    byte[] buff = null;
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    try {//from w  w w.j  ava 2 s .  co  m
        buff = new byte[2048];
        int bytesRead = 0;
        while ((bytesRead = bis.read(buff, 0, buff.length)) != -1) {
            os.write(buff, 0, bytesRead);
        }
    } finally {
        bis.close();
        os.flush();
        os.close();
    }
    return null;
}

From source file:com.siberhus.web.ckeditor.servlet.OpenFileManagerConnectorServlet.java

public StreamingResult show(HttpServletRequest request, HttpServletResponse response) throws IOException {

    CkeditorConfig config = CkeditorConfigurationHolder.config();
    String filename = PathUtils.checkSlashes(config.upload().basedir(request), "L+ R+", false)
            + request.getParameter("filepath");
    String ext = FilenameUtils.getExtension(request.getParameter("filepath"));
    String contentType = MimeUtils.getMimeTypeByExt(ext);
    File file = new File(filename);

    response.setHeader("Content-Type", contentType);
    response.setHeader("Content-Length", String.valueOf(file.length()));

    OutputStream os = response.getOutputStream();

    byte[] buff = null;
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    try {//from  w  w w. j ava 2  s .c o  m
        buff = new byte[2048];
        int bytesRead = 0;
        while ((bytesRead = bis.read(buff, 0, buff.length)) != -1) {
            os.write(buff, 0, bytesRead);
        }
    } finally {
        bis.close();
        os.flush();
        os.close();
    }
    return null;
}