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:com.thejustdo.util.Utils.java

/**
 * Creates a ZIP file containing all the files inside a directory.
 * @param location Directory to read./*from  w w w  .  j  av  a  2  s  . c  om*/
 * @param pathname Name and path where to store the file.
 * @throws FileNotFoundException If can't find the initial directory.
 * @throws IOException If can't read/write.
 */
public static void zipDirectory(File location, File pathname) throws FileNotFoundException, IOException {

    BufferedInputStream origin;
    FileOutputStream dest = new FileOutputStream(pathname);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    byte data[] = new byte[2048];

    // 1. Get a list of files from current directory
    String files[] = location.list();
    File f;

    // 2. Adding each file to the zip-set.
    for (String s : files) {
        log.info(String.format("Adding: %s", s));
        f = new File(location, s);
        FileInputStream fi = new FileInputStream(f);
        origin = new BufferedInputStream(fi, 2048);
        ZipEntry entry = new ZipEntry(location.getName() + File.separator + s);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, 2048)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();
    }
    out.close();
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static void sendFile(DataSource ds, HttpServletResponse response) {
    response.setContentType(ds.getContentType());

    if (ds.getName() != null && ds.getName().length() > 0) {
        response.addHeader("Content-Disposition", "attachment; filename=" + ds.getName());
    }/*from   w  ww  .  ja va  2  s. c  o m*/

    OutputStream outputStream = null;
    BufferedInputStream bufferedInputStream = null;
    try {
        outputStream = response.getOutputStream();

        bufferedInputStream = new BufferedInputStream(ds.getInputStream());
        int readBytes = 0;

        while ((readBytes = bufferedInputStream.read()) != -1) {
            outputStream.write(readBytes);
        }

        if (log.isDebugEnabled()) {
            log.debug("finished sending bytes");
        }

    } catch (IOException ex) {
        log.error("Error serving a file: " + ex.getMessage(), ex);
    } finally {
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (Exception ex) {
            }
        if (bufferedInputStream != null)
            try {
                bufferedInputStream.close();
            } catch (Exception ex) {
            }
    }
}

From source file:com.haoqee.chatsdk.net.Utility.java

/**
 * Upload file into output stream ./* w  ww  . j  a  va  2 s .c  o m*/
 * 
 * @param out
 *            : output stream for uploading
 * @param file
 *            : file for uploading
 * @param filekey
 *              :uploaded files' key;          
 * @return void
 */
private static void fileContentToUpload(OutputStream out, TCMorePicture filePath) throws HaoqeeChatException {
    StringBuilder temp = new StringBuilder();
    File file = new File(filePath.filePath);
    temp.append(MP_BOUNDARY).append("\r\n");
    temp.append(
            "Content-Disposition: form-data; name=\"" + filePath.key + "\"; filename=\"" + file.getName() + "")
            .append("").append("\"\r\n");
    byte[] fileData = getFileByte(file);
    String filetype = "multipart/form-data";

    temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    BufferedInputStream bis = null;
    try {
        out.write(res);
        out.write(fileData);
        //imgpath.compress(CompressFormat.PNG, 75, out);
        out.write("\r\n".getBytes());
        //out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new HaoqeeChatException(e);
    } finally {
        if (null != bis) {
            try {
                bis.close();
            } catch (IOException e) {
                throw new HaoqeeChatException(e);
            }
        }
    }
}

From source file:com.sun.faces.generate.RenderKitSpecificationGenerator.java

public static void copyResourceToFile(String resourceName, File file) throws Exception {
    FileOutputStream fos = null;/*  w  w w.j  a va2 s.c  om*/
    BufferedInputStream bis = null;
    URL url = null;
    URLConnection conn = null;
    byte[] bytes = new byte[1024];
    int len = 0;

    fos = new FileOutputStream(file);
    url = getCurrentLoader(fos).getResource(resourceName);
    conn = url.openConnection();
    conn.setUseCaches(false);
    bis = new BufferedInputStream(conn.getInputStream());
    while (-1 != (len = bis.read(bytes, 0, 1024))) {
        fos.write(bytes, 0, len);
    }
    fos.close();
    bis.close();

}

From source file:de.betterform.xml.xforms.XFormsProcessorImpl.java

/**
 * Returns betterForm version string.// ww w  . j  a v a2  s  .  com
 *
 * @return betterForm version string.
 */
public static String getAppInfo() {
    synchronized (XFormsProcessorImpl.class) {
        if (APP_INFO == null) {
            try {
                BufferedInputStream stream = new BufferedInputStream(
                        XFormsProcessorImpl.class.getResourceAsStream("/META-INF/version.info"));
                StringBuffer buffer = new StringBuffer();
                int c;

                while ((c = stream.read()) > -1) {
                    if (c != 10 && c != 13) {
                        buffer.append((char) c);
                    }
                }
                stream.close();
                APP_INFO = buffer.toString();
            } catch (IOException e) {
                APP_INFO = "betterFORM";
            }
        }
        return APP_INFO;
    }
}

From source file:com.sun.socialsite.util.Utilities.java

/**
 * Utility method to copy an input stream to an output stream.
 * Wraps both streams in buffers. Ensures right numbers of bytes copied.
 *///ww  w  .  j av a 2s  .c  o m
public static void copyInputToOutput(InputStream input, OutputStream output, long byteCount)
        throws IOException {
    int bytes;
    long length;

    BufferedInputStream in = new BufferedInputStream(input);
    BufferedOutputStream out = new BufferedOutputStream(output);

    byte[] buffer;
    buffer = new byte[8192];

    for (length = byteCount; length > 0;) {
        bytes = (int) (length > 8192 ? 8192 : length);

        try {
            bytes = in.read(buffer, 0, bytes);
        } catch (IOException ex) {
            try {
                in.close();
                out.close();
            } catch (IOException ex1) {
            }
            throw new IOException("Reading input stream, " + ex.getMessage());
        }

        if (bytes < 0)
            break;

        length -= bytes;

        try {
            out.write(buffer, 0, bytes);
        } catch (IOException ex) {
            try {
                in.close();
                out.close();
            } catch (IOException ex1) {
            }
            throw new IOException("Writing output stream, " + ex.getMessage());
        }
    }

    try {
        in.close();
        out.close();
    } catch (IOException ex) {
        throw new IOException("Closing file streams, " + ex.getMessage());
    }
}

From source file:com.macdonst.ftpclient.FtpClient.java

/**
 * Uploads a file to a ftp server./*from  w w  w.j a v  a2s .c om*/
 * @param filename the name of the local file to send to the server
 * @param url the url of the server
 * @throws IOException
 */
private void put(String filename, URL url) throws IOException {

    BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(new File(filename)));
    FTPClient f = setup(url);
    f.storeFile(extractFileName(url), buffIn);
    buffIn.close();

    teardown(f);
}

From source file:jm.web.Ftp.java

public boolean subirArchivo(String rutaArchivo, String rutaArchivoRemoto) {
    try {/*  www .  jav  a 2  s.  c om*/
        BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(rutaArchivo));//Ruta del archivo para enviar
        this.ftp.enterLocalPassiveMode();
        this.ftp.storeFile(rutaArchivoRemoto, buffIn);//Ruta completa de alojamiento en el FTP
        buffIn.close(); //Cerrar envio de arctivos al FTP
        return true;
    } catch (Exception e) {
        this.error = e.getMessage();
    }
    return false;
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * <p><em>Title: Save InputSream to an temporary File</em></p>
 * <p>Description: </p>/*from   ww w .  java  2 s.  c om*/
 * 
 * @return 
 */
public static void saveInputStreamToTempFile(InputStream is, String fileName) {

    File outputFile = new File(Configuration.getTempDirPath() + fileName);
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile);
        bos = new BufferedOutputStream(fos);
        int i = -1;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.flush();
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }

    }

}

From source file:com.clarionmedia.infinitum.internal.caching.RestResponseCache.java

@Override
protected RestResponse readValueFromDisk(File file) throws IOException {
    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    long fileSize = file.length();
    if (fileSize > Integer.MAX_VALUE) {
        inputStream.close();
        throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes.");
    }/*from  ww w .j  a v  a2 s .  c  o  m*/

    // The first byte is the status code
    int statusCode = inputStream.read();

    // The second byte is the size of the header data
    int headerSize = inputStream.read();

    // Next is the header data itself
    byte[] headerData = new byte[headerSize];
    inputStream.read(headerData, 0, headerSize);

    // The remainder is the message data
    int messageDataSize = (int) fileSize - headerSize - 2;
    byte[] messageData = new byte[messageDataSize];
    inputStream.read(messageData, 0, messageDataSize);
    inputStream.close();

    RestResponse response = new RestResponse();
    response.setStatusCode(statusCode);
    response.setResponseData(messageData);
    String headers = new String(headerData);
    Map<String, String> headerMap = new HashMap<String, String>();
    String[] values = headers.split("\n");
    for (int i = 0; i < values.length;)
        headerMap.put(values[i++], values[i++]);
    response.setHeaders(headerMap);
    return response;
}