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 String readAsset(Context context, String assetPath) throws IOException {
    String asset = null;// ww w.j  a v  a 2 s . c  o m
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(assetPath);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        is.close();
        asset = new String(data, "ASCII");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return asset;
}

From source file:Main.java

public static String inputStream2String(InputStream in) {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    try {//from  w w  w.j a  v  a2  s.  c om
        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toString();
}

From source file:Main.java

private static byte[] readSmallerStream(final InputStream in, final int streamlength) throws IOException {
    byte[] bytes = new byte[streamlength];
    int readed = in.read(bytes);
    int patchsize = 1;
    while (patchsize > 0 && readed != streamlength) {
        patchsize = in.read(bytes, readed, streamlength - readed);
        if (patchsize > 0) {
            readed += patchsize;//from  w ww. j  a  v  a 2 s. c o m
        }
    }
    if (readed != streamlength) {
        throw new IOException("InputStream(" + in + ", streamlength=" + streamlength + ", readedlength="
                + readed + ") read fail");
    }
    return bytes;
}

From source file:Main.java

public static void copy(InputStream in, OutputStream out) throws Exception {
    byte[] bucket = new byte[8 * 1024];
    int bytesRead = 0;
    while ((bytesRead = in.read(bucket)) != -1) {
        out.write(bucket, 0, bytesRead);
    }/*from   w  ww .jav a 2s.  c  o  m*/
}

From source file:Main.java

public static String loadJSONFromAsset(final Context context, final String fileName) throws IOException {
    String json;/* ww w. j a  v a2  s  . c o  m*/

    InputStream is = context.getAssets().open(fileName);
    int size = is.available();
    byte[] buffer = new byte[size];

    is.read(buffer);
    is.close();

    json = new String(buffer, "UTF-8");

    return json;
}

From source file:Main.java

/**
 * Performs copy of one stream into another.
 * @param is - input stream/*from w  ww  . j a  v a2s .c  o  m*/
 * @param os - output stream
 * @throws IOException
 */
public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[1024];
    int count = 0;

    while ((count = is.read(buffer)) != -1) {
        os.write(buffer, 0, count);
    }

    os.flush();
}

From source file:Main.java

public static void saveImage(String imageUrl, String destinationFile) throws Exception {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;/*from w ww  .j  av  a2s.c o m*/

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

From source file:Main.java

private static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buff = new byte[4096];
    int len;//from   ww  w .j ava 2 s  .c om
    while ((len = input.read(buff)) != -1) {
        output.write(buff, 0, len);
    }
}

From source file:Main.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;//from   www. j  a  v a 2  s  .  co  m
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
    out.flush();
}

From source file:Main.java

public static byte[] loadAsset(Context context, String asset) {
    byte[] buffer = null;
    try {// w  w w  .  ja  va 2 s  .c  om
        InputStream is = context.getAssets().open(asset);
        int size = is.available();
        buffer = new byte[size];
        is.read(buffer);
        is.close();
    } catch (IOException e) {
        Log.e(TAG, "Failed to load asset " + asset + ": " + e);
    }
    return buffer;
}