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:FileUtils.java

public static String loadStreamIntoString(InputStream stream) throws IOException {
    if (stream == null) {
        throw new java.io.IOException("null stream");
    }//from  w ww .j av a2 s  .c om
    stream = new java.io.BufferedInputStream(stream);
    int avail = stream.available();
    byte[] data = new byte[avail];
    int numRead = 0;
    int pos = 0;
    do {
        if (pos + avail > data.length) {
            byte[] newData = new byte[pos + avail];
            System.arraycopy(data, 0, newData, 0, pos);
            data = newData;
        }
        numRead = stream.read(data, pos, avail);
        if (numRead >= 0) {
            pos += numRead;
        }
        avail = stream.available();
    } while (avail > 0 && numRead >= 0);
    return new String(data, 0, pos, "US-ASCII");
}

From source file:ViewImageTest.java

/**
 * @param testFile/*from   w w  w.ja  va2  s. c  o m*/
 *            file to load
 * @return testFile content as a bytes array
 * @throws IOException
 *             when an error occured while loading
 */
private static byte[] getBytes(File testFile) throws IOException {
    InputStream inStream = new FileInputStream(testFile);
    byte[] res = new byte[inStream.available()];
    try {
        inStream.read(res);
    } finally {
        inStream.close();
    }
    return res;
}

From source file:Main.java

public static void copyAsset(Context context, String assetFileName, String dstFile) {
    AssetManager assetManager = null;//from w w w.jav  a2  s  .  c o m
    assetManager = context.getAssets();
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        inputStream = assetManager.open(assetFileName);
        fileOutputStream = new FileOutputStream(dstFile);
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        fileOutputStream.write(buffer);
        fileOutputStream.flush();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            inputStream.close();
            fileOutputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:br.com.manish.ahy.kernel.util.FileUtil.java

public static byte[] readResourceAsBytes(String path) {

    byte[] byteData = null;

    try {/*from www .  ja va 2  s  . c  om*/

        InputStream is = FileUtil.class.getResourceAsStream(path);

        if (is.available() > Integer.MAX_VALUE) {
            throw new OopsException("Oversized file :-( can't read it, sorry: " + path);
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[512];

        int readBytes;
        while ((readBytes = is.read(bytes)) > 0) {
            os.write(bytes, 0, readBytes);
        }

        byteData = os.toByteArray();

        is.close();
        os.close();

    } catch (Exception e) {
        throw new OopsException(e, "Problems when reading: [" + path + "].");
    }

    return byteData;
}

From source file:de.decidr.workflowmodeleditor.server.servicetask.WSDLUploadServletImpl.java

/**
 * Completely read input stream to memory and convert it to a ByteArrayInputStream.
 *//*from ww  w. jav a  2  s  .  c  om*/
private static ByteArrayInputStream receiveUpload(InputStream stream) throws IOException {
    byte[] buffer = new byte[4096];
    ByteArrayOutputStream data = new ByteArrayOutputStream(stream.available());
    int bytesRead;
    while ((bytesRead = stream.read(buffer)) > 0) {
        data.write(buffer, 0, bytesRead);
    }
    return new ByteArrayInputStream(data.toByteArray());
}

From source file:Main.java

public static StringBuffer getStringFromStream(InputStream is) throws IOException {

    int len;//from www. j  av  a2s. com
    int size = 1024;
    byte[] buf;
    StringBuffer result = new StringBuffer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    buf = new byte[size];
    while (is.available() > 0) {
        int x = is.read();
        char c = (char) x;
        result.append(c);
    }

    return result;
}

From source file:Main.java

public static String readAM(InputStream inputStream) {
    String result = "";
    try {/* w ww.  ja  v  a 2s.  c  o  m*/
        byte[] buffer = new byte[inputStream.available()];
        if (inputStream.read(buffer) > 0) {
            result = decompressXML(buffer);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }

    return result;
}

From source file:com.adito.security.pki.SshPublicKeyFile.java

/**
 * @param in//from  w ww.  j a v  a2s.c  om
 * @return SshPublicKeyFile
 * @throws InvalidKeyException
 * @throws IOException
 */
public static SshPublicKeyFile parse(InputStream in) throws InvalidKeyException, IOException {

    byte[] data = new byte[in.available()];
    in.read(data);
    in.close();

    return parse(data);
}

From source file:org.pmedv.core.util.ResourceUtils.java

/**
 * Creates a {@link ByteArrayResource} from an input stream
 * // ww w . ja va 2  s.  c o  m
 * @param is  the stream to read from
 * @return     the resource
 */
@SuppressWarnings("unused")
public static ByteArrayResource createResourceFromStream(InputStream is) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] b;

    try {
        b = new byte[is.available()];

        for (int n; (n = is.read(b)) != -1;) {
            bos.write(b);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return new ByteArrayResource(bos.toByteArray());

}

From source file:org.pmedv.core.util.ResourceUtils.java

/**
 * Writes a given <code>InputStream</code> to a file.
 * // w  w w  .j  a va2 s .  c o  m
 * @param is
 * @param out
 */
public static void writeStreamToFile(InputStream is, File out) {

    byte[] b;

    try {

        FileOutputStream fos = new FileOutputStream(out);

        b = new byte[is.available()];

        for (int n; (n = is.read(b)) != -1;) {
            fos.write(b);
        }

        fos.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}