List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public static byte[] decodeB(byte[] bytes) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int i = 0;/* ww w .j av a2 s . c o m*/ int len = bytes.length; while (i < len) { char[] four = new char[4]; int j = 0; while (j < 4) { byte b = bytes[i++]; if (b != '\r' || b != '\n') four[j++] = (char) b; } int k; if (four[3] == '=') { if (four[2] == '=') { k = 1; } else { k = 2; } } else { k = 3; } int aux = 0; for (j = 0; j < 4; j++) { if (four[j] != '=') { aux = aux | (chars.indexOf(four[j]) << (6 * (3 - j))); } } for (j = 0; j < k; j++) { out.write((aux >>> (8 * (2 - j))) & 0xFF); } } out.close(); return out.toByteArray(); }
From source file:com.beetle.framework.util.ObjectUtil.java
/** * ??// w w w. j av a2s . c om * * @param obj * @return */ public final static byte[] objToBytes(Object obj) { ByteArrayOutputStream bao = null; ObjectOutputStream oos; try { bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(obj); oos.flush(); oos.close(); return bao.toByteArray(); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (bao != null) { bao.close(); bao = null; } } catch (IOException e) { } } }
From source file:com.urs.triptracks.TripUploader.java
public static byte[] compress(String string) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes());// ww w .jav a2 s . c om gos.close(); byte[] compressed = os.toByteArray(); os.close(); return compressed; }
From source file:com.ery.ertc.estorm.util.GZIPUtils.java
/** * Returns an gunzipped copy of the input array, truncated to <code>sizeLimit</code> bytes, if necessary. If the gzipped input has been * truncated or corrupted, a best-effort attempt is made to unzip as much as possible. If no data can be extracted <code>null</code> is * returned./* w w w .j a v a 2 s.c om*/ */ public static final byte[] unzipBestEffort(byte[] in, int sizeLimit) { try { // 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]; int written = 0; while (true) { try { int size = inStream.read(buf); if (size <= 0) break; if ((written + size) > sizeLimit) { outStream.write(buf, 0, sizeLimit - written); break; } outStream.write(buf, 0, size); written += size; } catch (Exception e) { break; } } try { outStream.close(); } catch (IOException e) { } return outStream.toByteArray(); } catch (IOException e) { return null; } }
From source file:Main.java
public static String uploadFile(String path) { String sourceFileUri = path;//from w w w .j a v a 2 s . com File file = new File(sourceFileUri); ByteArrayOutputStream objByteArrayOS = null; FileInputStream objFileIS = null; boolean isSuccess = false; String strAttachmentCoded = null; if (!file.isFile()) { Log.w(TAG, "Source File not exist :" + sourceFileUri); } else { try { objFileIS = new FileInputStream(file); objByteArrayOS = new ByteArrayOutputStream(); byte[] byteBufferString = new byte[(int) file.length()]; objFileIS.read(byteBufferString); byte[] byteBinaryData = Base64.encode(byteBufferString, Base64.DEFAULT); strAttachmentCoded = new String(byteBinaryData); isSuccess = true; } catch (Exception e) { e.printStackTrace(); Log.e("Upload file to server", "error: " + e.getMessage(), e); } finally { try { objByteArrayOS.close(); objFileIS.close(); } catch (IOException e) { Log.e(TAG, "Error : " + e.getMessage()); } } } if (isSuccess) { return strAttachmentCoded; } else { return "No Picture"; } }
From source file:com.ery.ertc.estorm.util.ToolUtil.java
public static String serialObject(Object obj, boolean isGzip, boolean urlEnCode) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); String serStr = null;/*from w ww .j a v a2 s . co m*/ byte[] bts = null; if (isGzip) { bts = GZIPUtils.zip(byteArrayOutputStream.toByteArray()); } else { bts = byteArrayOutputStream.toByteArray(); } if (urlEnCode) { serStr = new String(org.apache.commons.codec.binary.Base64.encodeBase64(bts), "ISO-8859-1"); } else { serStr = new String(bts, "ISO-8859-1"); } objectOutputStream.close(); byteArrayOutputStream.close(); return serStr; }
From source file:io.bitsquare.common.util.Utilities.java
public static byte[] serialize(Serializable object) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null;/*from w w w. j a va2 s . com*/ byte[] result = null; try { out = new ObjectOutputStream(bos); out.writeObject(object); out.flush(); result = bos.toByteArray().clone(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException ignore) { } try { bos.close(); } catch (IOException ignore) { } } return result; }
From source file:gov.nih.nci.caarray.util.CaArrayUtils.java
/** * returns a byte array representing the input byte array, gzipped. Use this method with caution - for large arrays, * you should use streams instead./* w w w . ja va 2s . c om*/ * * @param input the content to compress * @return the gzipped input * @throws IOException if there is a problem zipping the content */ public static byte[] gzip(byte[] input) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteStreams.write(input, new OutputSupplier<OutputStream>() { @Override public OutputStream getOutput() throws IOException { return new GZIPOutputStream(baos); } }); baos.close(); return baos.toByteArray(); }
From source file:de.laures.cewolf.util.Renderer.java
/** * Renders a chart/* w w w . ja v a2s. c om*/ * @param cd the chart image to be rendered * @return the rendered image * @throws CewolfException */ private static RenderedImage renderChart(ChartImage cd, Object chart) throws CewolfException { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); final String mimeType = cd.getMimeType(); if (MIME_PNG.equals(mimeType)) { handlePNG(baos, (JFreeChart) chart, cd.getWidth(), cd.getHeight(), info); } else if (MIME_JPEG.equals(mimeType)) { handleJPEG(baos, (JFreeChart) chart, cd.getWidth(), cd.getHeight(), info); } else if (MIME_SVG.equals(mimeType)) { handleSVG(baos, (JFreeChart) chart, cd.getWidth(), cd.getHeight()); } else { throw new RenderingException("Mime type " + mimeType + " is unsupported."); } baos.close(); return new RenderedImage(baos.toByteArray(), mimeType, info); } catch (IOException ioe) { log.error(ioe); throw new ChartRenderingException(ioe.getMessage(), ioe); } }
From source file:com.zrlh.llkc.funciton.Http_Utility.java
/** * Implement a weibo http request and return results . * //w w w . ja va 2 s . c o m * @param context * context of activity * @param url * @param method * (GET|POST) * @param bm * @return * @throws Exception */ public static String openUrl(Context context, String url, String method, Bitmap bm) throws Exception { String result = ""; try { HttpClient client = getNewHttpClient(context); HttpUriRequest request = null; ByteArrayOutputStream bos = null; if (method.equals("GET")) { } else if (method.equals("POST")) { HttpPost post = new HttpPost(url); byte[] data = null; bos = new ByteArrayOutputStream(1024 * 50); post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); Http_Utility.imageContentToUpload(bos, bm); data = bos.toByteArray(); bos.close(); ByteArrayEntity formEntity = new ByteArrayEntity(data); post.setEntity(formEntity); request = post; } else if (method.equals("DELETE")) { request = new HttpDelete(url); } HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode != 200) { result = read(response); throw new Exception(); } // parse content stream from response result = read(response); return result; } catch (IOException e) { } return ""; }