Example usage for java.io ByteArrayInputStream available

List of usage examples for java.io ByteArrayInputStream available

Introduction

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

Prototype

public synchronized int available() 

Source Link

Document

Returns the number of remaining bytes that can be read (or skipped over) from this input stream.

Usage

From source file:Main.java

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

    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    bais.close();//from ww  w. jav a  2  s .co m

    int count = bais.available();
}

From source file:Main.java

public static void main(String[] arg) throws Throwable {
    File f = new File(arg[0]);
    InputStream in = new FileInputStream(f);

    byte[] buff = new byte[8000];

    int bytesRead = 0;

    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = in.read(buff)) != -1) {
        bao.write(buff, 0, bytesRead);/* ww  w  .j a v  a 2s .c  om*/
    }
    in.close();

    byte[] data = bao.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    System.out.println(bin.available());
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    int count = 0;

    while ((count = bais.available()) > 0) {
        char c = (char) bais.read();
        System.out.print("available byte(s) : " + count);
        System.out.println(c);/*www  .java 2 s  . c o  m*/
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*from w  w  w.  j av a 2  s  .  c o  m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:ByteArrayIOApp.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from   w  ww .j av a 2 s.  c o  m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*  w w  w .  ja v  a 2  s. co m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());

    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();

    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);

    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static String toString(ByteArrayInputStream is) {
    int size = is.available();
    char[] theChars = new char[size];
    byte[] bytes = new byte[size];

    is.read(bytes, 0, size);/*from w  w w .  j  av  a2s  . c o m*/
    for (int i = 0; i < size;)
        theChars[i] = (char) (bytes[i++] & 0xff);

    return new String(theChars);
}

From source file:Main.java

public static byte[] encrypt(byte[] byteArray, PrivateKey privateKey) {
    Cipher cipher = null;//from w  w w . j  a va  2s. com

    try {
        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        /*
           (define cipher (javax.crypto.Cipher.getInstance "RSA/ECB/NoPadding"))
         */
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[100];
            input.read(t0);
            output.write(cipher.doFinal(t0));
        }
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:Main.java

public static byte[] decrypt(byte[] byteArray, PublicKey publicKey) {
    Cipher cipher = null;//from  ww w . j a  va  2  s.  c  o m

    try {
        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[128];
            input.read(t0);
            output.write(cipher.doFinal(t0));
        }

    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:pl.otros.logview.io.Utils.java

public static boolean checkIfIsGzipped(FileObject fileObject) throws IOException {
    boolean gziped = false;
    if (fileObject.getContent().getSize() == 0) {
        LOGGER.fine("File object " + fileObject.getName() + " is empty, can't detect gzip compression");
        return false;
    }//from w w w  .j  ava 2s  .com
    InputStream inputStream = fileObject.getContent().getInputStream();
    byte[] loadProbe = loadProbe(inputStream, GZIP_CHECK_BUFFER_SIZE);
    // IOUtils.closeQuietly(inputStream);
    if (loadProbe.length < GZIP_MIN_SIZE) {
        LOGGER.info("Loaded probe is too small to check if it is gziped");
        return false;
    }
    try {
        ByteArrayInputStream bin = new ByteArrayInputStream(loadProbe);
        int available = bin.available();
        byte[] b = new byte[available < GZIP_CHECK_BUFFER_SIZE ? available : GZIP_CHECK_BUFFER_SIZE];
        int read = bin.read(b);
        gziped = checkIfIsGzipped(b, read);
    } catch (IOException e) {
        // Not gziped
        LOGGER.fine(fileObject.getName() + " is not gzip");
    }

    return gziped;
}