Example usage for java.io BufferedInputStream available

List of usage examples for java.io BufferedInputStream available

Introduction

In this page you can find the example usage for java.io BufferedInputStream available.

Prototype

public synchronized 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 by the next invocation of a method for this input stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("C:/ReadFile.txt");
    FileInputStream fin = new FileInputStream(file);

    BufferedInputStream bin = new BufferedInputStream(fin);
    while (bin.available() > 0) {
        System.out.println((char) bin.read());
    }/*from   ww w  . java 2  s . co  m*/
    bin.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream inStream = new FileInputStream("c:/test.txt");
    BufferedInputStream bis = new BufferedInputStream(inStream);

    int byteNum = bis.available();

    System.out.println(byteNum);/*from  w w  w  .j av a 2 s  .  c om*/

    bis.close();

    byteNum = bis.available();
    System.out.println(byteNum);

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read until a single byte is available
    while (bis.available() > 0) {
        // read the byte and convert the integer to character
        char c = (char) bis.read();

        System.out.println(c);/*from ww  w . j a v  a  2  s.com*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream is = new FileInputStream("C:/test.txt");

    // input stream is converted to buffered input stream
    BufferedInputStream bis = new BufferedInputStream(is);

    // read until a single byte is available
    while (bis.available() > 0) {
        // skip single byte from the stream
        bis.skip(1);/*from  w w w  . java 2 s . c om*/

        // read next available byte and convert to char
        char c = (char) bis.read();
        System.out.print(c);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read number of bytes available
    int numByte = bis.available();

    // byte array declared
    byte[] buf = new byte[numByte];

    // read byte into buf , starts at offset 2, 3 bytes to read
    bis.read(buf, 2, 3);/*w w w.ja v a  2  s .  c o m*/

    // for each byte in buf
    for (byte b : buf) {
        System.out.println((char) b + ": " + b);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read until a single byte is available
    while (bis.available() > 0) {
        // get the number of bytes available
        Integer nBytes = bis.available();
        System.out.println("Available bytes = " + nBytes);

        // read next available character
        char ch = (char) bis.read();

        // print the read character.
        System.out.println("The character read = " + ch);
    }// ww  w. j av  a 2 s.c  om

}

From source file:GenSig.java

public static void main(String[] args) {

    /* Generate a DSA signature */

    if (args.length != 1) {
        System.out.println("Usage: GenSig nameOfFileToSign");
    } else// w  w  w .j av a  2s. co  m
        try {

            /* Generate a key pair */

            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

            keyGen.initialize(1024, random);

            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();

            /*
             * Create a Signature object and initialize it with the private
             * key
             */

            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

            dsa.initSign(priv);

            /* Update and sign the data */

            FileInputStream fis = new FileInputStream(args[0]);
            BufferedInputStream bufin = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                dsa.update(buffer, 0, len);
            }
            ;

            bufin.close();

            /*
             * Now that all the data to be signed has been read in, generate
             * a signature for it
             */

            byte[] realSig = dsa.sign();

            /* Save the signature in a file */
            FileOutputStream sigfos = new FileOutputStream("sig");
            sigfos.write(realSig);

            sigfos.close();

            /* Save the public key in a file */
            byte[] key = pub.getEncoded();
            FileOutputStream keyfos = new FileOutputStream("suepk");
            keyfos.write(key);

            keyfos.close();

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

}

From source file:Main.java

/**
 * parse a certificate file into ArrayList of certificates
 *//*from  ww w  .j  av a2 s .c  o m*/
public static ArrayList<Certificate> readCertificate(File f) throws CertificateException {
    ArrayList<Certificate> certs = new ArrayList<Certificate>();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    BufferedInputStream in;
    try {
        in = new BufferedInputStream(new FileInputStream(f));
        while (in.available() > 0) {
            Certificate cert = cf.generateCertificate(in);
            certs.add(cert);
        }
        in.close();
        return certs;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/*****************************************************
 *
 * Returns the bytes from an input stream, or an exception
 * if there was an error.//from   ww w. j a  v  a2 s  .c o  m
 *
 *****************************************************/
private static Object getBytes(BufferedInputStream bis) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bis.available());

    byte[] buffer = new byte[READ_BUFFER_SIZE_IN_BYTES];

    int byteCount = -1;

    while ((byteCount = bis.read(buffer)) >= 0) {
        baos.write(buffer, 0, byteCount);
    }

    return (baos.toByteArray());
}

From source file:Main.java

public static String readFromFile(String file) {
    try {/*from   w ww  .  j a  v  a  2 s.c o m*/
        File f = new File(file);

        if (!f.exists() || !f.canRead()) {
            return null;
        }

        BufferedInputStream ipt = new BufferedInputStream(new FileInputStream(f));

        byte[] buf = new byte[512];
        StringBuilder sb = new StringBuilder();

        while (ipt.available() > 0) {
            int len = ipt.read(buf, 0, 512);
            sb.append(new String(buf, 0, len, "UTF-8"));
        }

        ipt.close();
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}