List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:com.arellomobile.android.push.utils.NetworkUtils.java
public static NetworkResult makeRequest(Map<String, Object> data, String methodName) throws Exception { NetworkResult result = new NetworkResult(500, null); OutputStream connectionOutput = null; InputStream inputStream = null; try {/* ww w.j a v a 2s.c om*/ String urlString = NetworkUtils.BASE_URL + methodName; if (useSSL) urlString = NetworkUtils.BASE_URL_SECURE + methodName; URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.setDoOutput(true); JSONObject innerRequestJson = new JSONObject(); for (String key : data.keySet()) { innerRequestJson.put(key, data.get(key)); } JSONObject requestJson = new JSONObject(); requestJson.put("request", innerRequestJson); connection.setRequestProperty("Content-Length", String.valueOf(requestJson.toString().getBytes().length)); connectionOutput = connection.getOutputStream(); connectionOutput.write(requestJson.toString().getBytes()); connectionOutput.flush(); connectionOutput.close(); inputStream = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream dataCache = new ByteArrayOutputStream(); // Fully read data byte[] buff = new byte[1024]; int len; while ((len = inputStream.read(buff)) >= 0) { dataCache.write(buff, 0, len); } // Close streams dataCache.close(); String jsonString = new String(dataCache.toByteArray()).trim(); Log.w(TAG, "PushWooshResult: " + jsonString); JSONObject resultJSON = new JSONObject(jsonString); result.setData(resultJSON); result.setCode(resultJSON.getInt("status_code")); } finally { if (null != inputStream) { inputStream.close(); } if (null != connectionOutput) { connectionOutput.close(); } } return result; }
From source file:Main.java
public static String getExceptionCauseString(final Throwable ex) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PrintStream ps = new PrintStream(bos); try {/*from w w w . j av a 2 s. c o m*/ // print directly Throwable t = ex; while (t.getCause() != null) { t = t.getCause(); } t.printStackTrace(ps); return toVisualString(bos.toString()); } finally { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle) { byte[] result = null; ByteArrayOutputStream output = null; try {//from w w w . j a v a 2 s .c om output = new ByteArrayOutputStream(); bitmap.compress(mCompressFormat, 100, output); result = output.toByteArray(); if (needRecycle) { bitmap.recycle(); } } catch (Exception e) { e.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } } return result; }
From source file:Main.java
public static byte[] decompress(byte[] data, int off, int len) { byte[] output = null; Inflater decompresser = new Inflater(); decompresser.reset();/*from w w w . j a va 2 s. c om*/ decompresser.setInput(data, off, len); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length); try { byte[] result = new byte[1024]; while (!decompresser.finished()) { int i = decompresser.inflate(result); out.write(result, 0, i); } output = out.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } finally { try { out.close(); } catch (Exception e) { } decompresser.end(); } return output; }
From source file:com.siemens.sw360.exporter.CSVExport.java
@NotNull private static ByteArrayOutputStream getCSVOutputStream(Iterable<String> csvHeaderIterable, Iterable<Iterable<String>> inputIterable) throws IOException { final ByteArrayOutputStream outB = new ByteArrayOutputStream(); try (Writer out = new BufferedWriter(new OutputStreamWriter(outB));) { CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat); csvPrinter.printRecord(csvHeaderIterable); csvPrinter.printRecords(inputIterable); csvPrinter.flush();/* w w w. j a v a 2s . c om*/ csvPrinter.close(); } catch (Exception e) { outB.close(); throw e; } return outB; }
From source file:de.kapsi.net.daap.DaapHeaderConstructor.java
/** * Converts statusLine and headers to an byte-Array *//*w w w . j a v a 2s . c om*/ private static byte[] toByteArray(String statusLine, Header[] headers) throws UnsupportedEncodingException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(statusLine.getBytes(ISO_8859_1)); out.write(CRLF); for (int i = 0; i < headers.length; i++) { out.write(headers[i].toExternalForm().getBytes(ISO_8859_1)); } out.write(CRLF); out.close(); return out.toByteArray(); }
From source file:Main.java
public static byte[] toByteArray(Serializable serializable) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null;// ww w. j a v a2 s. co m try { out = new ObjectOutputStream(bos); out.writeObject(serializable); byte[] bytes = bos.toByteArray(); return bytes; } catch (IOException e) { throw new IllegalArgumentException(e); } finally { try { out.close(); bos.close(); } catch (IOException e) { //can be ignored; } } }
From source file:Main.java
/** * Compress the byte array passed// ww w . j ava 2 s .c o m * <p> * @param input byte array * @param bufferLength buffer length * @return compressed byte array * @throws IOException thrown if we can't close the output stream */ public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException { // Compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[bufferLength]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ ) compressor.end(); bos.close(); // Get the compressed data return bos.toByteArray(); }
From source file:outfox.ynote.open.client.YNoteHttpUtils.java
/** * Get the response content as a string. * * @param response/* w w w. j a v a 2s . co m*/ * @return * @throws IOException */ public static String getResponseContent(InputStream response) throws IOException { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = response.read(buffer))) { bytes.write(buffer, 0, n); } bytes.close(); return new String(bytes.toByteArray(), YNoteConstants.ENCODING); } finally { // release the http response response.close(); } }
From source file:io.confluent.kafkarest.converters.AvroConverter.java
public static Object toAvro(JsonNode value, Schema schema) { try {// w ww . ja va2 s .c om ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonMapper.writeValue(out, value); DatumReader<Object> reader = new GenericDatumReader<Object>(schema); Object object = reader.read(null, decoderFactory.jsonDecoder(schema, new ByteArrayInputStream(out.toByteArray()))); out.close(); return object; } catch (IOException | RuntimeException e) { // These can be generated by Avro's JSON decoder, the input/output stream operations, and the // Jackson ObjectMapper.writeValue call. throw new ConversionException("Failed to convert JSON to Avro: " + e.getMessage()); } }