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

public static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;//ww  w . ja  v  a2 s .c  o  m
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}

From source file:Main.java

public static String getStringFromInput(InputStream in) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int c = 0;//  w  w w  .j a va2s.c o m
    byte[] buffer = new byte[1024];
    try {
        while ((c = in.read(buffer)) != -1) {
            bos.write(buffer, 0, c);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(bos.toByteArray());
}

From source file:helpers.FileUpload.java

public static boolean processFile(String path, FileItemStream item) {
    try {/*from   ww  w  .ja va  2 s. c  o m*/

        File f = new File(path + File.separator + "assets" + File.separator + "uploads");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println("FileUpload 35: " + e.getMessage());
    }
    return false;
}

From source file:Main.java

public static byte[] readInputStream(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len;/*from w  w  w. jav  a2 s.  co  m*/
    while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();
    baos.close();
    is.close();
    return baos.toByteArray();
}

From source file:Main.java

public static ArrayList<String> getBookmark(String extractPath, String md5) {
    ArrayList<String> listBookMark = new ArrayList<>();
    StringBuilder reval = new StringBuilder();
    try {/* w ww .j av a 2 s  .co m*/
        InputStream in = new FileInputStream(extractPath + "/bookmark_" + md5);
        byte[] buf = new byte[1024];
        int c = 0;
        while ((c = in.read(buf)) >= 0) {
            reval.append(new String(buf, 0, c));
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] arrSite = reval.toString().split(";");
    for (String str : arrSite) {
        if (str.length() > 0) {
            listBookMark.add(str);
        }
    }
    return listBookMark;
}

From source file:editor.util.URLUtil.java

public static String readText(String uri) {
    String res = "";
    try {//from  w  w  w.  j av  a 2 s. com
        java.net.URL url = new java.net.URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        byte buff[] = new byte[200];
        int ch;
        while ((ch = in.read(buff)) != -1)
            res += new String(buff, 0, ch);
        in.close();

        return res;
    } catch (Exception e) {
        return e.getLocalizedMessage();
    }
}

From source file:Main.java

public static String readString(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer)) > 0) {
        bos.write(buffer, 0, len);/*from w ww. j av a 2s .  c  om*/
    }
    bos.close();

    return bos.toString();
}

From source file:Main.java

public static byte[] streamToBytes(InputStream is) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int len;/* w  w w . j a va  2 s. co  m*/
    while ((len = is.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}

From source file:Main.java

public static void copyFromPackage(Context context, int ressourceId, String target) throws IOException {
    FileOutputStream lOutputStream = context.openFileOutput(target, 0);
    InputStream lInputStream = context.getResources().openRawResource(ressourceId);
    int readByte;
    byte[] buff = new byte[8048];
    while ((readByte = lInputStream.read(buff)) != -1) {
        lOutputStream.write(buff, 0, readByte);
    }/*  w  w  w .j a va  2s.  c  om*/
    lOutputStream.flush();
    lOutputStream.close();
    lInputStream.close();
}

From source file:Main.java

public static void transfer(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }/*from   ww w .j a v a 2  s. co  m*/
}