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 Object getAsObject(String key) {
    byte[] data = getAsBinary(key);
    if (data != null) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {//from w w w  .j ava 2s.c o m
            bais = new ByteArrayInputStream(data);
            ois = new ObjectInputStream(bais);
            Object reObject = ois.readObject();
            return reObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (bais != null)
                    bais.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static <T> T fromXml(String responseBody, Class<T> c) throws JAXBException, XMLStreamException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes());
    JAXBContext jaxbContext = JAXBContext.newInstance(c);
    XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(xsr);
}

From source file:Main.java

public static Document parse(String xmlContent) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(xmlContent.getBytes("UTF-8"));
    return parse(is);
}

From source file:Main.java

public static Document parseXML(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*w w w. j a v a2s  . com*/
    DocumentBuilder db = factory.newDocumentBuilder();

    ByteArrayInputStream sr = new ByteArrayInputStream(xml.getBytes());
    try {
        Document document = db.parse(sr);
        return document;
    } finally {
        sr.close();
    }
}

From source file:Main.java

public static <T> T unmarshal(Class<T> clazz, String xml) throws JAXBException {
    return unmarshal(clazz, new ByteArrayInputStream(xml.getBytes()));
}

From source file:Main.java

public static Document convertToDoc(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
}

From source file:Main.java

/**
 * Compress the bitmap to a PNG and return its {@link ByteArrayInputStream}
 * //  www.  j  a  v  a 2 s.c o  m
 * @param bitmap The {@link Bitmap} to compress
 * @return The {@link ByteArrayInputStream}
 */
public static ByteArrayInputStream toPNGInputStream(Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    return new ByteArrayInputStream(bytes.toByteArray());
}

From source file:Main.java

public static Document bytes2doc(byte[] xml) throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilder builder = builderFac.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml));
}

From source file:Main.java

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

        /* return the compressed bytes */
        return bout.toByteArray();
    }
}

From source file:Main.java

/**
 * @param xml is XML for a document//from   w ww.ja v  a 2 s  .c o m
 * @return the Document contained in that XML
 */
public static Document readString(String xml) {
    ensureFactory();
    try {
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}