List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param orig the original image/*from ww w .ja va 2 s.c o m*/ * @param maxHeight the max height of the scaled image * @param maxWidth the max width of the scaled image * @param preserveAspectRatio if true, the scaling preserves the aspect ratio of the original image * @param doHighQuality do higher quality thumbnail (slow) * @return the byte array of the scaled image * @throws IOException an error occurred while encoding the result as a JPEG image */ public static byte[] scaleImage(final BufferedImage orig, final int maxHeight, final int maxWidth, final boolean preserveAspectRatio, boolean doHighQuality) throws IOException { BufferedImage scaled; if (true) { int targetW = maxWidth; int targetH = maxHeight; if (preserveAspectRatio) { int origWidth = orig.getWidth(); int origHeight = orig.getHeight(); double origRatio = (double) origWidth / (double) origHeight; double scaledRatio = (double) maxWidth / (double) maxHeight; if (origRatio > scaledRatio) { targetH = (int) (targetW / origRatio); } else { targetW = (int) (targetH * origRatio); } } scaled = getScaledInstance(orig, targetW, targetH, doHighQuality); } else { scaled = generateScaledImage(orig, RenderingHints.VALUE_INTERPOLATION_BILINEAR, Math.max(maxHeight, maxWidth)); } ByteArrayOutputStream output = new ByteArrayOutputStream(8192); ImageIO.write(scaled, "jpeg", output); byte[] outputBytes = output.toByteArray(); output.close(); return outputBytes; }
From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java
static byte[] compress(String string) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes("UTF-8")); gos.close();/*from w ww.j a va2 s . co m*/ byte[] compressed = os.toByteArray(); os.close(); return compressed; }
From source file:com.aurel.track.admin.customize.lists.BlobBL.java
/** * This method returns the iconKey content in byte array.In case of not founds returns the default image content. * @param iconKey//from w w w . j a v a 2 s. c om * @param iconName * @param inline * @param defaultIconPath * @return */ public static byte[] getAvatarInByteArray(Integer iconKey, String iconName, boolean inline, String defaultIconPath) { byte[] imageContent = null; if (iconKey != null) { TBLOBBean blobBean = loadByPrimaryKey(iconKey); if (blobBean != null) { imageContent = blobBean.getBLOBValue(); } } if (defaultIconPath != null) { if (imageContent == null || imageContent.length == 0) { try { URL defaultIconURL = ApplicationBean.getInstance().getServletContext() .getResource(defaultIconPath); imageContent = IOUtils.toByteArray(defaultIconURL.openStream()); } catch (IOException e) { LOGGER.info("Getting the icon content for " + defaultIconPath + " from URL failed with " + e.getMessage()); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } if (imageContent == null || imageContent.length == 0) { InputStream is = ApplicationBean.getInstance().getServletContext() .getResourceAsStream(defaultIconPath); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { byte[] buf = new byte[1024]; int numBytesRead; while ((numBytesRead = is.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } imageContent = output.toByteArray(); output.close(); is.close(); } catch (IOException e) { LOGGER.info("Getting the icon content for " + defaultIconPath + " as resource stream failed with " + e.getMessage()); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } } return imageContent; }
From source file:com.aurel.track.admin.customize.lists.BlobBL.java
/** * Download the icon if exists. If there is no icon set then the default * blank icon will be rendered/*from w ww . j a v a 2 s . c o m*/ * * @param iconKey * @param iconName * @param request * @param reponse * @param inline */ public static void download(Integer iconKey, String iconName, HttpServletRequest request, HttpServletResponse reponse, boolean inline, String defaultIconPath) { byte[] imageContent = null; if (iconKey != null) { TBLOBBean blobBean = loadByPrimaryKey(iconKey); if (blobBean != null) { imageContent = blobBean.getBLOBValue(); } } if (defaultIconPath != null) { if (imageContent == null || imageContent.length == 0) { try { URL defaultIconURL = ApplicationBean.getInstance().getServletContext() .getResource(defaultIconPath); if (defaultIconURL != null) { imageContent = IOUtils.toByteArray(defaultIconURL.openStream()); } } catch (IOException e) { LOGGER.info("Getting the icon content for " + defaultIconPath + " from URL failed with " + e.getMessage()); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } if (imageContent == null || imageContent.length == 0) { InputStream is = ApplicationBean.getInstance().getServletContext() .getResourceAsStream(defaultIconPath); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { byte[] buf = new byte[1024]; int numBytesRead; while ((numBytesRead = is.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } imageContent = output.toByteArray(); output.close(); is.close(); } catch (IOException e) { LOGGER.info("Getting the icon content for " + defaultIconPath + " as resource stream failed with " + e.getMessage()); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } } if (imageContent != null) { download(imageContent, request, reponse, iconName, inline); } }
From source file:de.unwesen.packrat.api.APIBase.java
/** * Converts an InputStream to a byte array containing the InputStream's content. **//*from ww w. java 2s. c om*/ public static byte[] readStreamRaw(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(READ_BUFFER_SIZE); byte[] bytes = new byte[READ_BUFFER_SIZE]; try { // Read bytes from the input stream in chunks and write // them into the output stream int bytes_read = 0; while (-1 != (bytes_read = is.read(bytes))) { os.write(bytes, 0, bytes_read); } byte[] retval = os.toByteArray(); is.close(); os.close(); return retval; } catch (java.io.IOException ex) { Log.e(LTAG, "Could not read input stream: " + ex.getMessage()); } return null; }
From source file:net.brtly.monkeyboard.api.plugin.Bundle.java
/** * Convert a Bundle object to a byte array that can be deserialized by * {@link #fromByteArray(byte[])}/*from www .ja v a 2s . c om*/ * * @param b * @return * @throws IOException */ public static byte[] toByteArray(Bundle b) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(b); return bos.toByteArray(); } finally { out.close(); bos.close(); } }
From source file:com.example.android.wearable.mjpegviewwear.MainActivity.java
/** * Builds an {@link com.google.android.gms.wearable.Asset} from a bitmap. The image that we get * back from the camera in "data" is a thumbnail size. Typically, your image should not exceed * 320x320 and if you want to have zoom and parallax effect in your app, limit the size of your * image to 640x400. Resize your image before transferring to your wearable device. *//*from ww w . j a v a2s . c o m*/ private static Asset toAsset(Bitmap bitmap) { ByteArrayOutputStream byteStream = null; try { byteStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteStream); return Asset.createFromBytes(byteStream.toByteArray()); } finally { if (null != byteStream) { try { byteStream.close(); } catch (IOException e) { // ignore } } } }
From source file:Main.java
public static boolean saveJPEGBitmap(Bitmap aBmp, String aPath) { if (aBmp == null || aPath == null) { return false; }/*from w w w . j a v a 2 s. co m*/ FileOutputStream fos = null; ByteArrayOutputStream baos = null; boolean result = false; try { File file = new File(aPath); if (!file.exists()) { file.createNewFile(); } fos = new FileOutputStream(file); baos = new ByteArrayOutputStream(); aBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); //SUPPRESS CHECKSTYLE fos.write(baos.toByteArray()); baos.flush(); fos.flush(); result = true; } catch (Error e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); result = false; } finally { try { if (baos != null) { baos.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:org.kiji.scoring.server.KijiScoringServerCell.java
/** * Returns an encoded JSON string for the given object. * * @param input datum to encode as a JSON string. * @return the JSON string representing this object. * * @throws IOException if there is an error encoding the datum. *//* w w w . j a va2s . com*/ private static String getJsonString(final Object input) throws IOException { final String jsonString; if (input instanceof GenericContainer) { final GenericContainer record = (GenericContainer) input; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), baos); if (record instanceof SpecificRecord) { new SpecificDatumWriter<GenericContainer>(record.getSchema()).write(record, encoder); } else { new GenericDatumWriter<GenericContainer>(record.getSchema()).write(record, encoder); } encoder.flush(); jsonString = new String(baos.toByteArray(), Charset.forName("UTF-8")); } finally { baos.close(); } } else { jsonString = input.toString(); } return jsonString; }
From source file:com.appassit.common.Utils.java
/** * <p>/*from www . j a va 2s. com*/ * Get UTF8 bytes from a string * </p> * * @param string * String * @return UTF8 byte array, or null if failed to get UTF8 byte array */ public static byte[] getUTF8Bytes(String string) { if (string == null) return new byte[0]; try { return string.getBytes(ENCODING_UTF8); } catch (UnsupportedEncodingException e) { /* * If system doesn't support UTF-8, use another way */ try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeUTF(string); byte[] jdata = bos.toByteArray(); bos.close(); dos.close(); byte[] buff = new byte[jdata.length - 2]; System.arraycopy(jdata, 2, buff, 0, buff.length); return buff; } catch (IOException ex) { return new byte[0]; } } }