List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public static File saveBitmapToExternal(Bitmap bitmap, String targetFileDirPath) { if (bitmap == null || bitmap.isRecycled()) { return null; }/*w w w. ja v a 2s . c om*/ File targetFileDir = new File(targetFileDirPath); if (!targetFileDir.exists() && !targetFileDir.mkdirs()) { return null; } File targetFile = new File(targetFileDir, UUID.randomUUID().toString()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); FileOutputStream fos = null; try { fos = new FileOutputStream(targetFile); baos.writeTo(fos); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fos != null) { fos.flush(); fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return targetFile; }
From source file:kevin.gvmsgarch.App.java
private static String makeStringFromStream(InputStream stream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/* ww w . j av a2 s . c o m*/ int bytesRead = -1; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = stream.read(buffer)) > -1) { baos.write(buffer, 0, bytesRead); } return new String(baos.toByteArray()); } finally { baos.close(); } }
From source file:org.alfresco.bm.tools.BMTestRunner.java
/** * Helper method to extract the csv results to a string that can be output in the logs *///from ww w.j a va2 s. co m public static String getResultsCSV(ResultsRestAPI resultsAPI) { // Get the summary CSV results for the time period and check some of the values StreamingOutput out = resultsAPI.getReportCSV(); ByteArrayOutputStream bos = new ByteArrayOutputStream(2048); String summary = ""; try { out.write(bos); summary = bos.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException(e); } finally { try { bos.close(); } catch (Exception e) { } } if (logger.isDebugEnabled()) { logger.debug("BM000X summary report: \n" + summary); } return summary; }
From source file:com.facebook.infrastructure.utils.FBUtilities.java
public static byte[] serializeToStream(Object o) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = ArrayUtils.EMPTY_BYTE_ARRAY; try {// w w w . j ava 2 s . c om ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException e) { LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e)); } return bytes; }
From source file:com.spstudio.common.image.ImageUtils.java
/** * byte[]/*from w w w .j a v a 2 s .co m*/ * * @param ?? * @return ?? */ public static byte[] compress(byte[] data) { System.out.println("before:" + data.length); GZIPOutputStream gzip = null; ByteArrayOutputStream baos = null; byte[] newData = null; try { baos = new ByteArrayOutputStream(); gzip = new GZIPOutputStream(baos); gzip.write(data); gzip.finish(); gzip.flush(); newData = baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { gzip.close(); baos.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("after:" + newData.length); return newData; }
From source file:com.android.email.core.internet.MimeUtility.java
/** * Reads the Part's body and returns a String based on any charset conversion that needed * to be done./*w ww . j a v a2 s . co m*/ * @param part * @return * @throws IOException */ public static String getTextFromPart(Part part) { try { if (part != null && part.getBody() != null) { InputStream in = part.getBody().getInputStream(); String mimeType = part.getMimeType(); if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*")) { /* * Now we read the part into a buffer for further processing. Because * the stream is now wrapped we'll remove any transfer encoding at this point. */ ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out); byte[] bytes = out.toByteArray(); in.close(); out.close(); String charset = getHeaderParameter(part.getContentType(), "charset"); /* * We've got a text part, so let's see if it needs to be processed further. */ if (charset != null) { /* * See if there is conversion from the MIME charset to the Java one. */ charset = CharsetUtil.toJavaCharset(charset); } if (charset != null) { /* * We've got a charset encoding, so decode using it. */ return new String(bytes, 0, bytes.length, charset); } else { /* * No encoding, so use us-ascii, which is the standard. */ return new String(bytes, 0, bytes.length, "ASCII"); } } } } catch (Exception e) { /* * If we are not able to process the body there's nothing we can do about it. Return * null and let the upper layers handle the missing content. */ Log.e(Email.LOG_TAG, "Unable to getTextFromPart", e); } return null; }
From source file:Main.java
public static byte[] decompress(byte[] bytes) throws IOException { GZIPInputStream gzip = null;/*from ww w. ja v a2 s .com*/ ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(bytes)); int len = 0; byte data[] = new byte[BUFFER_SIZE]; while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) { baos.write(data, 0, len); } gzip.close(); baos.flush(); return baos.toByteArray(); } finally { if (gzip != null) { gzip.close(); } if (baos != null) { baos.close(); } } }
From source file:org.kiji.rest.serializers.AvroToJsonStringSerializer.java
/** * Returns an encoded JSON string for the given Avro object. * * @param record is the record to encode * @return the JSON string representing this Avro object. * * @throws IOException if there is an error. *///from ww w .j a va2s . c o m public static String getJsonString(GenericContainer record) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), os); DatumWriter<GenericContainer> writer = new GenericDatumWriter<GenericContainer>(); if (record instanceof SpecificRecord) { writer = new SpecificDatumWriter<GenericContainer>(); } writer.setSchema(record.getSchema()); writer.write(record, encoder); encoder.flush(); String jsonString = new String(os.toByteArray(), Charset.forName("UTF-8")); os.close(); return jsonString; }
From source file:Main.java
/** * Converts a node to a string.//from w ww . j a v a 2s.c om * @param node The node to convert. * @return A string representation of the node. */ public static String getStringFromNode(Node node) { DOMSource domSource = new DOMSource(node); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, result); baos.flush(); return new String(baos.toByteArray()); } catch (Exception e) { throw new RuntimeException("Failed to stream node to string", e); } finally { try { baos.close(); } catch (Exception e) { } } }
From source file:org.LinuxdistroCommunity.android.client.NetworkUtilities.java
static String readStreamToString(InputStream stream, int block_size) throws IOException, UnsupportedEncodingException { InputStream in = stream;/*ww w .ja v a 2 s. c om*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] pBuffer = new byte[block_size]; try { for (;;) { int res = in.read(pBuffer); if (res == -1) { break; } if (res > 0) { if (out != null) { out.write(pBuffer, 0, res); } } } out.close(); in.close(); in = null; return out.toString(); } finally { if (in != null) { try { in.close(); } catch (Throwable t) { /* Ignore me */ } } if (out != null) { try { out.close(); } catch (Throwable t) { /* Ignore me */ } } } }