Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

In this page you can find the example usage for java.io BufferedInputStream BufferedInputStream.

Prototype

public BufferedInputStream(InputStream in, int size) 

Source Link

Document

Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Usage

From source file:edu.umd.umiacs.clip.tools.io.GZIPFiles.java

public static List<String> readAllLines(Path path) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            new GZIPInputStream(new BufferedInputStream(newInputStream(path), BUFFER_SIZE)),
            UTF_8.newDecoder().onMalformedInput(IGNORE)))) {
        List<String> result = new ArrayList<>();
        String line;//from  w  w  w  .j av  a2  s.  c o  m
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    }
}

From source file:Main.java

/**
 * Zips a subfolder/*from   w w w.j a  v  a 2  s . c o  m*/
 */
protected static void zipSubFolder(ZipOutputStream zipOut, File srcFolder, int basePathLength)
        throws IOException {
    final int BUFFER = 2048;

    File[] fileList = srcFolder.listFiles();
    BufferedInputStream bis = null;

    for (File file : fileList) {

        if (file.isDirectory()) {
            zipSubFolder(zipOut, file, basePathLength);

        } else {
            byte data[] = new byte[BUFFER];
            String unmodifiedFilePath = file.getPath();
            String relativePath = unmodifiedFilePath.substring(basePathLength);
            Log.d("ZIP SUBFOLDER", "Relative Path : " + relativePath);

            FileInputStream fis = new FileInputStream(unmodifiedFilePath);
            bis = new BufferedInputStream(fis, BUFFER);

            ZipEntry zipEntry = new ZipEntry(relativePath);
            zipOut.putNextEntry(zipEntry);

            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                zipOut.write(data, 0, count);
            }

            bis.close();
        }
    }
}

From source file:edu.umd.umiacs.clip.tools.io.BZIP2Files.java

public static List<String> readAllLines(Path path) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            new BZip2CompressorInputStream(new BufferedInputStream(newInputStream(path), BUFFER_SIZE)),
            UTF_8.newDecoder().onMalformedInput(IGNORE)))) {
        List<String> result = new ArrayList<>();
        String line;/*from  www . j a  va2 s.  co  m*/
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    }
}

From source file:Main.java

/**
 * Zips a file at a location and places the resulting zip file at the toLocation.
 * <p/>//from  w  w  w . j av  a2 s  .  co m
 * Example:
 * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
 * <p/>
 * http://stackoverflow.com/a/14868161
 */
public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    FileOutputStream fos = new FileOutputStream(toLocation);
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

    if (sourceFile.isDirectory()) {
        zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ??

    } else {
        byte data[] = new byte[BUFFER];
        FileInputStream fis = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(fis, BUFFER);

        String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/"));

        ZipEntry zipEntry = new ZipEntry(lastPathComponent);
        zipOut.putNextEntry(zipEntry);

        int count;
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            zipOut.write(data, 0, count);
        }
    }

    zipOut.close();
}

From source file:net.sf.sahi.util.Utils.java

public static byte[] getBytes(final InputStream in, final int contentLength) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(in, BUFFER_SIZE);

    if (contentLength != -1) {
        int totalBytesRead = 0;
        byte[] buffer = new byte[contentLength];
        while (totalBytesRead < contentLength) {
            int bytesRead = -1;
            try {
                bytesRead = bin.read(buffer, totalBytesRead, contentLength - totalBytesRead);
            } catch (EOFException e) {
            }/*  w w w. j  a  va  2  s .c o  m*/
            if (bytesRead == -1) {
                break;
            }
            totalBytesRead += bytesRead;
        }
        return buffer;
    } else {
        ByteArrayOutputStream byteArOut = new ByteArrayOutputStream();
        BufferedOutputStream bout = new BufferedOutputStream(byteArOut);
        try {
            int totalBytesRead = 0;
            byte[] buffer = new byte[BUFFER_SIZE];

            while (true) {
                int bytesRead = -1;
                try {
                    bytesRead = bin.read(buffer);
                } catch (EOFException e) {
                }
                if (bytesRead == -1) {
                    break;
                }
                bout.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            }
        } catch (SocketTimeoutException ste) {
            ste.printStackTrace();
        }
        bout.flush();
        bout.close();
        return byteArOut.toByteArray();
    }
}

From source file:com.pickr.utils.BitmapUtils.java

private static Bitmap loadBitmap(URL url) throws IOException {
    InputStream is = null;/*from w w w. j  ava 2 s  . co  m*/
    try {
        is = new BufferedInputStream(url.openStream(), 512000);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;

        return BitmapFactory.decodeStream(is, null, options);

    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

/**
 * Compress files to a zip/*from w  ww.java2  s .  c o  m*/
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:edu.umd.umiacs.clip.tools.io.AllFiles.java

private static List<String> _readAllLines(Path path) throws IOException {
    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(new BufferedInputStream(newInputStream(path), BUFFER_SIZE),
                    UTF_8.newDecoder().onMalformedInput(IGNORE)))) {
        List<String> result = new ArrayList<>();
        String line;/*from   www . java  2 s .c  o  m*/
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    }
}

From source file:Main.java

/**
 * A safer decodeStream method/* ww w .  j av  a2  s  .c  om*/
 * rather than the one of {@link BitmapFactory} which will be easy to get OutOfMemory Exception
 * while loading a big image file.
 * 
 * @param uri
 * @param width
 * @param height
 * @return
 * @throws FileNotFoundException
 */
protected static Bitmap safeDecodeStream(Context context, Uri uri, int width, int height)
        throws FileNotFoundException {
    int scale = 1;
    // Decode image size without loading all data into memory  
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    android.content.ContentResolver resolver = context.getContentResolver();
    try {

        BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024), null,
                options);
        if (width > 0 || height > 0) {
            options.inJustDecodeBounds = true;
            int w = options.outWidth;
            int h = options.outHeight;
            while (true) {
                if ((width > 0 && w / 2 < width) || (height > 0 && h / 2 < height)) {
                    break;
                }
                w /= 2;
                h /= 2;
                scale *= 2;
            }
        }
        // Decode with inSampleSize option  
        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
        return BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024),
                null, options);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }
    return null;
}

From source file:io.github.runassudo.gtfs.FlatGTFSCSV.java

public BufferedReader getBufferedReader() throws IOException {
    //return new BufferedReader(new FileReader(file));
    return new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(file), 2048)));
}