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:com.comichentai.serialize.SerializeUtil.java

public static <T> T deserialize(byte[] bytes, Class<T> clazz) {
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {//from w  w w  . jav a2 s .c  om
        bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        Object object = ois.readObject();
        return clazz.cast(object);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(bais);
        closeQuietly(ois);
    }
}

From source file:Main.java

public static InputStream getBitmapInputStream(Bitmap bitmap) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
    return bs;/*from  w w w  . j  a v a2 s  .c o m*/
}

From source file:Main.java

public static String toPrettyString(String xml) {
    int indent = 4;
    try {/* ww w .jav a  2  s  .com*/
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        // Setup pretty print options
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        // Return pretty print xml string
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        System.out.println(xml);
        throw new RuntimeException(
                "Problems prettyfing xml content [" + Splitter.fixedLength(100).split(xml) + "]", e);
    }
}

From source file:Main.java

static public void writeMultiLine(PrintWriter writer, String message) {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(message.getBytes())));
    try {//w w  w .j av a2 s .com
        String line = reader.readLine();
        while (line != null) {
            writer.println(line);
            line = reader.readLine();
        }
    } catch (IOException e) {
    }
}

From source file:Main.java

/**
 * Parse xml string returned from received from a gateway application
 * @throws ParserConfigurationException 
 * @throws IOException //from w  w w . j a  va2  s .c o  m
 * @throws SAXException 
 */
public static Document parseXml(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {
    // Remove three lines before prolog
    xmlString = xmlString.substring(xmlString.indexOf(System.getProperty("line.separator")) + 1);
    xmlString = xmlString.substring(xmlString.indexOf(System.getProperty("line.separator")) + 1);
    xmlString = xmlString.substring(xmlString.indexOf(System.getProperty("line.separator")) + 1);

    // Create document DocumentBuilder and Document from the xmlString
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
    Document doc = builder.parse(input);

    return doc;
}

From source file:Main.java

public static Document parse(final String xmlContent) {
    try {//www.  j a va 2 s .c  o m
        final DocumentBuilder builder = getDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xmlContent.getBytes()));
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:brut.androlib.mod.SmaliMod.java

public static boolean assembleSmaliFile(String smali, DexBuilder dexBuilder, boolean verboseErrors,
        boolean printTokens, File smaliFile) throws IOException, RuntimeException, RecognitionException {

    InputStream is = new ByteArrayInputStream(smali.getBytes());
    return assembleSmaliFile(is, dexBuilder, verboseErrors, printTokens, smaliFile);
}

From source file:com.wisemapping.util.ZipUtils.java

public static byte[] zipToBytes(byte[] zip) throws IOException {

    byte[] result = null;
    if (zip != null) {
        final ByteArrayInputStream in = new ByteArrayInputStream(zip);
        final ZipInputStream zipIn = new ZipInputStream(in);
        zipIn.getNextEntry();// www . j  a  v  a 2 s.  c o  m
        result = IOUtils.toByteArray(zipIn);

        zipIn.closeEntry();
        zipIn.close();
    }

    return result;
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary.//  ww  w  .  ja v  a  2 s  .c o m
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}

From source file:es.caib.sgtsic.util.Zips.java

public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(baos);

    for (String key : documents.keySet()) {

        InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key)));
        ZipEntry zipEntry = new ZipEntry(key);
        zip.putNextEntry(zipEntry);/* w w w .ja  va2s  .  c o m*/
        IOUtils.copy(is, zip);
        zip.closeEntry();
        is.close();
    }

    zip.close();
    baos.close();

    return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip");

}