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 void Unzip(String dir, byte[] data) throws Exception {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ZipInputStream zipIn = new ZipInputStream(input);
    ZipEntry entry;/*  w w  w  .j av  a  2  s.c  o  m*/
    while ((entry = zipIn.getNextEntry()) != null) {
        String outpath = dir + entry.getName();
        FileOutputStream output = null;
        try {
            File f = new File(outpath);
            f.getParentFile().mkdirs();

            output = new FileOutputStream(f);
            int len = 0;
            byte[] buffer = new byte[4096];
            while ((len = zipIn.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

    zipIn.close();
}

From source file:Main.java

public static X509Certificate getCertificateFromBlob(byte[] encoded) throws IOException {
    try {/*from   w  ww  .j a  v a2s .  com*/
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(encoded));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse);
    }
}

From source file:Main.java

public static String getBinaryHash(Activity activity) throws IOException, NoSuchAlgorithmException {
    ArrayList<String> manifestEntries = new ArrayList<String>();
    addFolderEntriesToManifest(manifestEntries, "www", activity.getAssets());
    Collections.sort(manifestEntries);
    JSONArray manifestJSONArray = new JSONArray();
    for (String manifestEntry : manifestEntries) {
        manifestJSONArray.put(manifestEntry);
    }/*from   w w  w.ja  va2  s . co  m*/

    // The JSON serialization turns path separators into "\/", e.g. "www\/images\/image.png"
    String manifestString = manifestJSONArray.toString().replace("\\/", "/");
    return computeHash(new ByteArrayInputStream(manifestString.getBytes()));
}

From source file:Main.java

public static Drawable byteToDrawable(byte[] byteArray) {
    ByteArrayInputStream ins = new ByteArrayInputStream(byteArray);
    return Drawable.createFromStream(ins, null);
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/* www. j  a va  2 s.  c  om*/
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable)
                break;
        }
    } catch (NameNotFoundException e) {
    } catch (CertificateException e) {
    }
    return debuggable;
}

From source file:Main.java

public static <T> List<T> deepCopy(List<T> src) {
    try {/*  w w w.j  ava  2 s.c  om*/
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String setAttribute(String xmlStr, String tagName, String attrName, String newValue) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    int i, j;/*from w w  w.ja va 2  s . c o  m*/
    Document doc = null;
    DocumentBuilder builder = null;

    StringWriter stringOut = new StringWriter();
    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        // Get the root element
        Node rootNode = doc.getFirstChild();
        NodeList nodeList = doc.getElementsByTagName(tagName);
        if ((nodeList.getLength() == 0) || (nodeList.item(0).getAttributes().getNamedItem(attrName) == null)) {
            logger.error("Either node " + tagName + " or attribute " + attrName + " not found.");
        } else {
            logger.debug("value of " + tagName + " attribute: " + attrName + " = "
                    + nodeList.item(0).getAttributes().getNamedItem(attrName).getNodeValue());
            nodeList.item(0).getAttributes().getNamedItem(attrName).setNodeValue(newValue);

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(stringOut);
            // StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);
            logger.debug("Updated XML: \n" + stringOut.toString());
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return stringOut.toString();
}

From source file:Main.java

static public XmlPullParser createParser(String xml) throws XmlPullParserException {
    XmlPullParser xpp = createParser();/*from  w  w  w .ja  va2s  .  com*/
    InputStream is;
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new XmlPullParserException("UTF-8: unsupported encoding");
    }
    xpp.setInput(is, "UTF-8");
    return xpp;
}

From source file:ca.uhn.fhir.jpa.dao.GZipUtil.java

public static String decompress(byte[] theResource) {
    GZIPInputStream is;/*from w ww  .ja  v a2  s . c  o  m*/
    try {
        is = new GZIPInputStream(new ByteArrayInputStream(theResource));
        return IOUtils.toString(is, "UTF-8");
    } catch (IOException e) {
        throw new DataFormatException("Failed to decompress contents", e);
    }
}

From source file:Main.java

/**
 * whether current app is debuggable//from   w w w .  j  ava 2 s. co  m
 */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = packageInfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable) {
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    return debuggable;
}