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:eu.eidas.engine.test.simple.SSETestUtils.java

/**
 * Read SAML from file./*from   w  ww. j  av a2  s.com*/
 *
 * @param resource the resource
 *
 * @return the byte[]
 * @throws IOException the exception
 *
 */
public static byte[] readSamlFromFile(final String resource) throws IOException {
    InputStream inputStream = null;
    byte[] bytes;

    try {
        inputStream = AuthRequestTest.class.getResourceAsStream(resource);

        // Create the byte array to hold the data
        bytes = new byte[(int) inputStream.available()];
        inputStream.read(bytes);
    } catch (IOException e) {
        LOG.error("Error read from file: " + resource);
        throw e;
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return bytes;

}

From source file:eu.stork.peps.test.simple.SSETestUtils.java

/**
 * Read stork SAML from file./*from  w w  w.j  a v  a 2  s  .  c  o m*/
 * 
 * @param resource the resource
 * 
 * @return the byte[]
 * @throws IOException the exception
 * 
 */
public static byte[] readStorkSamlFromFile(final String resource) throws IOException {
    InputStream inputStream = null;
    byte[] bytes;

    try {
        inputStream = StorkAuthRequestTest.class.getResourceAsStream(resource);

        // Create the byte array to hold the data
        bytes = new byte[(int) inputStream.available()];
        inputStream.read(bytes);
    } catch (IOException e) {
        LOG.error("Error read from file: " + resource);
        throw e;
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return bytes;

}

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

public static String stream2Bytes(InputStream is, Charset charset) {
    String result = null;/*w w w . j a  v a2  s  .  c om*/
    try {
        int total = is.available();
        byte[] bs = new byte[total];
        is.read(bs);
        result = new String(bs, charset.name());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static byte[] read(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(in.available());
    transfer(in, out);/*from w w w  .j a va2  s .co  m*/
    return out.toByteArray();
}

From source file:Main.java

public static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[4096];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);

    // Do the first byte via a blocking read
    outputStream.write(inputStream.read());

    // Slurp the rest
    int available = 0;// inputStream.available();
    boolean run = true;
    while (run && (available = inputStream.available()) > 0) {
        // Log.d(TAG, "slurp " + available);
        while (available > 0) {
            int cbToRead = Math.min(buffer.length, available);
            int cbRead = inputStream.read(buffer, 0, cbToRead);
            if (cbRead <= 0) {
                run = false;/*from  ww w.  jav a  2s  . c  o  m*/
                break;
            }
            outputStream.write(buffer, 0, cbRead);
            available -= cbRead;
        }
    }
    return outputStream.toByteArray();
}

From source file:Main.java

public static void makeInternalCopy(Context c, String path, int resource) {
    InputStream is = c.getResources().openRawResource(resource);
    try {//from w w w . ja v a  2 s .  com

        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        File sniff = new File(path);
        if (sniff.exists()) {
            os.writeBytes("rm " + path + "\n");
        }
        byte[] bytes = new byte[is.available()];

        FileOutputStream setdbOutStream = new FileOutputStream(path);
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(bytes);

        setdbOutStream.write(bytes);
        setdbOutStream.close();

        os.writeBytes("chmod 777 " + path + "\n");
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        //           Toast.makeText(c, "Error Copying file: " + e.toString(),Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

From source file:Main.java

public static String getNextVCardItem(InputStream is) throws IOException {

    // We read as long as we get to the END:VCARD token
    StringBuffer event = new StringBuffer();
    StringBuffer line = new StringBuffer();
    boolean begun = false;
    char ahead = (char) 0;
    boolean lookAhead = false;

    while (lookAhead || is.available() > 0) {
        char ch;/*w  w  w.j a  va  2 s .  co  m*/
        if (lookAhead) {
            ch = ahead;
            lookAhead = false;
        } else {
            ch = (char) is.read();
        }
        if (ch == '\r' || ch == '\n') {
            if (is.available() > 0) {
                if (ch == '\n') {
                    ahead = '\n';
                } else {
                    ahead = (char) is.read();
                }
                lookAhead = true;
            }
            // Found an EOL
            if (begun) {
                line.append('\r');
                if (lookAhead && ahead == '\n') {
                    line.append('\n');
                    lookAhead = false;
                }
                event.append(line.toString());

                if (line.toString().indexOf("END:VCARD") >= 0) {
                    // This is the end of the event
                    begun = false;
                    break;
                }
            } else {
                lookAhead = false;
            }
            line = new StringBuffer();
        } else {
            line.append(ch);
            if (line.toString().indexOf("BEGIN:VCARD") >= 0) {
                begun = true;
            }
        }
    }
    return event.toString();
}

From source file:Main.java

public static String getNextCalendarItem(InputStream is) throws IOException {

    // We read as long as we get to the END:VCALENDAR token
    StringBuffer event = new StringBuffer();
    StringBuffer line = new StringBuffer();
    boolean begun = false;
    char ahead = (char) 0;
    boolean lookAhead = false;

    while (lookAhead || is.available() > 0) {
        char ch;/*w  w  w  . j  ava  2  s .  c  o m*/
        if (lookAhead) {
            ch = ahead;
            lookAhead = false;
        } else {
            ch = (char) is.read();
        }
        if (ch == '\r' || ch == '\n') {
            if (is.available() > 0) {
                if (ch == '\n') {
                    ahead = '\n';
                } else {
                    ahead = (char) is.read();
                }
                lookAhead = true;
            }
            // Found an EOL
            if (begun) {
                line.append('\r');
                if (lookAhead && ahead == '\n') {
                    line.append('\n');
                    lookAhead = false;
                }
                event.append(line.toString());

                if (line.toString().indexOf("END:VCALENDAR") >= 0) {
                    // This is the end of the event
                    begun = false;
                    break;
                }
            } else {
                lookAhead = false;
            }
            line = new StringBuffer();
        } else {
            line.append(ch);
            if (line.toString().indexOf("BEGIN:VCALENDAR") >= 0) {
                begun = true;
            }
        }
    }
    return event.toString();
}

From source file:com.github.consiliens.harv.util.Utils.java

/** Converts stream to string. **/
public static String streamToString(final InputStream input) {
    try {/*w w w. ja  va2  s .com*/
        final byte[] dataBytes = new byte[input.available()];
        ByteStreams.readFully(input, dataBytes);

        return new String(dataBytes, UTF8);
    } catch (final Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.amazonaws.services.kinesis.scaling.auto.AutoscalingConfiguration.java

public static AutoscalingConfiguration[] loadFromURL(String url)
        throws IOException, InvalidConfigurationException {
    File configFile = null;//from  ww  w .  j a va 2s .c o m

    if (url.startsWith("s3://")) {
        // download the configuration from S3
        AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());

        TransferManager tm = new TransferManager(s3Client);

        // parse the config path to get the bucket name and prefix
        final String s3ProtoRegex = "s3:\\/\\/";
        String bucket = url.replaceAll(s3ProtoRegex, "").split("/")[0];
        String prefix = url.replaceAll(String.format("%s%s\\/", s3ProtoRegex, bucket), "");

        // download the file using TransferManager
        configFile = File.createTempFile(url, null);
        Download download = tm.download(bucket, prefix, configFile);
        try {
            download.waitForCompletion();
        } catch (InterruptedException e) {
            throw new IOException(e);
        }

        // shut down the transfer manager
        tm.shutdownNow();

        LOG.info(String.format("Loaded Configuration from Amazon S3 %s/%s to %s", bucket, prefix,
                configFile.getAbsolutePath()));
    } else if (url.startsWith("http")) {
        configFile = File.createTempFile("kinesis-autoscaling-config", null);
        FileUtils.copyURLToFile(new URL(url), configFile, 1000, 1000);
        LOG.info(String.format("Loaded Configuration from %s to %s", url, configFile.getAbsolutePath()));
    } else {
        try {
            InputStream classpathConfig = AutoscalingConfiguration.class.getClassLoader()
                    .getResourceAsStream(url);
            if (classpathConfig != null && classpathConfig.available() > 0) {
                configFile = new File(AutoscalingConfiguration.class
                        .getResource((url.startsWith("/") ? "" : "/") + url).toURI());
            } else {
                // last fallback to a FS location
                configFile = new File(url);

                if (!configFile.exists()) {
                    throw new IOException("Unable to load local file from " + url);
                }
            }
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
        LOG.info(String.format("Loaded Configuration local %s", url));
    }

    // read the json config into an array of autoscaling
    // configurations
    AutoscalingConfiguration[] configuration;
    try {
        configuration = mapper.readValue(configFile, AutoscalingConfiguration[].class);
    } catch (Exception e) {
        throw new InvalidConfigurationException(e);
    }

    // validate each of the configurations
    for (AutoscalingConfiguration conf : configuration) {
        conf.validate();
    }

    return configuration;
}