Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

/**
 * Loads a file into string//from w  ww  .  j av a2  s .  c om
 */
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:co.edu.unal.arqdsoft.presentacion.JSON.java

/**
 * //from   www.  jav  a2s .  c o 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:Main.java

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;/*from  w  w w .ja  v  a 2s .  com*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    out.flush();
}

From source file:Main.java

public static byte[] readInputStream(InputStream in) {
    byte[] buffer = null;
    try {//from   w w w  .  jav  a2  s  .c o  m
        int length = in.available();
        buffer = new byte[length];
        in.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

private static long copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    long count = 0;
    int n = 0;// www . ja  va  2 s.  c o m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static String getContent(InputStream in) throws IOException {
    String retval = "";
    byte[] buf = new byte[512];
    while (true) {
        int n = in.read(buf);
        if (n <= 0)
            break;
        retval += new String(buf);
    }/*from w  w  w  .j a v a 2s.  com*/
    return retval;
}

From source file:Main.java

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

    try {//from   w w w .j  ava  2s  .c o  m
        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

/**
 * Reads at most <tt>maxBytes</tt> bytes from the supplied input stream and
 * returns them as a byte array./* w  w w.j av a2  s. c  om*/
 *
 * @param in The InputStream supplying the bytes.
 * @param maxBytes The maximum number of bytes to read from the input
 * stream.
 * @return A byte array of size <tt>maxBytes</tt> if the input stream can
 * produce that amount of bytes, or a smaller byte containing all available
 * bytes from the stream otherwise.
 */
public static final byte[] readBytes(InputStream in, int maxBytes) throws IOException {
    byte[] result = new byte[maxBytes];

    int bytesRead = in.read(result);
    int totalBytesRead = bytesRead;

    while (totalBytesRead < maxBytes && bytesRead >= 0) {
        // Read more bytes
        bytesRead = in.read(result, bytesRead, maxBytes - bytesRead);

        if (bytesRead > 0) {
            totalBytesRead += bytesRead;
        }
    }

    if (totalBytesRead < 0) {
        // InputStream at end-of-file
        result = new byte[0];
    } else if (totalBytesRead < maxBytes) {
        // Create smaller byte array
        byte[] tmp = new byte[totalBytesRead];
        System.arraycopy(result, 0, tmp, 0, totalBytesRead);
        result = tmp;
    }

    return result;
}

From source file:Main.java

public static byte[] readByteArray(InputStream stream, int length) {
    byte[] data = null;
    try {//from   w ww .  ja v  a2  s .c o m
        data = new byte[(int) length];
        stream.read(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}

From source file:com.splout.db.common.CompressorUtil.java

public static void createZip(File dir, File out, IOFileFilter filefilter, IOFileFilter dirFilter)
        throws IOException {
    Collection<File> files = FileUtils.listFiles(dir, filefilter, dirFilter);

    out.delete();//from   w w  w .j  a  v  a2 s  .  c  o m
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(getRelativePath(f, dir));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}