Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out, int size) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Usage

From source file:Main.java

public static Bitmap GetLocalOrNetBitmap(String url) {
    Bitmap bitmap = null;/*from w  w  w  . j  a v a 2 s .com*/
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream(), 1024);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024);
        copy(in, out);
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*from   w ww .  j  a v  a  2 s  .co m*/
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        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 Bitmap getBitmapFromURL(String urlString) {
    Bitmap bitmap = null;/*from  w  w  w.j  a  v  a  2 s  . co m*/
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(urlString).openStream(), 1024 * 4);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024 * 4);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

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

    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*  w  w  w . ja  v a 2  s.c  om*/
        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 Bitmap getAndSetBitmapFromNet(String urlPath) {

    Bitmap bm = null;//from   w  ww . ja  v a 2 s  . com

    if (urlPath != null) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024);
            copy(bis, out);
            out.flush();
            final byte[] data = dataStream.toByteArray();
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            Log.i(I, "data.length: " + data.length);
            out.close();
            dataStream.close();
            bis.close();
            bm = processBitmap(bm);
        } catch (IOException e) {
            Log.i(I, "URL Connection or Bitmap processing Exception");
            e.printStackTrace();
        }
    }
    return bm;
}

From source file:Main.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    out = new BufferedOutputStream(out, 0x1000);
    in = new BufferedInputStream(in, 0x1000);

    // Copy the contents from the input stream to the output stream.
    while (true) {
        int b = in.read();
        if (b == -1) {
            break;
        }/*from ww w.  j  av  a2 s .  com*/
        out.write(b);
    }
    out.flush();
}

From source file:Main.java

public static void writeInts(String file, int[] ints) throws IOException {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 4 * 1024));
    try {//  w ww . j  a  va 2 s .  c o m
        int len = ints.length;
        out.writeInt(len);
        for (int i = 0; i < len; i++) {
            out.writeInt(ints[i]);
        }
    } finally {
        out.close();
    }
}

From source file:Main.java

public static void writeFloats(String file, float[] floats) throws IOException {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 4 * 1024));
    try {// w ww  .  j a v a2  s.c o  m
        int len = floats.length;
        out.writeInt(len);
        for (int i = 0; i < len; i++) {
            out.writeFloat(floats[i]);
        }
    } finally {
        out.close();
    }
}

From source file:Main.java

public static void writeSettings(String file, Object... objs) throws IOException {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1024));
    try {/* w  w  w.ja  va  2s.c  om*/
        out.writeInt(objs.length);
        for (Object obj : objs) {
            char cl;
            if (obj instanceof Byte) {
                cl = 'Y';
            } else {
                cl = obj.getClass().getSimpleName().charAt(0);
            }

            out.writeChar(cl);
            if (obj instanceof String) {
                out.writeUTF((String) obj);
            } else if (obj instanceof Float) {
                out.writeFloat((Float) obj);
            } else if (obj instanceof Double) {
                out.writeDouble((Double) obj);
            } else if (obj instanceof Integer) {
                out.writeInt((Integer) obj);
            } else if (obj instanceof Long) {
                out.writeLong((Long) obj);
            } else if (obj instanceof Boolean) {
                out.writeBoolean((Boolean) obj);
            } else {
                throw new IllegalStateException("Unsupported type");
            }
        }
    } finally {
        out.close();
    }
}

From source file:Main.java

/**
 * Loads a bitmap from the specified url. This can take a while, so it should not
 * be called from the UI thread.//from w ww  .j a  v a2  s .  co m
 * 
 * @param url The location of the bitmap asset
 * 
 * @return The bitmap, or null if it could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}