Example usage for java.io ByteArrayOutputStream flush

List of usage examples for java.io ByteArrayOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:com.wareninja.opensource.gravatar4android.common.Utils.java

public static byte[] downloadImage_alternative2(String url) throws GenericException {

    byte[] imageData = null;
    try {/*from   ww  w. j a  v  a2  s  .  c om*/

        URLConnection connection = new URL(url).openConnection();

        InputStream stream = connection.getInputStream();

        //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer
        BufferedInputStream in = new BufferedInputStream(stream, 10240);// 10k=10240, 2x8k=16384
        ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
        int read;
        byte[] b = new byte[4096];

        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }

        out.flush();
        out.close();

        imageData = out.toByteArray();
    } catch (FileNotFoundException e) {
        return null;
    } catch (Exception e) {

        Log.w(TAG, "Exc=" + e);
        throw new GenericException(e);
    }

    return imageData;
}

From source file:com.risevision.ui.server.utils.MakeRequestServlet.java

private static byte[] inputStreamToByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;/*  ww w.  j a v  a2  s .  co  m*/
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();

    return buffer.toByteArray();
}

From source file:ai.susi.server.api.susi.ConsoleService.java

public static byte[] loadData(String url) throws IOException {
    ClientConnection cc = new ClientConnection(url);

    // fully read the input stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int n;/*  ww  w . j  a  v a  2  s. co  m*/
    byte[] buffer = new byte[16384];
    try {
        while ((n = cc.inputStream.read(buffer, 0, buffer.length)) != -1)
            baos.write(buffer, 0, n);
    } catch (IOException e) {
    }
    baos.flush();
    byte[] b = baos.toByteArray();

    // finished, close
    cc.close();
    return b;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java

private static byte[] getBytesFromFile(InputStream is) {
    try {/* w w  w  .  ja va 2 s .  c  o m*/
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        buffer.flush();
        return buffer.toByteArray();
    } catch (IOException e) {
        Log.e(TAG, "Error reading bytes from file", e);
        return null;
    }
}

From source file:com.github.czy1121.update.app.utils.UpdateUtil.java

public static String readString(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {//from   www.  ja v  a  2s.co  m
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        output.flush();
    } finally {
        close(input);
        close(output);
    }
    return output.toString("UTF-8");
}

From source file:de.bht.fpa.mail.s000000.common.mail.imapsync.MessageConverter.java

private static void handleInputStream(InputStream content, BodyPart bodyPart, MessageBuilder messageBuilder) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*from  w  w w .  j a v  a  2 s.  co  m*/
        int thisLine;
        while ((thisLine = content.read()) != -1) {
            bos.write(thisLine);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        bos.close();

        String encodeBase64String = new String(Base64.encodeBase64(bytes));
        // @formatter:off
        messageBuilder
                .attachment(newAttachmentBuilder().fileName(bodyPart.getFileName()).body(encodeBase64String));
        // @formatter:on
    } catch (Exception e) {
        // ignore
        return;
    }
}

From source file:Main.java

public static boolean saveBitmap(Bitmap aBmp, String aPath) {
    if (aBmp == null || aPath == null) {
        return false;
    }//from   w  w w. ja  v a 2 s. c om
    FileOutputStream fos = null;
    ByteArrayOutputStream baos = null;
    boolean result;
    try {
        File file = new File(aPath);
        if (!file.exists()) {
            file.createNewFile();
        }
        fos = new FileOutputStream(file);
        baos = new ByteArrayOutputStream();
        aBmp.compress(Bitmap.CompressFormat.PNG, 100, baos); //SUPPRESS CHECKSTYLE
        fos.write(baos.toByteArray());
        baos.flush();
        fos.flush();

        result = true;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        result = false;
    } catch (Exception e) {
        e.printStackTrace();
        result = false;
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static boolean saveJPEGBitmap(Bitmap aBmp, String aPath) {
    if (aBmp == null || aPath == null) {
        return false;
    }// www  . j  av  a  2 s  .  co  m

    FileOutputStream fos = null;
    ByteArrayOutputStream baos = null;
    boolean result = false;
    try {
        File file = new File(aPath);
        if (!file.exists()) {
            file.createNewFile();
        }
        fos = new FileOutputStream(file);
        baos = new ByteArrayOutputStream();
        aBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); //SUPPRESS CHECKSTYLE
        fos.write(baos.toByteArray());
        baos.flush();
        fos.flush();

        result = true;
    } catch (Error e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        result = false;
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.zhonghui.tool.controller.HttpClient.java

public static byte[] read(InputStream in) throws IOException {
    byte[] buf = new byte[1024];
    int length = 0;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    while ((length = in.read(buf, 0, buf.length)) > 0) {
        bout.write(buf, 0, length);//from   ww  w  . j  av  a 2 s .  c om
    }
    bout.flush();
    return bout.toByteArray();
}

From source file:com.kentdisplays.synccardboarddemo.Page.java

/**
 * Decodes an input stream from a file into Path objects that can be used to draw the page.
 *//*from www.java  2  s. c o  m*/
private static ArrayList<Path> pathsFromSamplePageInputStream(InputStream inputStream) {
    ArrayList<Path> paths = new ArrayList<Path>();
    try {
        // Retrieve a byte array from the sample page.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > -1) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        byte[] byteArray = baos.toByteArray();

        // Decode the path data from the sample page.
        String rawString = EncodingUtils.getAsciiString(byteArray);
        int startIndex = rawString.indexOf("<</Length 13 0 R/Filter /FlateDecode>>") + 46;
        int endIndex = rawString.indexOf("endstream", startIndex) - 1;
        byte[] flateEncodedByteArray = Arrays.copyOfRange(byteArray, startIndex, endIndex);
        net.sf.andpdf.nio.ByteBuffer flateEncodedBuffer = net.sf.andpdf.nio.ByteBuffer
                .NEW(flateEncodedByteArray);
        net.sf.andpdf.nio.ByteBuffer decodedBuffer = FlateDecode.decode(null, flateEncodedBuffer, null);
        String decodedString = new String(decodedBuffer.array(), "UTF-8");

        // Break every line of the decoded string into usable objects.
        String[] stringArray = decodedString.split("\\r?\\n");
        for (int i = 0; i < stringArray.length; i++) {
            String[] components = stringArray[i].split(" ");
            Path path = new Path();
            path.y1 = Float.valueOf(components[2]);
            path.x1 = Float.valueOf(components[3]);
            path.y2 = Float.valueOf(components[5]);
            path.x2 = Float.valueOf(components[6]);
            path.width = Float.valueOf(components[0]);
            paths.add(path);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return paths;
}