List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:es.caib.sgtsic.util.Zips.java
public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(baos); for (String key : documents.keySet()) { InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key))); ZipEntry zipEntry = new ZipEntry(key); zip.putNextEntry(zipEntry);//from w w w . j a v a2 s. c o m IOUtils.copy(is, zip); zip.closeEntry(); is.close(); } zip.close(); baos.close(); return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip"); }
From source file:Main.java
public static byte[] bitmapToByteArray(Bitmap srcBitmap, String type) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); }/*from w w w .j a va2 s. c o m*/ byte[] bMapArray = baos.toByteArray(); baos.close(); return bMapArray; }
From source file:com.qatickets.common.ZIPHelper.java
public static byte[] decompressAsBytes(byte[] data) { if (data == null) { return null; }//ww w. j ava 2 s . com // Create the decompressor and give it the data to compress final Inflater decompressor = new Inflater(); decompressor.setInput(data); // Create an expandable byte array to hold the decompressed data final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); // Decompress the data final byte[] buf = new byte[1024]; while (!decompressor.finished()) { try { final int count = decompressor.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { } } try { bos.close(); } catch (IOException e) { } // Get the decompressed data final byte[] decompressedData = bos.toByteArray(); decompressor.end(); return decompressedData; }
From source file:com.igormaznitsa.jcp.expression.functions.FunctionBINFILE.java
@Nonnull private static byte[] deflate(@Nonnull final byte[] data) throws IOException { final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); deflater.setInput(data);/*w w w.j a va 2 s . co m*/ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); final byte[] buffer = new byte[1024]; while (!deflater.finished()) { final int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); final byte[] output = outputStream.toByteArray(); deflater.end(); return output; }
From source file:com.kcs.core.utilities.Utility.java
public static byte[] generateToBytes(Object jaxbElement) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); generate(jaxbElement, output);/*from www . j a v a 2 s.c o m*/ output.flush(); output.close(); return output.toByteArray(); }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] cropBufferedImage(boolean isPNG, BufferedImage originalImage, int x, int y, int width, int height) throws IOException { byte[] imageFinal = null; if (originalImage != null) { // step 1: crop BufferedImage thumbnail = Scalr.crop(originalImage, x, y, width, height, Scalr.OP_ANTIALIAS); // step 2. convert BufferedImage to byte[]. String extension = ""; if (isPNG == true) { extension = "png"; } else {//from ww w. j a v a 2s.c o m extension = "jpg"; } ByteArrayOutputStream outstream = new ByteArrayOutputStream(); ImageIO.write(thumbnail, extension, outstream); outstream.flush(); imageFinal = outstream.toByteArray(); outstream.close(); } return imageFinal; }
From source file:Main.java
public static byte[] getBytes(Serializable obj) { byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try {//from w w w.jav a 2 s . c om oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (oos != null) { try { oos.close(); bos.close(); bos.close(); } catch (Exception e) { e.printStackTrace(); } } } return bytes; }
From source file:Main.java
public static byte[] getBytesFromFile(File f) { if (f == null) { return null; }//from w ww . ja v a 2 s . c o m FileInputStream stream = null; ByteArrayOutputStream out = null; try { stream = new FileInputStream(f); out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = stream.read(b)) != -1) out.write(b, 0, n); return out.toByteArray(); } catch (IOException e) { } finally { try { if (stream != null) stream.close(); if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:com.ery.ertc.estorm.util.GZIPUtils.java
/** * Returns an gunzipped copy of the input array. * //from ww w . j a v a 2s .co m * @throws IOException * if the input cannot be properly decompressed */ public static final byte[] unzip(byte[] in) throws IOException { // decompress using GZIPInputStream ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length); GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in)); byte[] buf = new byte[BUF_SIZE]; while (true) { int size = inStream.read(buf); if (size <= 0) break; outStream.write(buf, 0, size); } outStream.close(); return outStream.toByteArray(); }
From source file:Main.java
public static byte[] read(InputStream stream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try {//from w w w . ja v a 2 s. c om byte[] buffer = new byte[1024]; int length; while ((length = stream.read(buffer)) >= 0) { byteArrayOutputStream.write(buffer, 0, length); } stream.close(); return byteArrayOutputStream.toByteArray(); } finally { byteArrayOutputStream.flush(); byteArrayOutputStream.close(); } }