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:com.beligum.core.utils.AssetPacker.java

private static byte[] encodeGzip(String content) throws IOException {
    byte[] retVal = null;

    ByteArrayOutputStream out = null;
    GZIPOutputStream gzip = null;
    try {/*from  w ww. j  a  v  a 2s . c o m*/
        out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);
        gzip.write(content.getBytes("UTF-8"));
        gzip.close();
        out.close();

        retVal = out.toByteArray();
    } catch (Exception e) {
        if (gzip != null) {
            gzip.close();
        }
        if (out != null) {
            out.close();
        }
    }

    return retVal;
}

From source file:com.github.ipaas.ideploy.agent.util.SVNUtil.java

/**
 *  /*www  . j a  v a 2  s  .co  m*/
 * @param repository  svn repos
 * @param remotePath  svn
 * @param savePath    ??
 * @param revision    
 * @throws Exception
 */
private static void getFile(SVNRepository repository, String remotePath, File savePath, long revision)
        throws Exception {
    //
    FileUtils.deleteQuietly(savePath);

    //
    savePath.getParentFile().mkdirs();

    //
    savePath.createNewFile();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileOutputStream fos = new FileOutputStream(savePath);
    try {
        repository.getFile(remotePath, revision, null, baos);
        baos.writeTo(fos);
    } finally {
        if (fos != null) {
            fos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:com.money.manager.ex.utils.MmxFileUtils.java

/**
 * @param context application//  ww w .j  a  va  2s.c  o  m
 * @param resId:  rawid resources
 * @return String: String file
 */
public static String loadRaw(Context context, int resId) {
    String result = null;
    // take input stream
    InputStream is = context.getResources().openRawResource(resId);
    if (is != null) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[BUFFER_DIMENSION];
        int numRead = 0;
        try {
            while ((numRead = is.read(buffer)) >= 0) {
                outputStream.write(buffer, 0, numRead);
            }
            // convert to string
            result = new String(outputStream.toByteArray());
        } catch (IOException e) {
            Timber.e(e, "loadRaw");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    Timber.e(e, "close byte array");
                }
            }
        }
    }
    return result;
}

From source file:com.jaspersoft.jasperserver.util.JasperSerializationUtil.java

/**
 * Serialize to a byte array/* w  w w.  ja  v a 2 s  . c  o  m*/
 * @param input
 * @return
 */
public static byte[] serialize(Object input) {

    byte[] output = null;
    Exception exp = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter serialize .. Start Time" + System.currentTimeMillis());
        }

        oos = new ObjectOutputStream(bos);
        oos.writeObject(input);
        output = bos.toByteArray();
    } catch (IOException e) {
        exp = e;
    } finally {
        try {
            if (null != oos) {
                oos.close();
            }
            bos.close();
        } catch (IOException e) {
        }
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit serialize .. Total Time Spent: " + elapsedTime);
        }

        if (null != exp) {
            throw new RuntimeException(exp);
        }
    }

    return output;
}

From source file:edu.auburn.ppl.cyclecolumbus.TripUploader.java

/******************************************************************************************
 * Compresses the string to send to the server into byte array
 ******************************************************************************************
 * @param string String to compress//w ww .jav a2 s.  c om
 * @return Byte array to send to server
 * @throws IOException
 ******************************************************************************************/
public static byte[] compress(String string) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();
    return compressed;
}

From source file:aarddict.Volume.java

static String decompressZlib(byte[] bytes) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);//from   w w w.j ava 2s . c om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            out.write(buf, 0, count);
        }
    } finally {
        out.close();
    }
    return utf8(out.toByteArray());
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * @param name//  w w  w.  j a v  a 2s . c  om
 * @param imgIcon
 * @return
 */
public static String uuencodeImage(final String name, final ImageIcon imgIcon) {
    try {
        BufferedImage tmp = new BufferedImage(imgIcon.getIconWidth(), imgIcon.getIconWidth(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = tmp.createGraphics();
        //g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(imgIcon.getImage(), 0, 0, imgIcon.getIconWidth(), imgIcon.getIconWidth(), null);
        g2.dispose();

        ByteArrayOutputStream output = new ByteArrayOutputStream(8192);
        ImageIO.write(tmp, "PNG", output);
        byte[] outputBytes = output.toByteArray();
        output.close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        UUEncoder uuencode = new UUEncoder(name);
        uuencode.encode(new ByteArrayInputStream(outputBytes), bos);
        return bos.toString();

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GraphicsUtils.class, ex);
        ex.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static Bitmap loadImageFromUrl(String url) {
    ByteArrayOutputStream out = null;
    Bitmap bitmap = null;/* ww w  .j a  v  a2s  .com*/
    int BUFFER_SIZE = 1024 * 8;
    try {
        BufferedInputStream in = new BufferedInputStream(new URL(url).openStream(), BUFFER_SIZE);
        out = new ByteArrayOutputStream(BUFFER_SIZE);
        int length = 0;
        byte[] tem = new byte[BUFFER_SIZE];
        length = in.read(tem);
        while (length != -1) {
            out.write(tem, 0, length);
            out.flush();
            length = in.read(tem);
        }
        in.close();
        if (out.toByteArray().length != 0) {
            bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
        } else {
            out.close();
            return null;
        }
        out.close();
    } catch (OutOfMemoryError e) {

        out.reset();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2;
        opts.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size(), opts);
        return bitmap;
    } catch (Exception e) {

        return bitmap;
    }
    return bitmap;
}

From source file:ZipUtil.java

/**
 * Deflates the file and returns the deflated file.
 *//*from  w w w.j a  v  a2s. c om*/
public static byte[] zipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;
    Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
    oDeflate.setInput(file);
    oDeflate.finish();
    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (!oDeflate.finished()) {
            byte[] byRead = new byte[ZIP_BUFFER_SIZE];
            int iBytesRead = oDeflate.deflate(byRead);
            if (iBytesRead == byRead.length) {
                oZipStream.write(byRead);
            } else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        oDeflate.end();
        byReturn = oZipStream.toByteArray();
    } finally {
        oZipStream.close();
    }
    return byReturn;
}

From source file:com.handany.base.generator.FreemarkerUtil.java

public static void outputProcessResult(String outputFile, Template template, Map<String, Object> varMap) {
    String resultString;//  w  w w  . j  a  v a2  s.  c o  m
    ByteArrayOutputStream baos = null;
    Writer writer = null;

    try {
        baos = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(baos, CHARSET);
        template.process(varMap, writer);
        resultString = new String(baos.toByteArray(), CHARSET);
        FileUtils.writeStringToFile(new File(outputFile), resultString, CHARSET);
    } catch (UnsupportedEncodingException ex) {
        log.error(ex.getMessage(), ex);
    } catch (IOException | TemplateException ex) {
        log.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    } finally {
        if (null != baos) {
            try {
                baos.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }
    }
}