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

public static String readFromFile(String file) {
    try {/* w w w.j av  a 2 s.  c  om*/
        File f = new File(file);

        if (!f.exists() || !f.canRead()) {
            return null;
        }

        BufferedInputStream ipt = new BufferedInputStream(new FileInputStream(f));

        byte[] buf = new byte[512];
        StringBuilder sb = new StringBuilder();

        while (ipt.available() > 0) {
            int len = ipt.read(buf, 0, 512);
            sb.append(new String(buf, 0, len, "UTF-8"));
        }

        ipt.close();
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:ConsoleInput.java

public static String readLine() {
    StringBuffer response = new StringBuffer();
    try {/* www  .  j  av a  2  s  .  c o  m*/
        BufferedInputStream buff = new BufferedInputStream(System.in);
        int in = 0;
        char inChar;
        do {
            in = buff.read();
            inChar = (char) in;
            if ((in != -1) & (in != '\n') & (in != '\r')) {
                response.append(inChar);
            }
        } while ((in != -1) & (inChar != '\n') & (in != '\r'));
        buff.close();
        return response.toString();
    } catch (IOException e) {
        System.out.println("Exception: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);/* w w  w  .  ja va2s  . c o m*/
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

public static long downloadFileFromUrl(String urlPath, File file) {
    long size = 0;
    try {/*from  w w  w .j av  a2s.c o  m*/
        URL url = new URL(urlPath);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream());
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
        int i;
        while ((i = bufferedinputstream.read()) != -1) {
            bufferedoutputstream.write(i);
        }
        bufferedinputstream.close();
        bufferedoutputstream.close();
        httpurlconnection.disconnect();
        size = file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}

From source file:eu.aniketos.ncvm.impl.EncodeSupport.java

/**
 * This methods read a file into a string.
 * //  w ww  . j a v a 2 s  .c o  m
 * @param filePath
 *            Path of the text file that should be read.
 * @return File content as string.
 */
static String readFileAsString(String filePath) throws java.io.IOException {
    int fileSize = (int) new File(filePath).length();
    byte[] buffer = new byte[fileSize];
    BufferedInputStream f = null;
    try {
        FileInputStream inputStream = new FileInputStream(filePath);
        f = new BufferedInputStream(inputStream);
        f.read(buffer);
    } finally {
        if (f != null)
            try {
                f.close();
            } catch (IOException ignored) {
            }
    }
    return new String(buffer);
}

From source file:Main.java

public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException {
    URL url = new URL(imageUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agent",
            System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK");

    // determine the image size and allocate a buffer
    int fileSize = connection.getContentLength();
    byte[] imageData = new byte[fileSize];

    // download the file
    Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")");

    if (fileSize > 0) {

        BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
        int bytesRead = 0;
        int offset = 0;
        while (bytesRead != -1 && offset < fileSize) {
            bytesRead = istream.read(imageData, offset, fileSize - offset);
            offset += bytesRead;/*from  w  ww .j ava2 s .c  om*/
        }

        istream.close();
    } else
        Log.d(TAG, "fileSize is 0! skipping");

    // clean up
    connection.disconnect();

    return imageData;
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Reads a file.//w w  w . jav  a  2  s. com
 *
 * @param filePath The file path.
 * @return a byte[] The file data.
 * @throws java.io.IOException if an error occurs reading the file or if the file does not exists.
 */
public static byte[] readFile(String filePath) throws IOException {
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try {
        f = new BufferedInputStream(new FileInputStream(filePath));
        f.read(buffer);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ignored) {
            }
        }
    }
    return buffer;
}

From source file:Main.java

private static void compressFile(File file, ZipOutputStream out, String basedir) {
    if (!file.exists()) {
        return;/*from w  w w .  j av  a2 s  . c o m*/
    }
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(basedir + file.getName());
        out.putNextEntry(entry);
        int count;
        byte data[] = new byte[BUFFER];
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        bis.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:jeevlet.representation.BlobRepresentation.java

private static void copy(InputStream in, OutputStream output) throws IOException {
    BufferedInputStream input = new BufferedInputStream(in);
    try {/* w  w w.j av a2 s  .  com*/
        byte buffer[] = new byte[BUF_SIZE];
        int nRead;
        do {
            nRead = input.read(buffer, 0, BUF_SIZE);
            output.write(buffer, 0, nRead);

        } while (nRead == BUF_SIZE);
        input.close();
    } catch (IOException e) {
        input.close();
        throw e;
    }
    output.flush();
}

From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java

public static File buildArchive(File archive, File... files) throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(archive);
    ZipOutputStream zos = new ZipOutputStream(fos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    for (File file : files) {
        if (!file.exists()) {
            System.err.println("Skipping: " + file);
            continue;
        }//w  ww.j a  va2 s  .  co m
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            crc.reset();
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }

            bis.close();

            // Reset to beginning of input stream
            bis = new BufferedInputStream(new FileInputStream(file));
            String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);

            ZipEntry entry = new ZipEntry(entryPath);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(file.length());
            entry.setSize(file.length());
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
    IOUtils.closeQuietly(zos);
    return archive;
}