List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:edu.vt.middleware.ldap.LdapUtil.java
/** * Reads the data in the supplied stream and returns it as a byte array. * * @param is stream to read/*from w w w.j a v a2 s . c o m*/ * * @return bytes read from the stream * * @throws IOException if an error occurs reading data */ public static byte[] readInputStream(final InputStream is) throws IOException { final ByteArrayOutputStream data = new ByteArrayOutputStream(); try { final byte[] buffer = new byte[READ_BUFFER_SIZE]; int length; while ((length = is.read(buffer)) != -1) { data.write(buffer, 0, length); } } finally { is.close(); data.close(); } return data.toByteArray(); }
From source file:GZIPUtils.java
/** * Returns an gunzipped copy of the input array. * @throws IOException if the input cannot be properly decompressed *///from ww w . ja v a 2 s . co m 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:com.vaadin.testbench.screenshot.ImageUtil.java
/** * Encodes target image to a Base64 string * //from w ww.j av a 2 s. c o m * @param image * BufferedImage to encode to String * @return Base64 encoded String of image */ public static String encodeImageToBase64(BufferedImage image) { String encodedImage = ""; Base64 encoder = new Base64(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); baos.flush(); byte[] encodedBytes = encoder.encode(baos.toByteArray()); encodedImage = new String(encodedBytes); baos.close(); } catch (IOException e) { e.printStackTrace(); } return encodedImage; }
From source file:divconq.util.IOUtil.java
public static byte[] charsEncodeAndCompress(CharSequence v) { try {/* w ww.j a v a2 s. co m*/ byte[] buffer = Utf8Encoder.encode(v); ByteArrayOutputStream bos = new ByteArrayOutputStream(); GzipCompressorOutputStream cos = new GzipCompressorOutputStream(bos); cos.write(buffer); cos.close(); bos.close(); return bos.toByteArray(); } catch (Exception x) { } return null; }
From source file:br.com.manish.ahy.kernel.util.FileUtil.java
public static byte[] readResourceAsBytes(String path) { byte[] byteData = null; try {//from w ww. j a va 2 s .co m InputStream is = FileUtil.class.getResourceAsStream(path); if (is.available() > Integer.MAX_VALUE) { throw new OopsException("Oversized file :-( can't read it, sorry: " + path); } ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] bytes = new byte[512]; int readBytes; while ((readBytes = is.read(bytes)) > 0) { os.write(bytes, 0, readBytes); } byteData = os.toByteArray(); is.close(); os.close(); } catch (Exception e) { throw new OopsException(e, "Problems when reading: [" + path + "]."); } return byteData; }
From source file:com.faceye.feature.util.http.DeflateUtils.java
/** * Returns an inflated copy of the input array. * @throws IOException if the input cannot be properly decompressed *///from w ww. j a va2 s .c om public static final byte[] inflate(byte[] in) throws IOException { // decompress using InflaterInputStream ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length); InflaterInputStream inStream = new InflaterInputStream(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[] decompress(byte[] data, int off, int len, int srcLen) { byte[] output = null; Inflater decompresser = new Inflater(); decompresser.reset();/*from w ww . j a v a 2 s. co m*/ // decompresser.setInput(data); decompresser.setInput(data, off, len); ByteArrayOutputStream o = new ByteArrayOutputStream(srcLen); try { o.reset(); byte[] buf = new byte[1024]; while (!decompresser.finished()) { int i = decompresser.inflate(buf); o.write(buf, 0, i); } output = o.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } finally { try { o.close(); decompresser.end(); } catch (Exception e) { } } return output; }
From source file:Main.java
public static Bitmap getAndSetBitmapFromNet(String urlPath) { Bitmap bm = null;/*from w ww. j a v a 2 s.c o m*/ if (urlPath != null) { try { BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024); copy(bis, out); out.flush(); final byte[] data = dataStream.toByteArray(); bm = BitmapFactory.decodeByteArray(data, 0, data.length); Log.i(I, "data.length: " + data.length); out.close(); dataStream.close(); bis.close(); bm = processBitmap(bm); } catch (IOException e) { Log.i(I, "URL Connection or Bitmap processing Exception"); e.printStackTrace(); } } return bm; }
From source file:Main.java
public static byte[] toByteArray(InputStream istream) { ByteArrayOutputStream byteostream = null; try {/*from w w w . j av a 2 s . c om*/ byteostream = new ByteArrayOutputStream(8192); byte[] buffer = new byte[8192]; int lenght; while ((lenght = istream.read(buffer)) != -1) { byteostream.write(buffer, 0, lenght); } return byteostream.toByteArray(); } catch (Exception e) { return null; } finally { try { istream.close(); byteostream.close(); } catch (Exception e) { } } }
From source file:Main.java
public static byte[] compress(byte[] uncompressedBuffer) { Deflater deflater = new Deflater(); deflater.setInput(uncompressedBuffer); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length); try {/*from w w w . ja v a 2 s . com*/ deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } byte[] output = outputStream.toByteArray(); return output; } finally { try { deflater.end(); outputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }