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:fi.jumi.launcher.daemon.DirBasedSteward.java

private static boolean sameSize(Path path, InputStream in) throws IOException {
    return Files.exists(path) && Files.size(path) == in.available();
}

From source file:Main.java

public static Bitmap decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
        throws IOException {
    is.mark(is.available());

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w ww.  j  av a2  s .c o  m*/
    BitmapFactory.decodeStream(is, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    is.reset();

    return BitmapFactory.decodeStream(is, null, options);
}

From source file:Main.java

public static byte[] getBytesFromInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);/*from ww  w  . java2s . c  o  m*/
    out.write(buffer);
    return out.toByteArray();
}

From source file:Main.java

private static String loadJSONFromAsset(Context context, String pathNameFile) {
    String output = null;/*from w  w  w .java2 s . c  o  m*/
    try {
        InputStream is = context.getAssets().open(DIR_BOARDS_ASSETS + "/" + pathNameFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return output;
}

From source file:Main.java

public static String readFile(Context mContext, String file, String code) {
    int len = 0;/*from  ww  w  . ja v  a  2 s .  c o m*/
    byte[] buf = null;
    String result = "";
    try {
        InputStream in = mContext.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

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

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

/**
 * //from   w  w w.  j a  va2s.co  m
 * @param request
 * @return JSONObject con los parametros del request
 * @throws Exception 
 */
public static JSONObject toObject(HttpServletRequest request) throws Exception {
    if (request.getParameter("accion") != null) {//Servidor independiente
        JSONObject r = new JSONObject();
        r.put("accion", request.getParameter("accion"));
        r.put("datos", request.getParameter("datos"));
        return r;
    } else {//Servidor base netbeans
        InputStream is = request.getInputStream();
        byte[] charr = new byte[is.available()];
        is.read(charr);
        return (JSONObject) JSONValue.parse(new String(charr, "UTF-8"));
    }
}

From source file:com.o2d.pkayjava.editor.utils.Overlap2DUtils.java

private static String getMyDocumentsLocation() {
    String myDocuments = null;/*from   ww w. j  a va  2 s  .  co  m*/
    try {
        if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }
        if (SystemUtils.IS_OS_WINDOWS) {
            Process p = Runtime.getRuntime().exec(
                    "reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal");
            p.waitFor();

            InputStream in = p.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            in.close();

            myDocuments = new String(b);
            myDocuments = myDocuments.split("\\s\\s+")[4];
        }
        if (SystemUtils.IS_OS_LINUX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }

    } catch (Throwable t) {
        t.printStackTrace();
    }

    return myDocuments;
}

From source file:Main.java

public static int streamCopy(InputStream in, OutputStream out, byte[] buffer) {
    int total = 0;
    try {//  w  w  w.  j  a v a  2 s.c  o  m
        int available = in.available();
        int chunk = buffer.length;
        while (available > 0) {
            if (chunk > available)
                chunk = available;
            if (chunk > 0) {
                int readed = in.read(buffer, 0, chunk);
                if (readed == 0)
                    break;
                out.write(buffer, 0, readed);
                total += readed;
            }
            available -= chunk;
        }
    } catch (InterruptedIOException e) {
        return -1;
    } catch (IOException e) {
    }
    return total;
}

From source file:Main.java

public static byte[] getAssetBytes(String path) {
    byte[] mBytes = null;
    if (sAssetManager != null) {
        InputStream input = null;
        try {//from www .ja  v  a  2 s  .c  o  m
            input = sAssetManager.open(path);
            int length = input.available();
            mBytes = new byte[length];
            input.read(mBytes);
            input.close();
            if (!mFileTable.containsKey(path)) {
                mFileTable.put(path, true);
            }
        } catch (Exception e) {
            if (!mFileTable.containsKey(path)) {
                mFileTable.put(path, false);
            }
        }
    }
    return mBytes;
}

From source file:ch.ethz.coss.nervousnet.hub.ui.ShowcaseActivity.java

public static String parseJSONFile(String res, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(res);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);/*from  w  w w.j  a v  a2s  . c o  m*/
    file.close();

    return new String(formArray);
}