Example usage for java.io ByteArrayInputStream ByteArrayInputStream

List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream

Introduction

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

Prototype

public ByteArrayInputStream(byte buf[]) 

Source Link

Document

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

Usage

From source file:Main.java

public static byte[] encrypt(byte[] byteArray, PrivateKey privateKey) {
    Cipher cipher = null;//  w  ww.j  a va2  s .  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

/**
 * Uncompresses a GZIP file./*  ww  w . ja v a 2 s . c o m*/
 * 
 * @param bytes
 *            The compressed bytes.
 * @return The uncompressed bytes.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static byte[] gunzip(byte[] bytes) throws IOException {
    /* create the streams */
    InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len = 0;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
        }

        /* return the uncompressed bytes */
        return os.toByteArray();
    } finally {
        is.close();
    }
}

From source file:jfix.zk.Medias.java

public static InputStream asStream(Media media) {
    return new BufferedInputStream(
            media.inMemory() ? new ByteArrayInputStream(media.getByteData()) : media.getStreamData());
}

From source file:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

public static int getSizeOfTimeBytes(final byte[] timesAndSamples) {
    final DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(timesAndSamples));
    try {//  w  w w.j av  a  2  s.  co m
        return inputStream.readInt();
    } catch (IOException e) {
        throw new IllegalStateException(String.format(
                "Exception reading timeByteCount in TimelineChunkMapper.map() for timesAndSamples %s",
                Hex.encodeHex(timesAndSamples)), e);
    }
}

From source file:de.tntinteractive.portalsammler.engine.MapReaderTest.java

private static MapReader createReader(final String input) throws Exception {
    return MapReader.createFrom(new ByteArrayInputStream(input.getBytes("UTF-8")));
}

From source file:Main.java

/**
 * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
 *
 * @param compressed The compressed buffer.
 * @return The decompressed array./*from w w w .  ja  va2  s  . com*/
 * @throws IOException If there is an error decompressing the buffer.
 */
public static byte[] degzip(ByteBuffer compressed) throws IOException {
    try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed.array()));
            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];

        while (true) {
            int read = is.read(buffer, 0, buffer.length);
            if (read == -1) {
                break;
            }

            out.write(buffer, 0, read);
        }

        return out.toByteArray();
    }
}

From source file:Main.java

public static byte[] decompress(byte[] compressed) throws IOException {
    if (compressed == null || compressed.length == 0) {
        return compressed;
    }//from ww  w .  j av  a 2  s . c  o m
    ByteArrayInputStream sourceStream = new ByteArrayInputStream(compressed);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressed.length * 2);
    try (GZIPInputStream compressor = new GZIPInputStream(sourceStream)) {
        ByteStreams.copy(compressor, outputStream);
        compressor.close();
    }
    try {
        return outputStream.toByteArray();
    } finally {
        sourceStream.close();
        outputStream.close();
    }
}

From source file:com.honnix.yaacs.util.StreamUtil.java

public static InputStream byteArray2InputStream(byte[] byteArray) {
    return new ByteArrayInputStream(byteArray);
}

From source file:Main.java

public static String setValueXPath(String srcXmlString, String xPath, String newVal) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    int i, j;//from   w w  w . j ava  2s .c  o m
    Document doc = null;
    DocumentBuilder builder = null;
    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes()));
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList xPathNodes = (NodeList) result;
        logger.debug("xpath result count: " + xPathNodes.getLength());
        logger.debug(xPathNodes.item(0).getNodeName() + " = " + xPathNodes.item(0).getTextContent());

        // get list of all nodes in doc
        NodeList nodes = doc.getElementsByTagName("*");
        // iterate through all the nodes
        for (i = 0; i < xPathNodes.getLength(); i++) {
            // for each node in xpath result - traverse through all nodes in
            // doc to find match
            for (j = 0; j < nodes.getLength(); j++) {
                if (nodes.item(j).isSameNode(xPathNodes.item(i))) {
                    logger.debug("Old value " + i + ": " + xPathNodes.item(i).getNodeName() + " = "
                            + xPathNodes.item(i).getTextContent());
                    nodes.item(j).setTextContent(newVal);
                    logger.debug("New value " + i + ": " + xPathNodes.item(i).getNodeName() + " = "
                            + xPathNodes.item(i).getTextContent());
                    break;
                }
            }
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        // ex.printStackTrace();
    }
    return getW3CXmlFromDoc(doc);
}

From source file:Main.java

public static byte[] compress(byte[] source) throws IOException {
    if (source == null || source.length == 0) {
        return source;
    }//from w ww .  j a v  a  2  s .c o  m
    ByteArrayInputStream sourceStream = new ByteArrayInputStream(source);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(source.length / 2);
    try (OutputStream compressor = new GZIPOutputStream(outputStream)) {
        ByteStreams.copy(sourceStream, compressor);
        compressor.close();
    }
    try {
        return outputStream.toByteArray();
    } finally {
        sourceStream.close();
        outputStream.close();
    }
}