Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

public static byte[] compress(byte[] data, int level) throws IOException {
    if (data == null || data.length == 0) {
        return data;
    }/*w w  w.  j av a2 s. c o  m*/
    ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
    Deflater deflater = new Deflater();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    byte[] buf = new byte[BUFFER_SIZE];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bout.write(buf, 0, count);
    }
    deflater.end();
    bout.close();
    return bout.toByteArray();
}

From source file:com.adaptris.core.MimeEncoder.java

/**
 * Get metadata out of an AdaptrisMessage
 *
 * @return a byte array representing the adaptris message
 *//*ww  w .  j a va2  s  . c om*/
private static byte[] getMetadata(AdaptrisMessage msg) throws IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Properties metadata = convertToProperties(msg.getMetadata());
    metadata.store(out, "");
    out.close();
    return out.toByteArray();
}

From source file:org.dataconservancy.ui.it.support.HttpAssert.java

/**
 * Free the HTTP connection by consuming the entity body of the response.  Will return a copy of the entity body
 * according to {@code copy}./* ww w .  ja v a  2 s  . co m*/
 *
 * @param response the HTTP response
 * @param copy     creates a copy of the entity body if true
 * @return an InputStream to the entity body if {@code copy} is true, null otherwise.
 */
private static InputStream freeAndCopy(HttpResponse response, boolean copy) {
    HttpEntity entity;
    if ((entity = response.getEntity()) != null) {
        try {
            final InputStream entityBody = entity.getContent();
            InputStream entityBodyCopy = null;
            if (copy) {
                ByteArrayOutputStream copyOut = new ByteArrayOutputStream(1024);
                IOUtils.copy(entityBody, copyOut);
                copyOut.close();
                entityBodyCopy = new ByteArrayInputStream(copyOut.toByteArray());
            }

            entityBody.close();
            return entityBodyCopy;
        } catch (IOException e) {
            // ignore
        }
    }

    return null;
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

public static byte[] compressString(String value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(value.length());
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(Base64.encode(value.getBytes(StandardCharsets.UTF_8)));
    gos.close();/*from  w ww .ja  v  a2s. c  o m*/
    byte[] compressed = baos.toByteArray();
    baos.close();
    return compressed;
}

From source file:Main.java

public static byte[] inputStream2ByteArray(InputStream is, int bufferSize) throws IOException {
    if (null == is) {
        return null;
    }/* w  w w  . jav  a 2s.  co  m*/
    if (bufferSize < 1) {
        bufferSize = 1;
    }
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    // this is storage overwritten on each iteration with bytes
    byte[] buffer = new byte[bufferSize];

    // we need to know how may bytes were read to write them to the
    // byteBuffer
    int len = 0;
    while ((len = is.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }

    byteBuffer.close();
    is.close();

    // and then we can return your byte array.
    return byteBuffer.toByteArray();
}

From source file:net.sourceforge.jweb.maven.mojo.InWarMinifyMojo.java

public static String buildStack(Throwable t) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(arrayOutputStream);
    t.printStackTrace(printStream);// ww w  .j av  a  2  s .  c o m
    String ret = arrayOutputStream.toString();
    try {
        arrayOutputStream.close();
        printStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:pt.ist.expenditureTrackingSystem.presentationTier.actions.statistics.ChartGenerator.java

protected static byte[] createBarChartImage(final ChartData chartData) throws RuntimeException, IOException {
    final JFreeChart chart = createBarChart(chartData.getDataset(), chartData.getTitle());
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final BufferedImage bufferedImage = chart.createBufferedImage(625, 475);
    ImageIO.write(bufferedImage, "png", outputStream);
    bufferedImage.flush();//from www. j a v a  2 s  . co  m
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:pt.ist.expenditureTrackingSystem.presentationTier.actions.statistics.ChartGenerator.java

protected static byte[] createBarChartImage(final CategoryDataset categoryDataset, final String title)
        throws RuntimeException, IOException {
    final JFreeChart chart = createBarChart(categoryDataset, title);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final BufferedImage bufferedImage = chart.createBufferedImage(625, 475);
    ImageIO.write(bufferedImage, "png", outputStream);
    bufferedImage.flush();//  www  .  ja  v  a  2s. com
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {/*from www . j ava2 s  . c om*/
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:Main.java

static public byte[] getBytes(File file) throws IOException {
    InputStream ios = null;/*  w  ww  . j a  va  2  s .  c o  m*/
    ByteArrayOutputStream ous = null;
    try {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(file);
        int read = 0;
        while ((read = ios.read(buffer)) != -1) {
            ous.write(buffer, 0, read);
        }
    } finally {
        try {
            if (ous != null)
                ous.close();
        } catch (IOException e) {
        }

        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }

    return ous.toByteArray();
}