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.analysis.net.NetworkHelper.java

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

    String result = null;/*from  w w w  .j  a  va 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.e("NetworkHelper", "Base64Gzip == >>", e);
    }

    //Ln.i("after base64gizp", result);
    return result;
}

From source file:lapin.load.Loader.java

static private byte[] decompress(byte[] bytes) throws IOException {
    ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
    GZIPInputStream gin = new GZIPInputStream(bin);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    int b;// www.j a v a2  s  .c  o  m
    while ((b = gin.read()) != -1)
        bout.write(b);
    bout.flush();
    IO.close(gin);
    IO.close(bin);
    IO.close(bout);
    return bout.toByteArray();
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ??byte[]//from  w  w  w  .  ja  v  a 2 s  . c o  m
 */
public static byte[] read(InputStream input) throws IOException {
    ByteArrayOutputStream output = null;
    try {
        output = new ByteArrayOutputStream();
        copy(input, output);
        output.flush();
        return output.toByteArray();
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:net.minecraftforge.fml.relauncher.libraries.LibraryManager.java

private static byte[] readAll(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int read = -1;
    byte[] data = new byte[1024 * 16];

    while ((read = in.read(data, 0, data.length)) != -1)
        out.write(data, 0, read);/*from  w  ww.ja v a2 s. c o  m*/

    out.flush();
    return out.toByteArray();
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Stores BufferedImage as JPEG encoded bytestream
 *
 * @param bi/*from  ww  w  .  j  a v a2  s  .c  o m*/
 * @return
 * @throws IOException
 */
public static byte[] encodeBufferedImageAsJPEG(BufferedImage bi) throws IOException {

    byte[] byteStream = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ImageIO.write(bi, "jpeg", baos);
    baos.flush();
    byteStream = baos.toByteArray();
    baos.close();

    return byteStream;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Stores BufferedImage as JPEG2000 encoded bytestream
 *
 * @param bi//  w  w  w. j  a v a2  s. c o m
 * @return
 * @throws IOException
 */
public static byte[] encodeBufferedImageAsJP2(BufferedImage bi) throws IOException {

    byte[] byteStream = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ImageIO.write(bi, "jpeg2000", baos);
    baos.flush();
    byteStream = baos.toByteArray();
    baos.close();

    return byteStream;
}

From source file:de.kapsi.net.daap.DaapUtil.java

/**
 * Serializes the <code>chunk</code> and compresses it optionally.
 * The serialized data is returned as a byte-Array.
 *//*  w w  w  . j a  v a  2 s . co m*/
public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(chunk.getSize());

    if (compress) {
        GZIPOutputStream gzip = new GZIPOutputStream(buffer);
        chunk.serialize(gzip);
        gzip.finish();
        gzip.close();
    } else {
        chunk.serialize(buffer);
        buffer.flush();
        buffer.close();
    }

    return buffer.toByteArray();
}

From source file:com.streamsets.pipeline.lib.util.ProtobufTestUtil.java

public static byte[] getProtoBufData() throws IOException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    for (int i = 0; i < 10; i++) {
        getSingleProtobufData(bOut, i);/* w w w  . j av  a 2  s. c  o m*/
    }
    bOut.flush();
    return bOut.toByteArray();
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ?// w w w.  ja v a 2  s  .c  o m
 */
public static byte[] read(File sourceFile) throws IOException {
    if (sourceFile == null) {
        throw new IOException("Source must not be null");
    }

    if (false == sourceFile.exists()) {
        throw new IOException("Source '" + sourceFile + "' does not exist");
    }

    if (true == sourceFile.isDirectory()) {
        throw new IOException("Source '" + sourceFile + "' exists but is a directory");
    }

    FileInputStream input = null;
    ByteArrayOutputStream output = null;

    try {
        input = new FileInputStream(sourceFile);
        output = new ByteArrayOutputStream();
        copy(input, output);
        output.flush();
        return output.toByteArray();
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Encode a SAML2 request for the HTTP-POST binding.
 * //w w  w . j av a2s. c  o m
 * @param signable
 *            the request
 * @return the encoded request
 * @throws IOException
 */
public static String httpPostEncode(final String samlMessage) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = null;
    String base64EncodedRequest = null;

    try {
        byteArrayOutputStream = new ByteArrayOutputStream();

        // Base 64 Encoded Only for HTTP POST binding
        byteArrayOutputStream.write(samlMessage.getBytes());
        byteArrayOutputStream.flush();
        base64EncodedRequest = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);

        if (SamlHelper.LOGGER.isDebugEnabled()) {
            SamlHelper.LOGGER.debug(String.format("SAML 2.0 Request: %s", samlMessage));
            SamlHelper.LOGGER.debug(String.format("Encoded HTTP-POST Request: %s", base64EncodedRequest));
        }
    } finally {
        if (byteArrayOutputStream != null) {
            byteArrayOutputStream.close();
        }
    }

    return base64EncodedRequest;
}