Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

private static void copyFile2(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;/*from w w  w .  j av a  2s  . c  o m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

public static long copyLarge(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    long count = 0;
    int n = 0;//from  ww  w . j  ava  2  s .  co  m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

/**
 * Copies all of the bytes from {@code in} to {@code out}. Neither stream is closed.
 * Returns the total number of bytes transferred.
 *///from  www  . j a va2 s  .  c o m
public static int copy(InputStream in, OutputStream out) throws IOException {
    int total = 0;
    byte[] buffer = new byte[8192];
    int c;
    while ((c = in.read(buffer)) != -1) {
        total += c;
        out.write(buffer, 0, c);
    }
    return total;
}

From source file:Main.java

private static int copy(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    int count = 0;
    int n = 0;/*ww w. ja  v a 2s . com*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static boolean isImage(Blob blob) throws Exception {
    InputStream in = null;
    byte[] bytes = new byte[8];
    try {//from w  w w.ja va  2  s.c o m
        in = blob.getBinaryStream();

        in.read(bytes);
        return isImage(bytes);
    } catch (Exception ex) {
        throw new Exception(ex.toString());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                throw new Exception(ex.toString());
            }
        }
        bytes = null;
    }
}

From source file:Main.java

public static boolean copyStream(InputStream src, OutputStream dest) {
    byte[] buffer = new byte[COPYSTREAM_BUFFER_SIZE];

    try {//  w w w . j a v a  2s  . c o  m
        int size;

        while ((size = src.read(buffer)) != -1) {
            dest.write(buffer, 0, size);
        }
    } catch (IOException e) {
        return false;
    }

    return true;
}

From source file:com.o2d.pkayjava.editor.utils.Overlap2DUtils.java

private static String getMyDocumentsLocation() {
    String myDocuments = null;//from   w ww. j  a v a2  s. c om
    try {
        if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }
        if (SystemUtils.IS_OS_WINDOWS) {
            Process p = Runtime.getRuntime().exec(
                    "reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal");
            p.waitFor();

            InputStream in = p.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            in.close();

            myDocuments = new String(b);
            myDocuments = myDocuments.split("\\s\\s+")[4];
        }
        if (SystemUtils.IS_OS_LINUX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }

    } catch (Throwable t) {
        t.printStackTrace();
    }

    return myDocuments;
}

From source file:Utils.java

public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int len = in.read(buffer);
    while (len >= 0) {
        out.write(buffer, 0, len);//from  www . ja  v a2 s .  co  m
        len = in.read(buffer);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void copyDB(Context context, String fileName) throws IOException {
    String filePath = context.getFilesDir().getAbsolutePath() + "/" + fileName;
    if (new File(filePath).exists()) {
        return;/*from w w w. ja v  a  2 s. c  om*/
    }
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    InputStream is = context.getResources().getAssets().open(fileName);
    byte[] buffer = new byte[1024 * 500];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
        fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
}

From source file:ch.ethz.coss.nervousnet.hub.ui.ShowcaseActivity.java

public static String parseJSONFile(String res, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(res);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);
    file.close();//from   w  w  w. j  a v  a 2s  .co m

    return new String(formArray);
}