List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:control.HelperServlets.GenerarPdfServlet.java
public static String fileToString(File file) { byte[] fileBytes = new byte[0]; try {//from ww w . ja v a 2 s . c o m byte[] buffer = new byte[4096]; ByteArrayOutputStream outs = new ByteArrayOutputStream(); InputStream ins = new FileInputStream(file); int read = 0; while ((read = ins.read(buffer)) != -1) { outs.write(buffer, 0, read); } ins.close(); outs.close(); fileBytes = outs.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return new String(fileBytes); }
From source file:Main.java
public static byte[] getContents(Document document) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try {//from w ww. j a va 2 s .com print(new PrintStream(out, true, "UTF-8"), document); return out.toByteArray(); } catch (Exception ex) { throw new IOException(ex.getLocalizedMessage()); } finally { if (out != null) try { out.close(); } catch (Exception e) { // ignore } } }
From source file:de.bht.fpa.mail.s000000.common.mail.imapsync.MessageConverter.java
private static void handleInputStream(InputStream content, BodyPart bodyPart, MessageBuilder messageBuilder) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//from w w w .j a va2 s .c om int thisLine; while ((thisLine = content.read()) != -1) { bos.write(thisLine); } bos.flush(); byte[] bytes = bos.toByteArray(); bos.close(); String encodeBase64String = new String(Base64.encodeBase64(bytes)); // @formatter:off messageBuilder .attachment(newAttachmentBuilder().fileName(bodyPart.getFileName()).body(encodeBase64String)); // @formatter:on } catch (Exception e) { // ignore return; } }
From source file:com.wishlist.Utility.java
public static byte[] scaleImage(Context context, Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true;/* w ww .j a v a 2s . c o m*/ BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); float maxRatio = Math.max(widthRatio, heightRatio); // Create the bitmap from file BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); /* if the orientation is not 0 (or -1, which means we don't know), we * have to do a rotation. */ if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } String type = context.getContentResolver().getType(photoUri); 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); } byte[] bMapArray = baos.toByteArray(); baos.close(); return bMapArray; }
From source file:com.nloko.android.Utils.java
public static byte[] bitmapToPNG(Bitmap bitmap) { if (bitmap == null) { throw new IllegalArgumentException("bitmap"); }/* www .j a v a2 s. c om*/ byte[] image = null; try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); image = bytes.toByteArray(); bytes.close(); } catch (IOException e) { } return image; }
From source file:jedi.util.serialization.Pickle.java
/** * Serializes a object on the main memory and returns a sequence of bytes as a String. * /* w w w . j av a 2 s . c om*/ * @param o Object to be serialized. * @return String */ public static String dumps(Object o) { String s = ""; try { // Serializing. // Reference to a sequence of bytes on the memory. ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); // Converts a array of bytes into a String. // The default conversion doesn't works on the other hand Base64 works fine. s = new String(Base64.encodeBase64(baos.toByteArray())); baos.close(); } catch (IOException e) { e.printStackTrace(); } return s; }
From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java
/** * writes unchanged file to disk./*from w w w . j a va 2 s . c o m*/ * * @param stream - stream to read the file from * * @param destFile - file to write to * * @throws IOException when error occurs. */ private static void writeUntouchedImage(final InputStream stream, final File destFile) throws IOException { ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); byte[] buffer = new byte[MAX_BUFF_SIZE]; int readNum; while ((readNum = stream.read(buffer)) != -1) { byteArrayOS.write(buffer, 0, readNum); } byte[] bytes = byteArrayOS.toByteArray(); byteArrayOS.close(); FileOutputStream fileOS = new FileOutputStream(destFile); fileOS.write(bytes); fileOS.flush(); fileOS.close(); }
From source file:de.laures.cewolf.util.Renderer.java
/** * Renders a legend/*w w w.j av a2 s. co m*/ * @param cd the chart iamge to be rendred * @return the rendered image * @throws CewolfException */ private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException { try { JFreeChart chart = (JFreeChart) c; final int width = cd.getWidth(); final int height = cd.getHeight(); LegendTitle legend = getLegend(chart); boolean haslegend = true; // with JFreeChart v0.9.20, the only way to get a valid legend, // is either to retrieve it from the chart or to assign a new // one to the chart. In the case where the chart has no legend, // a new one must be assigned, but just for rendering. After, we // have to reset the legend to null in the chart. if (null == legend) { haslegend = false; legend = new LegendTitle(chart.getPlot()); } legend.setPosition(RectangleEdge.BOTTOM); BufferedImage bimage = ImageHelper.createImage(width, height); Graphics2D g = bimage.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); legend.arrange(g, new RectangleConstraint(width, height)); legend.draw(g, new Rectangle(width, height)); ByteArrayOutputStream out = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); param.setQuality(1.0f, true); encoder.encode(bimage, param); out.close(); // if the chart had no legend, reset it to null in order to give back the // chart in the state we received it. if (!haslegend) { removeLegend(chart); } return new RenderedImage(out.toByteArray(), "image/jpeg", new ChartRenderingInfo(new StandardEntityCollection())); } catch (IOException ioex) { log.error(ioex); throw new ChartRenderingException(ioex.getMessage(), ioex); } }
From source file:com.nloko.android.Utils.java
public static byte[] bitmapToJpeg(Bitmap bitmap, int quality) { if (bitmap == null) { throw new IllegalArgumentException("bitmap"); }//from w w w . j a v a2 s . c om byte[] image = null; try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bytes); image = bytes.toByteArray(); bytes.close(); } catch (IOException e) { } return image; }
From source file:com.asual.lesscss.ResourceUtils.java
public static byte[] readBinaryUrl(URL source) throws IOException { byte[] result; try {/*from ww w .j av a 2s .c o m*/ URLConnection urlc = source.openConnection(); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); InputStream input = urlc.getInputStream(); try { byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = input.read(buffer)) != -1) { byteStream.write(buffer, 0, bytesRead); } result = byteStream.toByteArray(); } finally { byteStream.close(); input.close(); } } catch (IOException e) { logger.error("Can't read '" + source.getFile() + "'."); throw e; } return result; }