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 loadJSONFromAsset(Context context, String fileName) {
    String json;/*from w  w  w .j  a va 2 s.c o m*/
    try {

        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");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

/**
 * A method to load x.json files from assets folder and convert them
 * into a string.//from w w w  .  ja  v a2 s.  c om
 *
 * @param c        A contect
 * @param fileName The json file in assets folder which to load json data from
 * @return
 */
public static String loadJSONFromAsset(Context c, String fileName) {
    String json = null;
    try {
        InputStream is = c.getAssets().open(fileName);
        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 loadJSONFromAsset(Context context, String fileName) {
    String json = null;//from  w ww.j ava 2s. c  om
    try {
        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");
    } catch (IOException ex) {
        Log.d(TAG, "Exception Occurred : " + ex.getMessage());
        return null;
    }
    return json;

}

From source file:Main.java

public static String LoadData(Context context, String inFile) {
    String tContents = "";
    try {/*from  w  w  w. j a v  a  2  s. c om*/
        InputStream stream = context.getAssets().open(inFile);
        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions

    }

    return tContents;
}

From source file:Main.java

public static File WriteStreamToFile(InputStream resStream) {
    try {/*  w  ww . ja v  a  2s .  c om*/
        byte[] bytes = new byte[resStream.available()];
        File tmpFile = File.createTempFile("z4-", ".tmp");
        tmpFile.deleteOnExit();
        DataInputStream dis = new DataInputStream(resStream);
        dis.readFully(bytes);
        FileOutputStream foutStream = new FileOutputStream(tmpFile.getPath());
        foutStream.write(bytes);
        foutStream.close();
        dis.close();
        return tmpFile;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readAssetTextFile(Context context, String inFile) {
    String tContents = "";

    try {//from   ww w  . j av  a 2 s  .c om
        InputStream stream = context.getAssets().open(inFile);

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions here
    }

    return tContents;

}

From source file:Main.java

public static void copyAssetFileToFiles(Context context, String root, String filename) throws IOException {
    InputStream is = context.getAssets().open(filename);
    byte[] buffer = new byte[is.available()];
    is.read(buffer);//  w  ww.ja  v a  2 s  . c o  m
    is.close();

    File tgtfile = new File(root + "/" + filename);
    tgtfile.createNewFile();
    tgtfile.setExecutable(true);
    FileOutputStream os = new FileOutputStream(tgtfile);
    os.write(buffer);
    os.close();
}

From source file:Main.java

protected static byte[] readBuffer(InputStream is) throws IOException {
    if (is.available() == 0) {
        return new byte[0];
    }/*w w w  . j a v a2  s  .  co m*/

    byte[] buffer = new byte[BUFFER_SIZE];
    is.mark(BUFFER_SIZE);
    int bytesRead = is.read(buffer, 0, BUFFER_SIZE);
    int totalBytesRead = bytesRead;

    while (bytesRead != -1 && (totalBytesRead < BUFFER_SIZE)) {
        bytesRead = is.read(buffer, totalBytesRead, BUFFER_SIZE - totalBytesRead);

        if (bytesRead != -1)
            totalBytesRead += bytesRead;
    }

    if (totalBytesRead < BUFFER_SIZE) {
        byte[] smallerBuffer = new byte[totalBytesRead];
        System.arraycopy(buffer, 0, smallerBuffer, 0, totalBytesRead);
        smallerBuffer = buffer;
    }

    is.reset();
    return buffer;
}

From source file:Main.java

/**
 * Loads a file into string/*ww  w  .j  ava2 s  .com*/
 */
public static String readFile(String fileName, AssetManager assetManager) throws IOException {
    InputStream input;
    input = assetManager.open(fileName);
    int size = input.available();
    byte[] buffer = new byte[size];
    input.read(buffer);
    input.close();
    return new String(buffer);
}

From source file:Main.java

/**
 * Get file content by filename/*  w w  w.j ava2 s  . c  o  m*/
 * 
 * @param c
 * @param filename
 * @return content String
 */
public static String getFileContent(Context c, String filename) {
    try {
        InputStream fin = c.getAssets().open(filename);
        byte[] buffer = new byte[fin.available()];
        fin.read(buffer);
        fin.close();
        return new String(buffer);
    } catch (IOException e) {
        Log.e("inspector", e.getLocalizedMessage());
    }
    return "";
}