List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:foam.zizim.android.net.BoskoiHttpClient.java
public static byte[] fetchImage(String address) throws MalformedURLException, IOException { InputStream in = null;//from w w w.java2s . c o m OutputStream out = null; try { in = new BufferedInputStream(new URL(address).openStream(), IO_BUFFER_SIZE); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, 4 * 1024); copy(in, out); out.flush(); //need to close stream before return statement closeStream(in); closeStream(out); return dataStream.toByteArray(); } catch (IOException e) { //android.util.Log.e("IO", "Could not load buddy icon: " + this, e); } finally { closeStream(in); closeStream(out); } return null; /*final URL url = new URL(address); final Object content = url.getContent(); return content;*/ }
From source file:eu.scape_project.spacip.utils.IOUtils.java
/** * Reads a file and copies the content to an output stream * * @param file File to be read//from w w w . jav a 2 s.c om * @param output Output stream * @param bufferSize Buffer size * @return */ public static void copyFileToOutputStream(File file, OutputStream output, int bufferSize) { InputStream input = null; byte[] buf = new byte[bufferSize]; try { input = new BufferedInputStream(new FileInputStream(file)); int bytesRead = input.read(buf); while (bytesRead != -1) { output.write(buf, 0, bytesRead); bytesRead = input.read(buf); } output.flush(); } catch (IOException e) { logger.error("IOException occurred", e); } finally { try { if (input != null) { input.close(); output.close(); } } catch (Exception _) { } } }
From source file:com.hybris.mobile.data.WebServiceDataProvider.java
/** * Synchronous call for logging in/* w w w . j a va2 s. c om*/ * * @param httpBody * @return The data from the server as a string, in almost all cases JSON * @throws MalformedURLException * @throws IOException * @throws JSONException */ public static String getLoginResponse(Bundle httpBody) throws MalformedURLException, IOException { String response = ""; URL url = new URL(WebServiceAuthProvider.tokenURL()); trustAllHosts(); HttpsURLConnection connection = createSecureConnection(url); connection.setHostnameVerifier(DO_NOT_VERIFY); String authString = "mobile_android:secret"; String authValue = "Basic " + Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", authValue); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); OutputStream os = new BufferedOutputStream(connection.getOutputStream()); os.write(encodePostBody(httpBody).getBytes()); os.flush(); try { LoggingUtils.d(LOG_TAG, connection.toString()); response = readFromStream(connection.getInputStream()); } catch (MalformedURLException e) { LoggingUtils.e(LOG_TAG, "Error reading stream \"" + connection.getInputStream() + "\". " + e.getLocalizedMessage(), null); } catch (IOException e) { response = readFromStream(connection.getErrorStream()); } finally { connection.disconnect(); } return response; }
From source file:Main.java
/** * Save jpeg image with background color * @param strFileName Save file path/*from ww w .java 2 s.c o m*/ * @param bitmap Input bitmap * @param nQuality Jpeg quality for saving * @param nBackgroundColor background color * @return whether success or not */ public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor) { boolean bSuccess1 = false; boolean bSuccess2 = false; boolean bSuccess3; File saveFile = new File(strFileName); if (saveFile.exists()) { if (!saveFile.delete()) return false; } int nA = (nBackgroundColor >> 24) & 0xff; // If Background color alpha is 0, Background color substitutes as white if (nA == 0) nBackgroundColor = 0xFFFFFFFF; Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawColor(nBackgroundColor); canvas.drawBitmap(bitmap, rect, rect, new Paint()); // Quality limitation min/max if (nQuality < 10) nQuality = 10; else if (nQuality > 100) nQuality = 100; OutputStream out = null; try { bSuccess1 = saveFile.createNewFile(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { out = new FileOutputStream(saveFile); bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out); } catch (Exception e) { e.printStackTrace(); } try { if (out != null) { out.flush(); out.close(); bSuccess3 = true; } else bSuccess3 = false; } catch (IOException e) { e.printStackTrace(); bSuccess3 = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return (bSuccess1 && bSuccess2 && bSuccess3); }
From source file:Main.java
static public final String readPassword(OutputStream out, InputStream in) throws IOException { int rd = in.read(); if (rd == -1) return null; byte r = (byte) rd; int i = 0;/*from w w w .j a va 2 s. co m*/ int l = 50; byte[] buf = new byte[l]; while (r != '\n') { if (i >= l - 1) { l += 50; byte[] old = buf; buf = new byte[l]; System.arraycopy(old, 0, buf, 0, old.length); } if (r != '\r') { buf[i++] = r; out.write('\b'); out.write('*'); out.flush(); } rd = in.read(); if (rd == -1) break; r = (byte) rd; } return new String(buf, 0, i); }
From source file:Main.java
/** * Save jpeg image with background color * @param strFileName Save file path/*from www .jav a 2s . c o m*/ * @param bitmap Input bitmap * @param nQuality Jpeg quality for saving * @param nBackgroundColor background color * @return whether success or not */ public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor) { boolean bSuccess1 = false; boolean bSuccess2 = false; boolean bSuccess3; File saveFile = new File(strFileName); if (saveFile.exists()) { if (!saveFile.delete()) return false; } int nA = (nBackgroundColor >> 24) & 0xff; // If Background color alpha is 0, Background color substitutes as white if (nA == 0) nBackgroundColor = 0xFFFFFFFF; Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawColor(nBackgroundColor); canvas.drawBitmap(newBitmap, rect, rect, new Paint()); canvas.drawBitmap(bitmap, rect, rect, new Paint()); // Quality limitation min/max if (nQuality < 10) nQuality = 10; else if (nQuality > 100) nQuality = 100; OutputStream out = null; try { bSuccess1 = saveFile.createNewFile(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { out = new FileOutputStream(saveFile); bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out); } catch (Exception e) { e.printStackTrace(); } try { if (out != null) { out.flush(); out.close(); bSuccess3 = true; } else bSuccess3 = false; } catch (IOException e) { e.printStackTrace(); bSuccess3 = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return (bSuccess1 && bSuccess2 && bSuccess3); }
From source file:org.dkpro.lab.Util.java
/** * Shove data from an {@link InputStream} into an {@link OutputStream}. * Neither of the streams are closed after the operation. * * @param is the source./* w w w . ja v a2 s.c om*/ * @param os the target. * @throws IOException in case of read or write problems. */ public static void shove(final InputStream is, final OutputStream os) throws IOException { byte[] buffer = new byte[65536]; int read; while (true) { read = is.read(buffer); if (read == -1) { break; } os.write(buffer, 0, read); } os.flush(); }
From source file:com.omertron.fanarttvapi.TestLogger.java
/** * Save properties to a file/* ww w . ja v a2s . c o m*/ * * @param props * @param propertyFile * @param headerText */ public static void saveProperties(Properties props, File propertyFile, String headerText) { OutputStream out = null; try { out = new FileOutputStream(propertyFile); if (StringUtils.isNotBlank(headerText)) { props.store(out, headerText); } } catch (FileNotFoundException ex) { LOG.warn("Failed to find properties file", ex); } catch (IOException ex) { LOG.warn("Failed to read properties file", ex); } finally { if (out != null) { try { out.flush(); out.close(); } catch (IOException ex) { LOG.warn("Failed to close properties file", ex); } } } }
From source file:ca.simplegames.micro.utils.IO.java
/** * Copy the contents of the given InputStream to the given OutputStream. * Closes both streams when done.//ww w. ja v a 2s . c om * * @param in the stream to copy from * @param out the stream to copy to * @return the number of bytes copied * @throws IOException in case of I/O errors */ public static long copy(InputStream in, OutputStream out) throws IOException { try { int byteCount = 0; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); return byteCount; } finally { IO.close(in); IO.close(out); } }
From source file:most.voip.api.Utils.java
static void copyAssets(Context ctx) { AssetManager assetManager = ctx.getAssets(); String[] files = null;/*w ww . j a v a 2 s . com*/ try { files = assetManager.list(""); } catch (IOException e) { Log.e(TAG, "Failed to get asset file list.", e); } for (String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); File outFile = new File(ctx.getExternalFilesDir(null), filename); Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath()); out = new FileOutputStream(outFile); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (IOException e) { Log.e("tag", "Failed to copy asset file: " + filename, e); } } }