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 String[] getMultiValue(String xmlStr, String xpathStr)
        throws XPathExpressionException, SAXException, IOException {
    if (xmlStr == null)
        return null;
    InputStream is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
    return getMultiValue(is, xpathStr);
}

From source file:Main.java

public static List<Map<String, Object>> xmlPullParser(String xmlString, String tagName, String[] tagNameList) {

    List<Map<String, Object>> list = null;
    Map<String, Object> map = null;
    try {//w  ww .jav  a2  s  . c  o m
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser pullParser = factory.newPullParser();

        InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

        pullParser.setInput(is, "utf-8");
        int event = pullParser.getEventType();

        while (event != XmlPullParser.END_DOCUMENT) {
            String nodeName = pullParser.getName();

            switch (event) {
            case 0:
                list = new ArrayList<Map<String, Object>>();
                break;
            case 2:
                if (nodeName.equals(tagName)) {
                    map = new HashMap<String, Object>();
                }

                for (int i = 0; i < tagNameList.length; i++) {
                    if (nodeName.equals(tagNameList[i])) {
                        map.put(tagNameList[i], pullParser.nextText());
                    }
                }
                break;
            case 3:
                if (tagName.equals(nodeName)) {
                    list.add(map);
                }
                break;
            }
            event = pullParser.next();
        }
        return list;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** 
 *Copies source stream to target./*from ww  w  .  j  a va  2 s.  c  o m*/
 * 
 *@param source The source. 
 *@param name target The target.
 **/
protected static void copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target) throws Exception {
    // See if this is a MemoryStream -- we can use WriteTo.
    ByteArrayOutputStream memContentStream = source;
    if (memContentStream != null) {
        memContentStream.writeTo(target);
        memContentStream.flush();
    } else {
        // Otherwise, copy data through a buffer

        int c;
        ByteArrayInputStream inStream = new ByteArrayInputStream(source.toByteArray());

        while ((c = inStream.read()) != -1) {
            target.write((char) c);

        }
    }
}

From source file:Main.java

public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) {

    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*from   www  .  ja va  2s .  com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

        in = new BufferedInputStream(inputStream, 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);

        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

public static <T> T unserializer(Class<T> clazz, byte[] xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream is = new ByteArrayInputStream(xml);
    @SuppressWarnings("unchecked")
    T obj = (T) unmarshaller.unmarshal(is);
    return obj;//www.j a va  2 s. c  o m
}

From source file:Main.java

/**
 * Creates a {@link Node} out of a {@link String}
 * /* w  w w. j av  a2  s .  c om*/
 * @param xmlString
 *            never <code>null</code>
 * @return the string as node, never <code>null</code>
 */
public static Node asNode(String xmlString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream is = new ByteArrayInputStream(xmlString.getBytes());
    try {
        Document wfsCapabilities = builder.parse(is);
        return wfsCapabilities.getDocumentElement();
    } finally {
        is.close();
    }
}

From source file:Main.java

/**
 * Degzips the compressed array and places the results into the decompressed array.
 *
 * @param compressed The compressed array.
 * @param decompressed The decompressed array.
 * @throws IOException If an I/O error occurs.
 *///from  w  w  w . ja v a  2 s . c o m
public static void degzip(byte[] compressed, byte[] decompressed) throws IOException {
    try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
        is.readFully(decompressed);
    }
}

From source file:Main.java

/**
 * Obtiene el nodo raiz del documento xml
 *
 * @param respuestaPost//from  w w w .  ja va  2  s  .  co  m
 * @return
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Element getRootElement(final String respuestaPost)
        throws ParserConfigurationException, IOException, SAXException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new ByteArrayInputStream(respuestaPost.getBytes())).getDocumentElement();
}

From source file:Main.java

/**
 * Parse XML/*from www. j a v a  2 s. c o  m*/
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromString(String str)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(str.getBytes()));
    return doc;
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * //from  w w w .  ja  v a  2  s.  c  o  m
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}