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:com.yahoo.ycsb.db.RedisClient.java

public static String compress(String st) {
    if (compress != null && compress.equals("y")) {
        if (compressAlgo != null && (compressAlgo.equals("lz4") || compressAlgo.equals("lz4hc"))) {
            try {
                byte[] data = st.getBytes("ISO-8859-1");
                LZ4Compressor compressor;
                if (compressAlgo.equals("lz4")) {
                    compressor = lz4factory.fastCompressor();
                } else {
                    compressor = lz4factory.highCompressor();
                }/*ww w.j  a va2s .c om*/
                final int decompressedLength = data.length;
                int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
                byte[] compressed = new byte[maxCompressedLength];
                int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0,
                        maxCompressedLength);
                byte[] compressed2 = Arrays.copyOf(compressed, compressedLength);
                String ret = decompressedLength + "|" + new String(compressed2, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("bzip2")) {
            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(st.getBytes("ISO-8859-1"));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(baos);
                final byte[] buffer = new byte[8192];
                int n = 0;
                while (-1 != (n = byteArrayInputStream.read(buffer))) {
                    bzOut.write(buffer, 0, n);
                }
                bzOut.close();
                return new String(baos.toByteArray(), "ISO-8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("snappy")) {
            try {
                byte[] compressed = Snappy.compress(st, "ISO-8859-1");
                String ret = new String(compressed, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return st;
}

From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java

public static String prettyPrintAPDUResponse(final byte[] data, final int indentLength) {
    StringBuilder buf = new StringBuilder();

    ByteArrayInputStream stream = new ByteArrayInputStream(data);

    while (stream.available() > 0) {
        buf.append("\n");
        if (stream.available() == 2) {
            stream.mark(0);//from w w w .  j a v a 2s .  com
            byte[] value = new byte[2];
            try {
                stream.read(value);
            } catch (IOException e) {
            }
            SwEnum sw = SwEnum.getSW(value);
            if (sw != null) {
                buf.append(getSpaces(0));
                buf.append(BytesUtils.bytesToString(value)).append(" -- ");
                buf.append(sw.getDetail());
                continue;
            }
            stream.reset();
        }

        buf.append(getSpaces(indentLength));

        TLV tlv = TlvUtil.getNextTLV(stream);

        byte[] tagBytes = tlv.getTagBytes();
        byte[] lengthBytes = tlv.getRawEncodedLengthBytes();
        byte[] valueBytes = tlv.getValueBytes();

        ITag tag = tlv.getTag();

        buf.append(prettyPrintHex(tagBytes));
        buf.append(" ");
        buf.append(prettyPrintHex(lengthBytes));
        buf.append(" -- ");
        buf.append(tag.getName());

        int extraIndent = (lengthBytes.length + tagBytes.length) * 3;

        if (tag.isConstructed()) {
            // indentLength += extraIndent; //TODO check this
            // Recursion
            buf.append(prettyPrintAPDUResponse(valueBytes, indentLength + extraIndent));
        } else {
            buf.append("\n");
            if (tag.getTagValueType() == TagValueTypeEnum.DOL) {
                buf.append(TlvUtil.getFormattedTagAndLength(valueBytes, indentLength + extraIndent));
            } else {
                buf.append(getSpaces(indentLength + extraIndent));
                buf.append(prettyPrintHex(BytesUtils.bytesToStringNoSpace(valueBytes),
                        indentLength + extraIndent));
                buf.append(" (");
                buf.append(TlvUtil.getTagValueAsString(tag, valueBytes));
                buf.append(")");
            }
        }
    }
    return buf.toString();
}

From source file:com.easyhome.common.modules.network.HttpManager.java

private static void imageContentToUpload4Byte(OutputStream out, byte[] fileContent) throws WeiboException {
    if (fileContent == null) {
        return;//from   ww  w .ja v  a 2s .com
    }
    StringBuilder temp = new StringBuilder();

    temp.append(MP_BOUNDARY).append("\r\n");
    temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"").append("news_image")
            .append("\"\r\n");
    String filetype = "image/png";
    temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    ByteArrayInputStream input = null;
    try {
        out.write(res);
        input = new ByteArrayInputStream(fileContent);
        byte[] buffer = new byte[1024 * 50];
        while (true) {
            int count = input.read(buffer);
            if (count == -1) {
                break;
            }
            out.write(buffer, 0, count);
        }
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new WeiboException(e);
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                throw new WeiboException(e);
            }
        }
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static void writeSignatureBlock(byte[] sigBlock, SignatureType signatureType, String signatureName,
        JarOutputStream jos) throws IOException {

    // Block's extension depends on signature type
    String extension = null;/*w w  w  .j  a v  a 2s .  co  m*/

    if (signatureType == SHA1_DSA) {
        extension = DSA_SIG_BLOCK_EXT;
    } else {
        extension = RSA_SIG_BLOCK_EXT;
    }

    // Signature block entry
    JarEntry bkJarEntry = new JarEntry(
            MessageFormat.format(METAINF_FILE_LOCATION, signatureName, extension).toUpperCase());
    jos.putNextEntry(bkJarEntry);

    // Write content
    ByteArrayInputStream bais = new ByteArrayInputStream(sigBlock);

    byte[] buffer = new byte[2048];
    int read = -1;

    while ((read = bais.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }

    jos.closeEntry();
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static void writeSignatureFile(byte[] sf, String signatureName, JarOutputStream jos)
        throws IOException {

    // Signature file entry
    JarEntry sfJarEntry = new JarEntry(
            MessageFormat.format(METAINF_FILE_LOCATION, signatureName, SIGNATURE_EXT).toUpperCase());
    jos.putNextEntry(sfJarEntry);//from  w w w.  j  a  v a2  s.co m

    // Write content
    ByteArrayInputStream bais = null;

    try {
        bais = new ByteArrayInputStream(sf);

        byte[] buffer = new byte[2048];
        int read = -1;

        while ((read = bais.read(buffer)) != -1) {
            jos.write(buffer, 0, read);
        }

        jos.closeEntry();
    } finally {
        IOUtils.closeQuietly(bais);
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static void writeManifest(byte[] manifest, JarOutputStream jos) throws IOException {

    // Manifest file entry
    JarEntry mfJarEntry = new JarEntry(MANIFEST_LOCATION);
    jos.putNextEntry(mfJarEntry);/*  w  w  w  . j a v a  2 s. com*/

    // Write content
    ByteArrayInputStream bais = null;

    try {
        bais = new ByteArrayInputStream(manifest);

        byte[] buffer = new byte[2048];
        int read = -1;

        while ((read = bais.read(buffer)) != -1) {
            jos.write(buffer, 0, read);
        }

        jos.closeEntry();
    } finally {
        IOUtils.closeQuietly(bais);
    }
}

From source file:org.talend.utils.io.FilesUtils.java

/**
 * DOC amaumont Comment method "getFile".
 * //from w  w  w .  j av  a2s  . c  om
 * @param jobScriptArchive
 * @throws IOException
 */
public static File getFile(String filePath, byte[] jobScriptArchive) throws IOException {
    File file = new File(filePath);

    ByteArrayInputStream bis = new ByteArrayInputStream(jobScriptArchive, 0, jobScriptArchive.length);

    FileOutputStream fos = new FileOutputStream(file);

    int bufferSize = 1024;
    byte[] buf = new byte[bufferSize];

    int readBytes = 0;

    while ((readBytes = bis.read(buf)) != -1) {
        fos.write(buf, 0, readBytes);
    }

    bis.close();
    fos.close();

    return file;
}

From source file:org.eclipse.smarthome.binding.lifx.internal.fields.MACAddress.java

private void formatHex(String original, int length, String separator) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(original.getBytes());
    byte[] buffer = new byte[length];
    String result = "";
    while (bis.read(buffer) > 0) {
        for (byte b : buffer) {
            result += (char) b;
        }/*from w  w w .j  a v  a  2 s  .  c om*/
        Arrays.fill(buffer, (byte) 0);
        result += separator;
    }

    hex = StringUtils.left(result, result.length() - 1);

}

From source file:org.osaf.cosmo.dao.hibernate.HibernateTestHelper.java

public void verifyInputStream(InputStream is1, byte[] content) throws Exception {
    byte[] buf1 = new byte[4096];
    byte[] buf2 = new byte[4096];

    ByteArrayInputStream is2 = new ByteArrayInputStream(content);

    int read1 = is1.read(buf1);
    int read2 = is2.read(buf2);
    while (read1 > 0 || read2 > 0) {
        Assert.assertEquals(read1, read2);

        for (int i = 0; i < read1; i++)
            Assert.assertEquals(buf1[i], buf2[i]);

        read1 = is1.read(buf1);// ww w.ja v a  2  s. co  m
        read2 = is2.read(buf2);
    }
}

From source file:org.openhab.binding.lifx.internal.fields.MACAddress.java

private void formatHex(String original, int length, String separator) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(original.getBytes());
    byte[] buffer = new byte[length];
    String result = "";
    while (bis.read(buffer) > 0) {
        for (byte b : buffer) {
            result += (char) b;
        }//from   ww  w. j  a  v  a  2 s. c  o  m
        Arrays.fill(buffer, (byte) 0);
        result += separator;
    }

    hex = StringUtils.left(result, result.length() - 1);
}