List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:com.chnoumis.commons.zip.utils.ZipUtils.java
public static List<ZipInfo> unZiptoZipInfo(InputStream is) throws ArchiveException, IOException { List<ZipInfo> results = new ArrayList<ZipInfo>(); ArchiveInputStream in = null;//from w ww . j av a 2 s . c om try { in = new ArchiveStreamFactory().createArchiveInputStream("zip", is); ZipArchiveEntry entry = null; while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) { ZipInfo zipInfo = new ZipInfo(); OutputStream o = new ByteArrayOutputStream(); try { IOUtils.copy(in, o); } finally { o.close(); } zipInfo.setFileName(entry.getName()); zipInfo.setFileContent(o.toString().getBytes()); results.add(zipInfo); } } finally { if (in != null) { in.close(); } } is.close(); return results; }
From source file:com.ibm.jaggr.core.util.CopyUtil.java
public static int copy(InputStream in, OutputStream out) throws IOException { try {//from w w w .j a v a 2 s . c o m return IOUtils.copy(in, out); } finally { try { out.close(); } catch (Exception ignore) { } ; try { in.close(); } catch (Exception ignore) { } ; } }
From source file:flow.visibility.tapping.OpenDaylightHelper.java
/** The function for inserting the flow */ public static boolean installFlow(JSONObject postData, String user, String password, String baseURL) { StringBuffer result = new StringBuffer(); /** Check the connection to ODP REST API page */ try {//ww w . j av a 2 s . c o m if (!baseURL.contains("http")) { baseURL = "http://" + baseURL; } baseURL = baseURL + "/controller/nb/v2/flowprogrammer/default/node/OF/" + postData.getJSONObject("node").get("id") + "/staticFlow/" + postData.get("name"); /** Create URL = base URL + container */ URL url = new URL(baseURL); /** Create authentication string and encode it to Base64*/ String authStr = user + ":" + password; String encodedAuthStr = Base64.encodeBase64String(authStr.getBytes()); /** Create Http connection */ HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /** Set connection properties */ connection.setRequestMethod("PUT"); connection.setRequestProperty("Authorization", "Basic " + encodedAuthStr); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); /** Set JSON Post Data */ OutputStream os = connection.getOutputStream(); os.write(postData.toString().getBytes()); os.close(); /** Get the response from connection's inputStream */ InputStream content = (InputStream) connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line = ""; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } /** checking the result of REST API connection */ if ("success".equalsIgnoreCase(result.toString())) { return true; } else { return false; } }
From source file:Main.java
public static int CopyFile(String fromFile, String toFile) { try {//from w w w .j a v a2 s .c o m InputStream fosfrom = new FileInputStream(fromFile); OutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); return 0; } catch (Exception ex) { return -1; } }
From source file:Main.java
public static void copyfile(File source, File dest) { try {//from www .j a v a 2 s.c om InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(dest); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException e) { Log.e(TAG, e.getLocalizedMessage()); } catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); } }
From source file:Main.java
/** * The following method creates a File from a given Bitmap. * //from ww w. j a v a 2s . c o m * @param context * - The application context. * @param bitmap * - The bitmap to be converted into a File. * @return */ public static File getFilefromBitmap(Context context, Bitmap bitmap) { File file = null; try { String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; file = new File(path, "1" + ".jpg"); fOut = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); } catch (Exception e) { e.printStackTrace(); } return file; }
From source file:StreamsUtils.java
public static void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException { int readedBytes; byte[] buf = new byte[1024]; while ((readedBytes = stream.read(buf)) > 0) { out.write(buf, 0, readedBytes);//from ww w . j av a 2s .c o m } stream.close(); out.close(); }
From source file:com.chnoumis.commons.zip.utils.ZipUtils.java
public static void unZip(InputStream is) throws ArchiveException, IOException { ArchiveInputStream in = null;//from w w w .j a v a 2s . co m try { in = new ArchiveStreamFactory().createArchiveInputStream("zip", is); ZipArchiveEntry entry = null; while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) { OutputStream o = new FileOutputStream(entry.getName()); try { IOUtils.copy(in, o); } finally { o.close(); } } } finally { if (in != null) { in.close(); } } is.close(); }
From source file:Main.java
public static void fileCopy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len;/* www . ja v a 2s. c o m*/ while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
From source file:Main.java
public static String extractFromAssets(Context ctx, String file, String destinationDirectory) throws IOException, FileNotFoundException { final int BUFFER = 2048; BufferedOutputStream dest = null; AssetManager assetManager = ctx.getAssets(); InputStream in = assetManager.open(file); String destinationFilename = destinationDirectory + File.separator + file; OutputStream out = new FileOutputStream(destinationFilename); byte[] buffer = new byte[1024]; int read;/*from w w w . j a v a2 s. c o m*/ while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); return destinationFilename; }