List of usage examples for java.io ByteArrayOutputStream flush
public void flush() throws IOException
From source file:Main.java
public static String objectSerializableToString(Serializable object) { ByteArrayOutputStream baos = null; ObjectOutputStream os = null; try {//from ww w .j av a 2 s . c o m if (object != null) { baos = new ByteArrayOutputStream(); os = new ObjectOutputStream(baos); os.writeObject(object); os.flush(); baos.flush(); return baos.toString("UTF-8"); } } catch (Throwable e) { } finally { try { if (os != null) { os.close(); } } catch (Throwable e2) { } try { if (baos != null) { baos.close(); } } catch (Throwable e2) { } } return null; }
From source file:Main.java
public static Bitmap loadBitmap(String url) { Log.d(TAG, "Start Load Url : " + url); //Bitmap bitmap = null; InputStream in = null;//w w w. jav a 2 s. c om BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream()); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; while ((nRead = in.read(data, 0, data.length)) != -1) { dataStream.write(data, 0, nRead); } dataStream.flush(); final byte[] newData = dataStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options); } catch (IOException e) { Log.e("My fault", "Could not load Bitmap from: " + url); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmapResult; }
From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java
/** * Determines if an InputStream is compressed. The java.util.zip GZip * implementation does not expose the GZip header so it is difficult to determine * if a string is compressed.//from w w w . j ava2 s .c om * * @param inputStream an array of bytes * @return true if the stream is compressed or false otherwise * @throws java.io.IOException if the byte array couldn't be read */ public static boolean isCompressed(InputStream inputStream) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[2]; int nRead = inputStream.read(data, 0, 2); buffer.write(data, 0, nRead); buffer.flush(); return isCompressed(buffer.toByteArray()); }
From source file:com.ikon.util.Serializer.java
/** * @param obj/*from w w w. ja va2 s . c o m*/ */ public static byte[] write(Object obj) throws IOException { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.flush(); baos.flush(); return baos.toByteArray(); } finally { IOUtils.closeQuietly(oos); IOUtils.closeQuietly(baos); } }
From source file:org.apache.htrace.viewer.HBaseSpanViewer.java
public static String toJsonString(final Message message) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out, Charset.defaultCharset()); appendJsonString(message, writer);//from w w w. j av a2 s . c o m writer.flush(); out.flush(); return out.toString(); }
From source file:com.syncthemall.enml4j.util.Utils.java
/** * Encode an {@code InputStream} representing a file in base64. * //w ww . ja v a 2 s.c o m * @param is the source to encode * @return a {@code String} representation of the {@code InputStream} in parameter, encoded in base64 * @throws IOException if an I/O error occurs reading the {@code InputStream} in parameter */ public static String encodeToBase64Binary(final InputStream is) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[BUFFER_SIZE]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return Base64.encodeBase64String(buffer.toByteArray()); }
From source file:com.vaadin.testbench.screenshot.ImageUtil.java
/** * Encodes target image to a Base64 string * /* ww w . j ava 2s . c o m*/ * @param image * BufferedImage to encode to String * @return Base64 encoded String of image */ public static String encodeImageToBase64(BufferedImage image) { String encodedImage = ""; Base64 encoder = new Base64(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); baos.flush(); byte[] encodedBytes = encoder.encode(baos.toByteArray()); encodedImage = new String(encodedBytes); baos.close(); } catch (IOException e) { e.printStackTrace(); } return encodedImage; }
From source file:com.lingeringsocket.mobflare.RpcCoordinator.java
private static String readInputStream(InputStream is) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead;// w w w . ja va 2 s . c o m byte[] data = new byte[16384]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); is.close(); return new String(buffer.toByteArray()); }
From source file:Main.java
private static byte[] getBytesFromDriveVideoUri(Context context, Uri uri) { InputStream inputStream = null; try {// w w w . j ava 2s. co m inputStream = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException e) { e.printStackTrace(); } if (inputStream == null) { return null; } int maxBufferSize = 1024 * 1024; int available = 0; try { available = inputStream.available(); } catch (IOException e) { e.printStackTrace(); } if (available == 0) { available = maxBufferSize; } int bufferSize = Math.min(available, maxBufferSize); byte[] data = new byte[bufferSize]; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; try { while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } } catch (IOException e) { e.printStackTrace(); } try { buffer.flush(); } catch (IOException e) { e.printStackTrace(); } return buffer.toByteArray(); }
From source file:com.criticalsoftware.mobics.presentation.util.GeolocationUtil.java
private static byte[] readResponse(HttpMethod method) throws IOException { InputStream is = method.getResponseBodyAsStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead;/*from w w w . j av a 2 s . c o m*/ byte[] data = new byte[16384]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); byte[] responseBody = buffer.toByteArray(); return responseBody; }