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 String readAsset(AssetManager assets, String path) throws IOException {
    InputStream is = assets.open(path);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);/*from w ww  .ja  va 2 s.  c om*/
    is.close();
    return new String(buffer);
}

From source file:Main.java

public static String getInputStreamString(InputStream is) throws IOException {
    byte[] buffer = new byte[is.available()];
    int len = is.read(buffer);
    return new String(buffer, 0, len, "utf-8");
}

From source file:Main.java

public static String AssetJSONFile(String filename, AssetManager manager) throws IOException {
    String json = null;//from   w  ww.j a v a2s . c o  m
    InputStream is = manager.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[] readFileFromAsset(String fileName) throws IOException {
    AssetManager assetMgr = context.getAssets();
    InputStream is = assetMgr.open(fileName);
    int size = is.available();
    byte[] content = new byte[size];
    is.read(content);/* www  . j  a  va  2 s. c  o m*/
    is.close();
    return content;
}

From source file:Main.java

public static String fromFile(File f) throws IOException {
    InputStream is = new FileInputStream(f);
    byte[] bs = new byte[is.available()];
    is.read(bs);//w w  w. j a  va  2  s  .c o  m
    is.close();
    return new String(bs);
}

From source file:Main.java

public static String readText(InputStream in) {
    try {/*  w  w  w  .j  a va2s . co m*/
        int available = in.available();
        if (available > 0) {
            char[] text = new char[available];
            InputStreamReader reader = new InputStreamReader(in);
            int readed = reader.read(text, 0, available);
            return new String(text);
        }
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

public static byte[] suckFile(String inputfile) throws IOException {
    InputStream is = context.openFileInput(inputfile);
    int size = is.available();
    byte[] content = new byte[size];

    is.read(content);//ww w . j a  va 2s. c om

    is.close();
    return content;
}

From source file:Main.java

public static String readAsset(Context context, String assetPath) throws IOException {
    String asset = null;//from ww w  . j av a 2s . c  om
    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 AssetJSONFile(String filename, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(filename);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);//from w  ww.  j a v  a 2  s  .  c  o  m
    file.close();

    return new String(formArray);
}

From source file:Main.java

public static String readFile(Context context, String file, String code) {
    int len = 0;//  w w w.jav a2 s.  co m
    byte[] buf = null;
    String grammar = "";
    try {
        InputStream in = context.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

        grammar = new String(buf, code);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return grammar;
}