Example usage for java.io InputStream available

List of usage examples for java.io InputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

Usage

From source file:Main.java

public static int getInputStreamSize(InputStream in) {
    try {/*w w w .  ja  va  2 s .c  o m*/
        if (in != null)
            return in.available();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static String loadJSONFromAsset(final Context context, final String fileName) throws IOException {
    String json;/*w  ww . j  av a  2  s. com*/

    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

public static byte[] loadAsset(Context context, String asset) {
    byte[] buffer = null;
    try {/*from www .  ja  v  a 2  s .  com*/
        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;
}

From source file:Main.java

public static String extractAssetToString(Context context, String file) {
    String json;/*from ww  w  . java2 s . co m*/
    try {
        InputStream is = context.getAssets().open(file);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

public static String readAssetsFileString(Context context, String fileName) {
    String str = null;//from   www .j  a v  a 2 s . c om
    try {
        InputStream is = context.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        str = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFileName) throws IOException {

    AssetManager manager = context.getAssets();
    InputStream is = manager.open(jsonFileName);

    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);//  www .j a  v a2 s.  co m
    is.close();

    return new String(buffer, "UTF-8");
}

From source file:Main.java

public static byte[] readBytes(InputStream is) throws IOException {
    byte[] buffer;

    buffer = new byte[is.available()];
    is.read(buffer);/*  w ww  .  j a v a 2  s  .  com*/
    is.close();

    return buffer;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFile) {
    String json = null;/*from   w w  w  . j a v  a 2s.  co m*/
    try {
        InputStream is = context.getAssets().open(jsonFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

public static byte[] loadIntoBytes(String fileName) {
    AssetManager assetManager = context.getAssets();
    try {/* w  w  w.  java  2s .c o  m*/
        InputStream is = assetManager.open(fileName);
        byte buf[] = new byte[is.available()];
        is.read(buf);
        return buf;
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

public static JSONObject loadJsonFile(Context context, String fileName) throws IOException, JSONException {
    InputStream is = context.getAssets().open(fileName);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);/*from   w ww  .j  a  v  a 2  s .com*/
    is.close();
    String jsonString = new String(buffer);
    return new JSONObject(jsonString);
}