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 long copyLarge(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[4096];
    long count = 0L;
    int n = 0;//from ww  w.j  av a 2 s .c  o m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:com.nuvolect.deepdive.util.FileUtil.java

public static String readFile(File file) {

    String fileContents = "";
    StringBuilder sb = new StringBuilder();

    try {/*from  w w  w  .  ja  v a2s .  c o  m*/
        InputStream is = new FileInputStream(file);

        byte[] buffer = new byte[4096];
        int len;
        while ((len = is.read(buffer)) > 0) {

            String s = new String(buffer, 0, len, "UTF-8");
            sb.append(s);
        }
        fileContents = sb.toString();

        if (is != null)
            is.close();
    } catch (FileNotFoundException e) {
        LogUtil.logException(FileUtil.class, e);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return fileContents;
}

From source file:Main.java

public static byte[] getBytesFromStream(InputStream inputStream) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;/*from   ww w .  ja v  a  2 s  .  c  om*/
    try {
        while ((len = inputStream.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return os.toByteArray();
}

From source file:Main.java

public static String readInputStream(InputStream is) {
    try {//from www  .j  a v  a  2  s.c  om
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
        baos.close();
        byte[] result = baos.toByteArray();
        String temp = new String(result);
        return temp;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void copy(InputStream in, OutputStream out) {
    final byte[] buf = new byte[1024];
    int len;/* w  ww  . ja va  2  s. co m*/
    try {
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    closeQuietly(in);
    closeQuietly(out);
}

From source file:Main.java

public static int CopyFile(String fromFile, String toFile) {
    try {/*from w  w w.  j  a va 2s . co m*/
        InputStream fosfrom = new FileInputStream(fromFile);
        OutputStream fosto = new FileOutputStream(toFile);
        byte bt[] = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();
        return 0;

    } catch (Exception ex) {
        return -1;
    }
}

From source file:Main.java

public static void copyfile(File source, File dest) {
    try {/* w w  w .java2s. com*/
        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
}

From source file:com.eucalyptus.auth.euare.ldap.LicParserTest.java

private static String readInputAsString(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[512];
    int nRead = 0;
    while ((nRead = in.read(buf)) >= 0) {
        baos.write(buf, 0, nRead);/*from w ww  .j a v a2 s .co m*/
    }

    return new String(baos.toByteArray(), "UTF-8");
}

From source file:Main.java

public static void transfer(InputStream in, OutputStream out, int maxBytes) throws IOException {
    byte[] buf = new byte[maxBytes];
    int done = 0;
    int len;/* w  w  w.j  a  va2  s .c  o  m*/
    while (done < maxBytes && (len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
        done += len;
    }
}

From source file:Main.java

/**
 * Copies the contents of one stream to the other.
 * @param in not null//from   w ww  .java 2 s  . c om
 * @param out not null
 * @throws IOException
 */
public static void copy(final InputStream in, final OutputStream out) throws IOException {
    final byte[] buffer = new byte[DEFAULT_ENCODING_BUFFER_SIZE];
    int inputLength;
    while (-1 != (inputLength = in.read(buffer))) {
        out.write(buffer, 0, inputLength);
    }
}