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:cn.sharesdk.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;//from   ww w . j  av  a 2  s  . co  m
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);
    } catch (IOException e) {
        e.printStackTrace();
        Ln.i("NetworkHelper", "Base64Gzip == >>", e);
    }
    return result;
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Decode a safeEncode() string.//from w w w .  j av  a  2 s.  co m
 * 
 * @param encodedWebsafeString
 * @return rawString
 */
public static String safeDecode(String encodedWebsafeString) {
    if (encodedWebsafeString == null || encodedWebsafeString.length() == 0) {
        return encodedWebsafeString;
    }

    try {
        ByteArrayInputStream in = new ByteArrayInputStream(
                Base64.decodeBase64(encodedWebsafeString.getBytes(CharEncoding.UTF_8)));
        GZIPInputStream gzip = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int ch = gzip.read();
        while (ch >= 0) {
            out.write(ch);
            ch = gzip.read();
        }
        gzip.close();
        out.flush();
        out.close();
        return new String(out.toByteArray(), CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    }
}

From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Decompresses a string./*from  w  w  w .  ja  va2 s  .c om*/
 * 
 * @param s
 *            The string to decompress
 * @return The decompressed string
 */
public static String decompress(String s) {
    ByteArrayOutputStream baos = null;
    try {
        Inflater ifl = new Inflater();
        ifl.setInput(Base64.decodeBase64(s));

        baos = new ByteArrayOutputStream();
        while (!ifl.finished()) {
            byte[] buff = new byte[1024];
            int count = ifl.inflate(buff);
            baos.write(buff, 0, count);
        }
        baos.flush();
        byte[] output = baos.toByteArray();

        return new String(output, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } catch (DataFormatException e) {
        logger.error(e.getMessage(), e);
    } finally {
        ClosingUtils.close(baos);
    }
    return "";
}

From source file:com.microsoft.intellij.helpers.IDEHelperImpl.java

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

    int nRead;/* w  ww. j  a v a2  s. c  om*/
    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:Main.java

/**
 * Read and return the entire contents of the supplied {@link InputStream stream}. This method always closes the stream when
 * finished reading./*  w ww  .java 2 s .  c om*/
 * 
 * @param stream the stream to the contents; may be null
 * @return the contents, or an empty byte array if the supplied reader is null
 * @throws IOException if there is an error reading the content
 */
public static byte[] readBytes(InputStream stream) throws IOException {
    if (stream == null)
        return new byte[] {};
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    boolean error = false;
    try {
        int numRead = 0;
        while ((numRead = stream.read(buffer)) > -1) {
            output.write(buffer, 0, numRead);
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            if (!error)
                throw e;
        }
    }
    output.flush();
    return output.toByteArray();
}

From source file:es.sm2.openppm.core.plugin.action.GenericAction.java

/**
 *
 * @param files/*from  w w  w .j  a v  a2  s  .  com*/
 * @return
 * @throws IOException
 */
public static byte[] zipFiles(List<File> files) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    for (File f : files) {

        zos.putNextEntry(new ZipEntry(f.getName()));

        zos.write(getBytesFromFile(f.getAbsoluteFile()));

        zos.closeEntry();
    }

    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}

From source file:Main.java

public static String doGet(String urlStr) {
    URL url = null;//from w w w .  j av  a 2s .co m
    HttpURLConnection conn = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
        conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        if (conn.getResponseCode() == 200) {
            is = conn.getInputStream();
            baos = new ByteArrayOutputStream();
            int len = -1;
            byte[] buf = new byte[128];

            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            baos.flush();
            return baos.toString();
        } else {
            throw new RuntimeException(" responseCode is not 200 ... ");
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
        try {
            if (baos != null)
                baos.close();
        } catch (IOException e) {
        }
        conn.disconnect();
    }

    return null;

}

From source file:Main.java

public static byte[] doGet(String urlStr) {
    URL url = null;//  w w w .j  a v  a 2s . c  o m
    HttpURLConnection conn = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
        conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        if (conn.getResponseCode() == 200) {
            is = conn.getInputStream();
            baos = new ByteArrayOutputStream();
            int len = -1;
            byte[] buf = new byte[128];

            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            baos.flush();
            return baos.toByteArray();
        } else {
            throw new RuntimeException(" responseCode is not 200 ... ");
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
        try {
            if (baos != null)
                baos.close();
        } catch (IOException e) {
        }
        conn.disconnect();
    }

    return null;

}

From source file:Main.java

public static byte[] readBytesFormZipFile(String fileName, String fname) {
    byte[] bytes = null;
    ByteArrayOutputStream os = null;
    ZipInputStream in = null;/*from  w ww.  j a  v a 2s  .  c  om*/
    try {
        os = new ByteArrayOutputStream(4096);
        in = new ZipInputStream(new FileInputStream(fileName));
        boolean found = false;
        while (!found) {
            ZipEntry entry = in.getNextEntry();
            if (fname.equalsIgnoreCase(entry.getName())) {
                found = true;
            }
        }
        int read;
        byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) >= 0) {
            os.write(buffer, 0, read);
        }
        bytes = os.toByteArray();
    } catch (Exception e) {
        bytes = null;
    } finally {
        try {
            if (os != null) {
                os.flush();
                os = null;
            }
            if (in != null) {
                in.close();
                in = null;
            }
        } catch (Exception e) {
        }
    }

    return bytes;
}

From source file:Main.java

public static ByteArrayOutputStream loadFile(File root) {
    String inputString;//from ww w  .  j  ava 2s. c o  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        for (File child : root.listFiles()) {
            if (root.isDirectory()) {
                if (child.exists()) {
                    FileInputStream fis;
                    fis = globalContext.openFileInput(child.getName().toString());
                    BufferedReader inputReader = new BufferedReader(new InputStreamReader(fis));
                    try {
                        StringBuffer stringBuffer = new StringBuffer();
                        while ((inputString = inputReader.readLine()) != null) {
                            stringBuffer.append(inputString);
                            baos.write(inputString.getBytes());
                        }
                        baos.flush();

                        connectAndSendHttp(baos);
                    } catch (IOException e) {
                        Log.e("tag", e.getMessage());
                    }
                }
            }
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    return baos;
}