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[] decompressGzip(byte[] compressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(bis);
    try {/*from w ww.j  av  a  2  s. co m*/
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        int read = 0;
        while ((read = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
        }
    } finally {
        gis.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

public static Document parseToDom(String content) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);// ww w  . j av a  2 s  .  c o m
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(content.getBytes()));
}

From source file:Main.java

public static InputStream getBitmapPngStream(Bitmap theBitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    theBitmap.compress(Bitmap.CompressFormat.PNG, 95, baos);

    byte[] data = baos.toByteArray();
    return new ByteArrayInputStream(data);
}

From source file:Main.java

public static InputStream getBitmapJpegStream(Bitmap theBitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    theBitmap.compress(Bitmap.CompressFormat.JPEG, 95, baos);

    byte[] data = baos.toByteArray();
    return new ByteArrayInputStream(data);
}

From source file:Main.java

/**
 * uncompress the xmlByteArray//from w w  w .j a va2  s.c  o  m
 */
public static byte[] uncompressByteArray(byte[] xmlByteArray) throws IOException {
    byte[] tmp = new byte[2048];
    int byteCount = 0;
    ByteArrayOutputStream uncompressedData = new ByteArrayOutputStream();
    GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(xmlByteArray));

    while ((byteCount = gzipIS.read(tmp)) != -1) {
        uncompressedData.write(tmp, 0, byteCount);
    }

    return uncompressedData.toByteArray();
}

From source file:Main.java

public static Document getDocFromBuffer(byte[] buf) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from www .ja  v a  2s .c om

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(buf));

    return doc;
}

From source file:Main.java

public static InputStream getStringStream(String sInputString) {
    ByteArrayInputStream tInputStringStream = null;
    if (sInputString != null && !sInputString.trim().equals("")) {
        byte[] bytes;
        try {/*from   w  ww  . j  av a2 s . c o m*/
            bytes = sInputString.getBytes("UTF-8");
            tInputStringStream = new ByteArrayInputStream(bytes);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
    return tInputStringStream;
}

From source file:Main.java

public static Document parse(byte[] ba) throws IOException {
    return parse(new ByteArrayInputStream(ba));
}

From source file:Main.java

public static <T> T xmlToObj(Class<T> t, String xml, String encoding) {
    try {/*from  w  ww  . j a  va  2  s  . co m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(encoding));
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(bais);
        bais.close();
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document getDocument(String xmlDocument) {
    try {/* ww  w.j av a 2  s . co  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xmlDocument.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new RuntimeException("Error parsing XML document", e);
    }
}