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 Bitmap convertByteArrayToBitmap(byte[] byteArray) {
    if (byteArray != null) {
        ByteArrayInputStream imageStream = new ByteArrayInputStream(byteArray);
        return BitmapFactory.decodeStream(imageStream);
    } else {//  ww w .  j  a  va 2 s.com
        return null;
    }
}

From source file:Main.java

public static InputStream StringTOInputStream(String in, String encode) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes(encode));
    return is;//w  w w.  j av  a2  s. com
}

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {/*w  w  w  . j  a  v  a2s  .  c om*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static String decompress(byte[] bytes) {
    try {// w  w  w  . ja v  a 2s. com
        InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[262144]; // about 300kb
        int len;
        while ((len = in.read(buffer)) > 0)
            baos.write(buffer, 0, len);
        return new String(baos.toByteArray(), "UTF-8");
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static ByteArrayInputStream toByteStream(InputStream istream) {
    ByteArrayInputStream byteistream = new ByteArrayInputStream(new byte[0]);
    try {// w  w w.  j a  va 2 s  .  c  om
        ByteArrayOutputStream byteostream = new ByteArrayOutputStream(8192);
        byte[] buffer = new byte[8192];
        int lenght;
        while ((lenght = istream.read(buffer)) != -1) {
            byteostream.write(buffer, 0, lenght);
        }
        byteistream = new ByteArrayInputStream(byteostream.toByteArray());
        byteostream.close();
    } catch (Exception e) {
    } finally {
        try {
            istream.close();
        } catch (Exception e) {
        }
    }
    return byteistream;
}

From source file:Main.java

public static int byteArrayCompare(byte[] byte1, byte[] byte2) {
    byte[] tByte1 = new byte[byte2.length];

    ByteArrayInputStream input = new ByteArrayInputStream(byte1);

    try {/*from w  w w  . j a va 2  s  . c o m*/
        input.read(tByte1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ByteBuffer byteBuf1 = ByteBuffer.wrap(tByte1);
    ByteBuffer byteBuf2 = ByteBuffer.wrap(byte2);

    return byteBuf1.compareTo(byteBuf2);
}

From source file:Main.java

public static byte[] unZip(byte[] data) {
    byte[] b = null;
    try {/*ww  w  . j a va  2 s  .com*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        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 Map<String, String> toMap(String properties) {
    try {//from w  w w .  java  2 s. com
        InputStream is = new ByteArrayInputStream(properties.getBytes("UTF-8"));
        Properties prop = new Properties();
        prop.load(is);
        Map<String, String> ret = new HashMap<String, String>();
        for (String key : prop.stringPropertyNames()) {
            ret.put(key, prop.getProperty(key));
        }
        return ret;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) {
    byte[] b = null;
    try {/* ww  w. ja v a  2s  .co  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 final byte[] unCompress(byte[] buf) throws IOException {
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
    ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

    int count;/*from   w  ww . j  a  v a 2s  .co m*/
    byte[] tmp = new byte[2048];
    while ((count = gzi.read(tmp)) != -1) {
        bos.write(tmp, 0, count);
    }

    // store uncompressed back to buffer      
    gzi.close();
    return bos.toByteArray();
}