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 ByteToObject(byte[] bytes) {
    Object obj = null;//from w ww.j  a  v a2  s .  c  om
    try {
        // bytearray to object
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = new ObjectInputStream(bi);

        obj = oi.readObject();
        bi.close();
        oi.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Object deserialize(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;// www  .ja  v  a  2s  .  c om
    Object o = null;
    try {
        in = new ObjectInputStream(bis);
        o = in.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return o;
}

From source file:Main.java

/**
 * Print xml to System.out./*from w w  w. j  a  va2 s . co m*/
 *
 * @param xml XML
 */
public static void println(byte[] xml) throws Exception {
    FileCopyUtils.copy(new InputStreamReader(new ByteArrayInputStream(xml), "utf8"),
            new PrintWriter(System.out));
    System.out.println();
    System.out.flush();
}

From source file:Main.java

public static byte[] gzipDecodeByteArray(byte[] data) {
    GZIPInputStream gzipInputStream = null;
    try {/*ww w . j  av  a2 s.  c o  m*/
        gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(data));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (gzipInputStream.available() > 0) {
            int count = gzipInputStream.read(buffer, 0, 1024);
            if (count > 0) {
                //System.out.println("Read " + count + " bytes");
                outputStream.write(buffer, 0, count);
            }
        }

        outputStream.close();
        gzipInputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object cloneObject(Object obj) {
    try {// w w  w.  j  a v  a  2 s  .  c om
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(obj);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        return in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Node parseString(String string) throws Exception {
    InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16"));

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    // from AIMLProcessor.evalTemplate and AIMLProcessor.validTemplate:
    //   dbFactory.setIgnoringComments(true); // fix this
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    Node root = doc.getDocumentElement();
    return root;//from   w  w  w . j  ava  2 s  .c o  m
}

From source file:Main.java

public static Object deserializeObject(byte[] b) {
    try {// ww  w  . j a v a 2 s  .  com
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
        Object object = in.readObject();
        in.close();

        return object;
    } catch (ClassNotFoundException cnfe) {
        Log.e("deserializeObject", "class not found error", cnfe);

        return null;
    } catch (IOException ioe) {
        Log.e("deserializeObject", "io error", ioe);

        return null;
    }
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {//from ww  w.  ja  v a2  s .  c  o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static ByteArrayInputStream Bitmap2ByteArrayInputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:Main.java

public final static Bitmap Bytes2Bimap(byte[] b) {
    if (b == null) {
        return null;
    }/*w  w  w. ja  v  a 2 s.c  om*/
    if (b.length != 0) {
        InputStream is = new ByteArrayInputStream(b);
        Bitmap bmp = BitmapFactory.decodeStream(is);
        return bmp;
    } else {
        return null;
    }
}