Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream ByteArrayOutputStream.

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {/*from  ww w .j  a v a  2 s  .c o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(fis);
        int ch;
        while ((ch = bis.read()) >= 0) {
            baos.write(ch);
        }
        baos.flush();
        return baos.toByteArray();
    } finally {
        fis.close();
    }
}

From source file:Main.java

public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);/*  ww w . j  av a2 s.co m*/

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

From source file:Main.java

static byte[] readInputStream(InputStream inputStream) {
    byte[] bArr = null;
    if (inputStream != null) {
        try {/* w  ww.  j  a va  2  s  . c  o  m*/
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[16384];
            while (true) {
                int chunkSize = inputStream.read(buffer);
                if (chunkSize < 0) {
                    break;
                } else if (chunkSize > 0) {
                    result.write(buffer, 0, chunkSize);
                }
            }
            bArr = result.toByteArray();
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        } catch (IOException e2) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e3) {
                }
            }
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e4) {
                }
            }
        }
    }
    return bArr;
}

From source file:Main.java

public static String readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;/*w  w  w. ja  v  a  2s  .c o m*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        return null;
    }
    return byteArrayOutputStream.toString();
}

From source file:Main.java

public static byte[] bitmap2JPGByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2PNGByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

/**
 * /*  w  w w  .  j  av  a 2s .c o  m*/
 * @param in
 * @return
 * @throws IOException
 */
public static String readFormStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    int len = 0;
    byte[] buffer = new byte[1024];
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }

    String result = out.toString();
    in.close();
    out.close();

    return result;
}

From source file:Main.java

public static byte[] toBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int ch;/*  ww  w.  j  a  v  a 2  s.c  o m*/
    while ((ch = in.read()) != -1) {
        out.write(ch);
    }
    byte buffer[] = out.toByteArray();
    out.close();
    return buffer;
}

From source file:Main.java

public static String readFile(Activity activity, String fileName) {
    AssetManager assets = activity.getAssets();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;/* www  .j a  v a2  s .  c  om*/
    try {
        InputStream inputStream = assets.open(fileName);
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream os = null;
    try {/*from   w  w  w  . j a v  a2  s.  co  m*/
        os = new ByteArrayOutputStream();
        copyStream(is, os);
    } finally {
        os.close();
    }
    return os.toByteArray();
}