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:com.msopentech.odatajclient.testservice.utils.Commons.java

public static String getETag(final String basePath, final ODataVersion version) throws Exception {
    try {/*from  w ww .j  a v  a2 s.  c  o m*/
        final InputStream is = FSManager.instance(version).readFile(basePath + "etag", Accept.TEXT);
        if (is.available() <= 0) {
            return null;
        } else {
            final String etag = IOUtils.toString(is);
            IOUtils.closeQuietly(is);
            return etag;
        }
    } catch (Exception e) {
        return null;
    }
}

From source file:com.eryansky.common.web.utils.DownloadUtils.java

/**
 * //from  w ww.j a  va  2s  . c  o  m
 * @param request
 * @param response
 * @param inputStream ?
 * @param displayName ??
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream,
        String displayName) throws IOException {
    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) inputStream.available());

    //        String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    //        displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayName);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(inputStream);
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static byte[] stream2Bytes(InputStream is) throws IOException {
    int total = is.available();
    byte[] bs = new byte[total];
    is.read(bs);/*from   w w w .ja va 2s. c  o  m*/
    return bs;
}

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

/**
 * @param in//ww  w.  j a v  a  2  s . c o  m
 * @return SshPrivateKeyFile
 * @throws InvalidKeyException
 * @throws IOException
 */
public static SshPrivateKeyFile parse(InputStream in) throws InvalidKeyException, IOException {

    byte[] data = null;

    try {
        data = new byte[in.available()];
        in.read(data);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }

    return parse(data);
}

From source file:io.cloudine.rhq.plugins.worker.ResourceUtils.java

/**
 *  ?  {@link org.springframework.core.io.Resource}? ? ?  .
 *
 * @param resource /*from w w w  . j a va2  s .co  m*/
 * @return {@link org.springframework.core.io.Resource}? ?  
 * @throws java.io.IOException ??    
 */
public static byte[] getResourceByteContents(Resource resource) throws IOException {
    InputStream inputStream = resource.getInputStream();
    int size = inputStream.available();
    byte[] bytes = new byte[size];
    inputStream.read(bytes);
    return bytes;
}

From source file:com.bacic5i5j.framework.toolbox.web.WebUtils.java

/**
 * ??//  w  w  w  .  j a  va 2s . c  o  m
 *
 * @param params
 * @param url
 * @return
 */
public static String post(Map<String, String> params, String url) {
    String result = "";

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> nvps = generateURLParams(params);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage() + " : " + e.getCause());
    }

    CloseableHttpResponse response = null;

    try {
        response = httpClient.execute(httpPost);
    } catch (IOException e) {
        log.error(e.getMessage() + " : " + e.getCause());
    }

    if (response != null) {
        StatusLine statusLine = response.getStatusLine();
        log.info("??: " + statusLine.getStatusCode());
        if (statusLine.getStatusCode() == 200 || statusLine.getStatusCode() == 302) {
            try {
                InputStream is = response.getEntity().getContent();
                int count = is.available();
                byte[] buffer = new byte[count];
                is.read(buffer);
                result = new String(buffer);
            } catch (IOException e) {
                log.error("???: " + e.getMessage());
            }
        }
    }

    return result;
}

From source file:Main.java

public static String getResponseInFile(String address, File file) throws IOException {
    String dataString = "";

    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {//from   w ww  .j  ava 2  s. co  m
        URL url = new URL(address);
        URLConnection connection = url.openConnection();

        inputStream = connection.getInputStream();

        byte[] data;
        if (inputStream.available() > BUFFER_SIZE) {
            data = new byte[BUFFER_SIZE];
        } else {
            data = new byte[inputStream.available()];
        }

        int dataCount;
        while ((dataCount = inputStream.read(data)) > 0) {
            outputStream.write(data, 0, dataCount);
        }

        OutputStream output = new OutputStream() {
            private StringBuilder string = new StringBuilder();

            @Override
            public void write(int b) throws IOException {
                this.string.append((char) b);
            }

            //Netbeans IDE automatically overrides this toString()
            public String toString() {
                return this.string.toString();
            }
        };
        output.write(data);

        dataString = output.toString();

    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        }
        try {
            outputStream.close();
        } catch (Exception e) {
        }
    }

    return dataString;
}

From source file:gemlite.core.util.Util.java

public static Resource toResource(ClassLoader loader, String file) {
    InputStream in = loader.getResourceAsStream(file);
    byte[] bt;/*from  w w  w.  j  a  va  2  s.  c o m*/
    try {
        bt = new byte[in.available()];
        in.read(bt);
        in.close();
        ByteArrayResource res = new ByteArrayResource(bt);
        return res;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.alfresco.webservice.util.ContentUtils.java

/**
 * Convert an input stream to a byte array
 * /*  w ww  .j  a v a 2s .c  om*/
 * @param inputStream   the input stream
 * @return              the byte array
 * @throws Exception
 */
public static byte[] convertToByteArray(InputStream inputStream) throws Exception {
    byte[] result = null;

    if (inputStream.available() > 0) {
        result = new byte[inputStream.available()];
        inputStream.read(result);
        ;
    }

    return result;
}

From source file:com.milang.torparknow.GreenParkingApp.java

/**
 * Load JSONObject from javascript file/*from   w  w w  . ja v a 2s. c o  m*/
 * @param context
 * @return
 */
public static JSONObject readJson(Context context) {
    InputStream is = null;
    String jsonString = null;
    JSONObject json = null;

    Log.i(TAG, "Reading JSON...");

    try {
        is = context.getResources().openRawResource(R.raw.carparks);
        byte[] reader = new byte[is.available()];
        while (is.read(reader) != -1) {
        }
        jsonString = "{\"carparks\": " + new String(reader) + "}";
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    }

    try {
        json = new JSONObject(jsonString);
    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
    }

    return json;
}