Example usage for java.io ByteArrayInputStream read

List of usage examples for java.io ByteArrayInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

public static int byteArrayCompare(byte[] byte1, byte[] byte2) {
    byte[] tByte1 = new byte[byte2.length];

    ByteArrayInputStream input = new ByteArrayInputStream(byte1);

    try {/*w  w w  .j a  va2 s . com*/
        input.read(tByte1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ByteBuffer byteBuf1 = ByteBuffer.wrap(tByte1);
    ByteBuffer byteBuf2 = ByteBuffer.wrap(byte2);

    return byteBuf1.compareTo(byteBuf2);
}

From source file:Main.java

public static void writeFile(File file, byte[] content) throws IOException {
    if (!file.exists()) {
        try {/* w ww . j  a  v a2  s. co m*/
            file.createNewFile();
        } catch (IOException e) {
            throw new IOException("not crete file=" + file.getAbsolutePath());
        }
    }
    FileOutputStream fileOutputStream = null;
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(content);
        fileOutputStream = new FileOutputStream(file, false);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        fileOutputStream.flush();
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

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

    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 v  a 2s  .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:com.uzhnu.reedmuller.api.JsonFileCache.java

private static String getSha1Hash(String input) throws Exception {
    String retval = null;//from w  ww.j  av a2s .  c  om
    ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes("UTF-8"));
    MessageDigest hash = MessageDigest.getInstance("SHA1");
    byte[] buffer = new byte[1024];

    int numRead = 0;
    while ((numRead = inputStream.read(buffer)) != -1) {
        hash.update(buffer, 0, numRead);
    }

    retval = toHexString(hash.digest());
    return retval;
}

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  ww  . j  ava 2  s . c o  m*/
    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;
}

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

public static boolean checkIfIsGzipped(FileObject fileObject) throws IOException {
    boolean gziped = false;
    if (fileObject.getContent().getSize() == 0) {
        LOGGER.debug("File object " + fileObject.getName() + " is empty, can't detect gzip compression");
        return false;
    }/*  w  w w . j ava2s.c o m*/
    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.debug(fileObject.getName() + " is not gzip");
    }

    return gziped;
}

From source file:net.sf.keystore_explorer.utilities.io.HexUtil.java

/**
 * Get hex and clear text dump of byte array.
 *
 * @param bytes/*from   w  w w .  j  a v  a2  s .  c  o m*/
 *            Array of bytes
 * @return Hex/clear dump
 * @throws IOException
 *             If an I/O problem occurs
 */
public static String getHexClearDump(byte[] bytes) throws IOException {
    ByteArrayInputStream bais = null;

    try {
        // Divide dump into 8 byte lines
        StringBuffer strBuff = new StringBuffer();

        bais = new ByteArrayInputStream(bytes);
        byte[] line = new byte[8];
        int read = -1;
        boolean firstLine = true;

        while ((read = bais.read(line)) != -1) {
            if (firstLine) {
                firstLine = false;
            } else {
                strBuff.append(NEWLINE);
            }

            strBuff.append(getHexClearLineDump(line, read));
        }

        return strBuff.toString();
    } finally {
        IOUtils.closeQuietly(bais);
    }
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

private static byte[] read(ByteArrayInputStream bais) throws IOException {
    byte[] array = new byte[bais.available()];
    bais.read(array);

    return array;
}

From source file:org.roda.common.certification.ODFSignatureUtils.java

public static Path runDigitalSignatureSign(Path input, String ks, String alias, String password,
        String fileFormat) throws Exception {

    Security.addProvider(new BouncyCastleProvider());
    Path output = Files.createTempFile("odfsigned", "." + fileFormat);

    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream storeStream = new FileInputStream(ks);
    keystore.load(storeStream, password.toCharArray());
    X509Certificate certificate = (X509Certificate) keystore.getCertificate(keystore.aliases().nextElement());
    Key key = keystore.getKey(alias, password.toCharArray());
    IOUtils.closeQuietly(storeStream);//from   ww  w  .j  a va  2s. c  o m

    ByteArrayInputStream bais = createSignature(input.toString(), certificate, key);
    File file = output.toFile();
    if (file != null) {
        byte[] buffer = new byte[2048];
        int length = 0;
        FileOutputStream fos = new FileOutputStream(file);
        while ((length = bais.read(buffer)) >= 0) {
            fos.write(buffer, 0, length);
        }
        IOUtils.closeQuietly(fos);
    }

    return output;
}