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 int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

/*****************************************************
 *
 * Returns the bytes from an input stream, or an exception
 * if there was an error.//from w  w w . j av a 2s. c om
 *
 *****************************************************/
private static Object getBytes(BufferedInputStream bis) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bis.available());

    byte[] buffer = new byte[READ_BUFFER_SIZE_IN_BYTES];

    int byteCount = -1;

    while ((byteCount = bis.read(buffer)) >= 0) {
        baos.write(buffer, 0, byteCount);
    }

    return (baos.toByteArray());
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * //w w  w . j a  va 2s.  c o m
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:Main.java

public static byte[] loadImageDataFromWeb(String url) {

    byte[] imageData = null;
    try {//from   www. ja  v a 2  s  .  c o m

        URLConnection connection = new URL(url).openConnection();
        InputStream stream = connection.getInputStream();
        //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer
        BufferedInputStream in = new BufferedInputStream(stream, 10240);//YG: 10k=10240, 2x8k=16384
        ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
        int read;
        byte[] b = new byte[4096];

        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }

        out.flush();
        out.close();

        imageData = out.toByteArray();

    } catch (Exception e) {

        Log.w(TAG, "Exc=" + e);
        return null;
    }

    return imageData;
}

From source file:Main.java

/** Downloads a file from the specified URL and stores the file to the specified location 
 * @param fileUrl the URL from which the file should be downloaded
 * @param storageLocation the location to which the downloaded file should be stored. If the file exists, it will
 * be overridden! /*from   www. java2s .  c o m*/
 * @throws IOException */
public static void downloadFileFromWebserver(String fileUrl, String storageLocation) throws IOException {
    URL url = new URL(fileUrl);
    File file = new File(storageLocation);

    URLConnection urlConnection = url.openConnection();
    InputStream inputStream = urlConnection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    byte[] buffer = new byte[1024];
    int bytesInBuffer = 0;
    while ((bytesInBuffer = bufferedInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, bytesInBuffer);
    }

    fileOutputStream.close();
}

From source file:Main.java

/**
 * Method to save audio file to SD card//  w  w w. ja  v a2 s.c  o  m
 * @param course_title, title of course
 * @param format, the file format, for example .mp3, .txt
 */
public static boolean copyToStorage(File source_file, String course_title, String format) {

    File output_file = getEmptyFileWithStructuredPath(course_title, format);

    try {
        byte[] buffer = new byte[4096];
        int bytesToHold;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source_file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output_file));

        while ((bytesToHold = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesToHold);
        }

        bos.flush();
        bos.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static Bitmap loadImageFromUrl(String url) {
    ByteArrayOutputStream out = null;
    Bitmap bitmap = null;//from w  w w  .j av a 2 s .  c  o m
    int BUFFER_SIZE = 1024 * 8;
    try {
        BufferedInputStream in = new BufferedInputStream(new URL(url).openStream(), BUFFER_SIZE);
        out = new ByteArrayOutputStream(BUFFER_SIZE);
        int length = 0;
        byte[] tem = new byte[BUFFER_SIZE];
        length = in.read(tem);
        while (length != -1) {
            out.write(tem, 0, length);
            out.flush();
            length = in.read(tem);
        }
        in.close();
        if (out.toByteArray().length != 0) {
            bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
        } else {
            out.close();
            return null;
        }
        out.close();
    } catch (OutOfMemoryError e) {

        out.reset();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2;
        opts.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size(), opts);
        return bitmap;
    } catch (Exception e) {

        return bitmap;
    }
    return bitmap;
}

From source file:com.cloudera.recordbreaker.analyzer.UnknownTextDataDescriptor.java

public static boolean isTextData(FileSystem fs, Path p) {
    try {/* w  ww . j  a v a 2  s  . c  o  m*/
        BufferedInputStream in = new BufferedInputStream(fs.open(p));
        try {
            byte buf[] = new byte[1024];
            int numBytes = in.read(buf);
            if (numBytes < 0) {
                return false;
            }
            int numASCIIChars = 0;
            for (int i = 0; i < numBytes; i++) {
                if (buf[i] >= 32 && buf[i] < 128) {
                    numASCIIChars++;
                }
            }
            return ((numASCIIChars / (1.0 * numBytes)) > asciiThreshold);
        } finally {
            in.close();
        }
    } catch (IOException iex) {
        return false;
    }
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;//from w w w  .j  av a 2  s.c  om
    try {
        i = new FileInputStream(src);
        BufferedInputStream in = new BufferedInputStream(i);
        FileOutputStream o = new FileOutputStream(dst);
        BufferedOutputStream out = new BufferedOutputStream(o);
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        in.close();
        out.close();
        o.close();
        i.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;/*  www.j  a  va2 s  .  c  o  m*/
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;/*from w  w w .  ja v a  2  s.c  om*/
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}