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:org.apache.solr.common.util.Utils.java

/**
 * Make sure the InputStream is fully read.
 * //  www.j ava 2  s.  c o  m
 * @param is to read
 * @throws IOException on problem with IO
 */
private static void readFully(InputStream is) throws IOException {
    is.skip(is.available());
    while (is.read() != -1) {
    }
}

From source file:dk.dbc.DevelMain.java

private static String readInputStream(final InputStream is) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[4096];
        while (is.available() > 0) {
            int read = is.read(buffer);
            if (read > 0) {
                bos.write(buffer, 0, read);
            }//from  w  ww .ja v  a2 s  . c om
        }
        String s = new String(bos.toByteArray(), StandardCharsets.UTF_8);
        return s;
    }
}

From source file:com.zenome.bundlebus.Util.java

public static String readAsStringFromFile(@NonNull String aAbsolutePath)
        throws FileNotFoundException, IOException {
    final InputStream is = new FileInputStream(aAbsolutePath);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);/*from   ww  w  .j  a v  a2s  . co m*/
    is.close();
    return new String(buffer, "UTF-8");
}

From source file:org.apache.camel.component.http4.HttpEntityConverter.java

private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
    InputStreamEntity entity;/*from ww w.j  av  a  2 s  .  c o  m*/
    if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, in);
        entity = new InputStreamEntity(stream,
                stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1
                        : -1);
    } else {
        entity = new InputStreamEntity(in, -1);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}

From source file:com.quackware.handsfreemusic.Utility.java

private static Object readStream(int length, java.io.InputStream stream) throws java.io.IOException {
    String charset = null;//from w  w w .  j  a  va2  s.co m
    final int buflen = Math.max(1024, Math.max(length, stream.available()));
    byte[] buf = new byte[buflen];
    ;
    byte[] bytes = null;

    for (int nRead = stream.read(buf); nRead != -1; nRead = stream.read(buf)) {
        if (bytes == null) {
            bytes = buf;
            buf = new byte[buflen];
            continue;
        }
        final byte[] newBytes = new byte[bytes.length + nRead];
        System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
        System.arraycopy(buf, 0, newBytes, bytes.length, nRead);
        bytes = newBytes;
    }

    return new String(bytes);
}

From source file:org.apache.camel.component.http4.HttpEntityConverter.java

private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
    InputStreamEntity entity;/*from  ww w  .  ja  va  2  s  .co  m*/
    if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, data);
        entity = new InputStreamEntity(stream,
                stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1
                        : -1);
    } else {
        entity = new InputStreamEntity(new ByteArrayInputStream(data), data.length);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}

From source file:Main.java

public static String runAsRoot(String[] cmds) throws Exception {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    InputStream is = p.getInputStream();
    String result = null;/*from  w  w w.  j a  v a2s . co  m*/
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd + "\n");
        int readed = 0;
        byte[] buff = new byte[4096];
        while (is.available() <= 0) {
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
            }
        }

        while (is.available() > 0) {
            readed = is.read(buff);
            if (readed <= 0)
                break;
            String seg = new String(buff, 0, readed);
            result = seg; //result is a string to show in textview
        }
    }
    os.writeBytes("exit\n");
    os.flush();
    return result;
}

From source file:Main.java

/**
 * Show a level// w  w w .j a v a2  s .co  m
 * 
 * @param context The context
 * @param level The level
 */
public static void showLevel(Context context, int level) {
    String filename;
    switch (level) {
    case 1: {
        filename = "ground_floor.png";
        break;
    }
    case 2: {
        filename = "talks_floor.png";
        break;
    }

    default: {
        return;
    }
    }
    File f = new File(context.getFilesDir() + "/" + filename);
    try {
        if (f.exists() == false) {
            InputStream is = context.getAssets().open(filename);
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            // write the stream to file
            fos.write(buffer, 0, buffer.length);
            fos.close();
            is.close();
        }

        // Prepare the intent
        //TODO - create an activity for this instead. Internal viewers might be quite heavy
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(f), "image/png");
        context.startActivity(intent);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.google.android.apps.iosched.io.StubHttpClient.java

/**
 * Build a stub {@link HttpResponse}, probably for use with
 * {@link #setResponse(HttpResponse)}.//from   w ww  . j  ava2  s . c  om
 *
 * @param statusCode {@link HttpStatus} code to use.
 * @param assetName Name of asset that should be included as a single
 *            {@link HttpEntity} to be included in the response, loaded
 *            through {@link AssetManager}.
 */
public static HttpResponse buildResponse(int statusCode, String assetName, Context context) throws IOException {
    final StatusLine status = new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, null);
    final HttpResponse response = new BasicHttpResponse(status);
    if (assetName != null) {
        final InputStream entity = context.getAssets().open(assetName);
        response.setEntity(new InputStreamEntity(entity, entity.available()));
    }
    return response;
}

From source file:examples.utils.CifarReader.java

public static List<double[]> rawDouble(InputStream is) {
    List<double[]> result = new ArrayList<>();
    int row = 1 + (IMAGE_WIDTH * IMAGE_HIGHT * IMAGE_DEPTH);
    try {//from  w ww . j  a  v a2  s  . co  m
        while (is.available() > 0) {
            byte[] arr = new byte[row];
            is.read(arr);
            result.add(
                    Arrays.stream(ArrayUtils.toObject(arr)).mapToDouble(b -> Byte.toUnsignedInt(b)).toArray());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
    return result;
}