Example usage for java.io BufferedInputStream read

List of usage examples for java.io BufferedInputStream read

Introduction

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

Prototype

public synchronized int read() throws IOException 

Source Link

Document

See the general contract of the read method of InputStream.

Usage

From source file:ca.simplegames.micro.utils.IO.java

/**
 * Read the data from the given file into a byte array and return
 * the array.//  w w  w .ja  v a  2  s.c om
 *
 * @param file The file
 * @return The byte array
 * @throws IOException
 */

public static byte[] readData(File file) throws IOException {
    BufferedInputStream in = null;
    ByteArrayOutputStream out = null;

    try {
        in = new BufferedInputStream(new FileInputStream(file));
        out = new ByteArrayOutputStream();

        int c = -1;
        while ((c = in.read()) != -1) {
            out.write(c);
        }

        return out.toByteArray();
    } finally {
        IO.close(in);
        IO.close(out);
    }
}

From source file:bala.padio.Settings.java

public static boolean DownloadFile(String httpUrl, String filePath) {
    OutputStream outputStream;/*  w  ww  .  java  2 s  . c o  m*/
    try {
        Log.d(TAG, "Downloading url: " + httpUrl + ", to file: " + filePath);
        URL url = new URL(httpUrl);
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        // file to save
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int current = 0;
        while ((current = bis.read()) != -1) {
            outputStream.write((byte) current);
        }
        is.close();
        outputStream.close();

        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:com.github.pascalgn.jiracli.web.HttpClient.java

private static InputStream maybeDecompress(InputStream input) throws IOException {
    // Due to a bug, Jira sometimes returns double-compressed responses. See JRA-37608
    BufferedInputStream buffered = new BufferedInputStream(input, 2);
    buffered.mark(2);/*w  w w  .  j  av a2s. co m*/
    int[] buf = new int[2];
    buf[0] = buffered.read();
    buf[1] = buffered.read();
    buffered.reset();
    int header = (buf[1] << 8) | buf[0];
    if (header == GZIPInputStream.GZIP_MAGIC) {
        return new GZIPInputStream(buffered);
    } else {
        return buffered;
    }
}

From source file:com.example.shutapp.DatabaseHandler.java

private static boolean downloadDatabase(Context context) {
    try {/* ww w. j  a v a2s. co m*/
        Log.d(TAG, "downloading database");
        URL url = new URL(StringLiterals.SERVER_DB_URL);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(StringLiterals.DATABASE_BUFFER);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = null;
        // Select storage location
        fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE);

        fos.write(baf.toByteArray());
        fos.close();
        Log.d(TAG, "downloaded");
    } catch (IOException e) {
        String exception = e.toString();
        Log.e(TAG, "downloadDatabase Error: " + exception);
        return false;
    } catch (Exception e) {
        String exception = e.toString();
        Log.e(TAG, "downloadDatabase Error: " + exception);
        return false;
    }
    return true;
}

From source file:org.jared.synodroid.ds.utils.Utils.java

public static Uri moveToStorage(Activity a, Uri uri) {
    ContentResolver cr = a.getContentResolver();
    try {//from   w ww.  j  a va2 s.  com
        InputStream is = cr.openInputStream(uri);

        File path = Environment.getExternalStorageDirectory();
        path = new File(path, "Android/data/org.jared.synodroid.ds/cache/");
        path.mkdirs();

        String fname = getContentName(cr, uri);
        File file = null;
        if (fname != null) {
            file = new File(path, fname);
        } else {
            file = new File(path, "attachment.att");
        }

        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();

        return uri = Uri.fromFile(file);
    } catch (FileNotFoundException e) {
        // do nothing
    } catch (IOException e) {
        // do nothing
    }

    return null;
}

From source file:pl.lewica.util.FileUtil.java

public static boolean fetchAndSaveImage(String sourceUrl, String destinationPath, boolean overwrite)
        throws IOException {
    File destinationFile = new File(destinationPath);
    if (destinationFile.exists() && !overwrite) {
        return false;
    }/*from  ww w  . ja v a  2  s.  com*/

    BufferedInputStream bis = null;
    FileOutputStream fos = null;

    try {
        URL url = new URL(sourceUrl);
        // See http://stackoverflow.com/questions/3498643/dalvik-message-default-buffer-size-used-in-bufferedinputstream-constructor-it/7516554#7516554
        int bufferSize = 8192;
        bis = new BufferedInputStream(url.openStream(), bufferSize);
        ByteArrayBuffer bab = new ByteArrayBuffer(50);

        int current = 0;
        while ((current = bis.read()) != -1) {
            bab.append((byte) current);
        }

        fos = new FileOutputStream(destinationFile);
        fos.write(bab.toByteArray());
        fos.close();
    } catch (MalformedURLException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioe) {
                // This issue can be safely skipped but there's no harm in logging it
                Log.w("FileUtil", "Unable to close file output stream for " + sourceUrl);
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
                // This issue can be safely skipped but there's no harm in logging it
                Log.w("FileUtil", "Unable to close buffered input stream for " + sourceUrl);
            }
        }
    }

    return true;
}

From source file:net.sf.jasperreports.eclipse.util.FileUtils.java

public static String readInputStreamAsString(InputStream in) throws IOException {

    BufferedInputStream bis = new BufferedInputStream(in);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while (result != -1) {
        byte b = (byte) result;
        buf.write(b);/*from  www .ja  v  a  2 s.co m*/
        result = bis.read();
    }
    return buf.toString();
}

From source file:fr.paris.lutece.plugins.updater.service.UpdateService.java

/**
 * Copies data from an input stream to an output stream.
 * @param inStream The input stream/*w ww.  j a  va 2  s.com*/
 * @param outStream The output stream
 * @throws IOException If an I/O error occurs
 */
private static void copyStream(InputStream inStream, OutputStream outStream) throws IOException {
    BufferedInputStream inBufferedStream = new BufferedInputStream(inStream);
    BufferedOutputStream outBufferedStream = new BufferedOutputStream(outStream);

    int nByte;

    while ((nByte = inBufferedStream.read()) > -1) {
        outBufferedStream.write(nByte);
    }

    outBufferedStream.close();
    inBufferedStream.close();
}

From source file:com.sangupta.clitools.file.HexDump.java

public static void hexDump(PrintStream outStream, BufferedInputStream bis, int currentRow, int maxRows)
        throws IOException {
    int row = currentRow + 1;
    if (maxRows == 0) {
        maxRows = Integer.MAX_VALUE;
    } else {/*from   w  w w.j a  va 2s.  c  om*/
        maxRows += currentRow;
    }

    StringBuilder builder1 = new StringBuilder(100);
    StringBuilder builder2 = new StringBuilder(100);

    while (bis.available() > 0) {
        outStream.printf("%04X  ", row * 16);
        for (int j = 0; j < 16; j++) {
            if (bis.available() > 0) {
                int value = (int) bis.read();
                builder1.append(String.format("%02X ", value));

                if (!Character.isISOControl(value)) {
                    builder2.append((char) value);
                } else {
                    builder2.append(".");
                }
            } else {
                for (; j < 16; j++) {
                    builder1.append("   ");
                }
            }
        }
        outStream.print(builder1);
        outStream.println(builder2);
        row++;

        if (row > maxRows) {
            break;
        }

        builder1.setLength(0);
        builder2.setLength(0);
    }
}

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   w w w  .  j  a va2  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);
            }
        }

    }

}