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 byte[] readInputStream(InputStream in) {
    byte[] buffer = null;
    try {/*from w w  w . ja  v  a  2  s.c om*/
        int length = in.available();
        buffer = new byte[length];
        in.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static String readAsset(Context c, String fileName) {
    try {/*from  w ww  . j  a v a 2 s .c o m*/
        InputStream is = c.getAssets().open(fileName);
        byte[] buffer = new byte[is.available()];
        //noinspection ResultOfMethodCallIgnored
        is.read(buffer); // buffer is exactly the right size, a guarantee of asset files
        return new String(buffer);
    } catch (IOException ignore) {
    }
    return null;
}

From source file:Main.java

/**
 * Extract a resource into a real file/*from w w w .j ava  2s .  com*/
 * 
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param name of the resulting file
 * @param directory target directory
 * @return the resulting file
 * @throws IOException
 */
public static File extractResource(InputStream in, String filename, File directory) throws IOException {
    int n = in.available();
    byte[] buffer = new byte[n];
    in.read(buffer);
    in.close();
    File file = new File(directory, filename);
    FileOutputStream out = new FileOutputStream(file);
    out.write(buffer);
    out.close();
    return file;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String filename) {
    String json = null;//  w  w  w .ja  v a 2s .com
    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

public static String inputStream2String(InputStream is) {
    StringBuffer buffer = new StringBuffer("");
    try {//  w ww . j  a  v a 2  s . co m
        if (is.available() > 0) {
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer.toString();
}

From source file:Main.java

public static byte[] readInputStreamAsByteArray(InputStream is) throws IOException {
    int available = is.available();
    if (available <= 0) {
        available = 32 * 1024;//from w ww.  j a  v a2  s. co m
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream(available);

    byte[] buffer = new byte[32 * 1024];

    try {
        while (true) {

            int len = is.read(buffer);

            if (len <= 0) {

                break;
            }

            baos.write(buffer, 0, len);
        }

        return (baos.toByteArray());

    } finally {

        is.close();
    }
}

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {/*from   ww  w.j  av a2 s  .  c o  m*/
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:co.edu.unal.arqdsoft.presentacion.JSON.java

/**
 *
 * @param is//from w  w w  .j ava 2 s.c  o m
 * @return
 */
public static String getTemplate(InputStream is) {
    try {
        byte[] charr = new byte[is.available()];
        is.read(charr);
        String text = new String(charr, "UTF-8");
        text = text.replace("\n", "").replace("\r", "").replace("  ", "").replace("\"", "'");
        return text;
    } catch (IOException ex) {
        Logger.getLogger(JSON.class.getName()).log(Level.SEVERE, null, ex);
        return "";
    }
}

From source file:Main.java

/**
 * Get text file from assets/*w w  w. j  a  v  a  2 s  . co  m*/
 *
 * @param context
 * @param fileName
 * @return
 */
public static String getTextFromAssets(Context context, String fileName) {
    String result = null;
    try {
        InputStream in = getFileFromAssets(context, fileName);
        int length = in.available();
        byte[] buffer = new byte[length];
        in.read(buffer);
        result = new String(buffer, Charset.forName(ENCODING));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Document createDocument(InputStream input) {
    DOMParser parser = null;/*from  ww  w  . j a  v  a 2 s  .  com*/
    try {
        parser = getLargeParser(input.available());
        parser.parse(new InputSource(new FilterInputStream(input) {
            public void close() {
                // disable close. Because createDocument calls close, but it shoudn't do it.
            }
        }));
        return parser.getDocument();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}