List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:Main.java
public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException { if (bitmap == null || fileName == null || context == null) return;//from w w w . j a va2s . c om FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream); byte[] bytes = stream.toByteArray(); fos.write(bytes); fos.close(); }
From source file:Main.java
public static void copyFile(File fromFile, File toFile, Boolean rewrite) { if (!fromFile.exists()) { return;//from w ww . jav a 2 s.co m } if (!fromFile.isFile()) { return; } if (!fromFile.canRead()) { return; } if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } if (toFile.exists() && rewrite) { toFile.delete(); } try { java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile); java.io.FileOutputStream 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(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void writeStringToFile(String string, File file) { FileOutputStream fos = null; try {/*from ww w .j av a 2s . c o m*/ fos = new FileOutputStream(file); fos.write(string.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void write(InputStream in, File file) { if (file.exists()) { file.delete();// w w w .j a va 2 s.c o m } try { file.createNewFile(); FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[1024]; while (in.read(buffer) > -1) { out.write(buffer); } out.flush(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.hazelcast.test.starter.HazelcastVersionLocator.java
private static File downloadFile(String url, File targetDirectory, String filename) { CloseableHttpClient client = HttpClients.createDefault(); File targetFile = new File(targetDirectory, filename); if (targetFile.isFile() && targetFile.exists()) { return targetFile; }// w ww . j a v a2 s . c o m HttpGet request = new HttpGet(url); try { CloseableHttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() != SC_OK) { throw new GuardianException("Cannot download file from " + url + ", http response code: " + response.getStatusLine().getStatusCode()); } HttpEntity entity = response.getEntity(); FileOutputStream fos = new FileOutputStream(targetFile); entity.writeTo(fos); fos.close(); targetFile.deleteOnExit(); return targetFile; } catch (IOException e) { throw rethrowGuardianException(e); } finally { try { client.close(); } catch (IOException e) { // ignore } } }
From source file:in.cs654.chariot.ashva.AshvaProcessor.java
/** * This function takes in request, executes the request and returns the response. * The request is written into /tmp/<request_id>.req file. While running the docker container, /tmp dir is mounted * to /tmp of the container. This enables ease of data exchange. TODO encryption * Docker runs the request and puts the result into /tmp/<request_id>.res. * If the request specifies a timeout, it is picked up, else default timeout is set for the process to run * @param request containing function_name and other required information * @return response of the request/*from w w w . j av a 2 s . c o m*/ */ private static BasicResponse handleTaskProcessingRequest(BasicRequest request) { final String requestID = request.getRequestId(); try { final byte[] serializedBytes = AvroUtils.requestToBytes(request); final FileOutputStream fos = new FileOutputStream("/tmp/" + requestID + ".req"); fos.write(serializedBytes); fos.close(); String timeoutString = request.getExtraData().get("chariot_timeout"); Long timeout; if (timeoutString != null) { timeout = Long.parseLong(timeoutString); } else { timeout = PROCESS_DEFAULT_TIMEOUT; } final String dockerImage = Mongo.getDockerImage(request.getDeviceId()); final String cmd = "docker run -v /tmp:/tmp " + dockerImage + " /bin/chariot " + requestID; final Process process = Runtime.getRuntime().exec(cmd); TimeoutProcess timeoutProcess = new TimeoutProcess(process); timeoutProcess.start(); try { timeoutProcess.join(timeout); // milliseconds if (timeoutProcess.exit != null) { File file = new File("/tmp/" + requestID + ".res"); byte[] bytes = FileUtils.readFileToByteArray(file); LOGGER.info("Response generated for request " + requestID); return AvroUtils.bytesToResponse(bytes); } else { LOGGER.severe("Timeout in generating response for request " + requestID); return ResponseFactory.getTimeoutErrorResponse(request); } } catch (InterruptedException ignore) { timeoutProcess.interrupt(); Thread.currentThread().interrupt(); LOGGER.severe("Error in generating response for request " + requestID); return ResponseFactory.getErrorResponse(request); } finally { process.destroy(); } } catch (IOException ignore) { LOGGER.severe("Error in generating response for request: " + requestID); return ResponseFactory.getErrorResponse(request); } }
From source file:Main.java
public static String saveToInternalStorage(Bitmap bitmap, String title, Context context) throws IOException { ContextWrapper cw = new ContextWrapper(context); File directory = cw.getDir("bitmaps", Context.MODE_PRIVATE); File image = new File(directory, title); FileOutputStream fileOutputStream = null; try {/* www.j a v a2 s .c om*/ fileOutputStream = new FileOutputStream(image); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); } catch (IOException e) { e.printStackTrace(); } finally { fileOutputStream.close(); } return directory.getAbsolutePath(); }
From source file:IORoutines.java
public static void save(File file, byte[] content) throws IOException { FileOutputStream out = new FileOutputStream(file); try {//from ww w .j a v a2s. co m out.write(content); } finally { out.close(); } }
From source file:Main.java
public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) { File fromFile = new File(fromFilePath); File toFile = new File(toFilePath); if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return;//from w w w. j a v a 2 s.c o m } if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } if (toFile.exists() && rewrite) { toFile.delete(); } try { FileInputStream fosfrom = new FileInputStream(fromFile); FileOutputStream 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(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
private static void streamToFile(InputStream is, File targetFile) throws IOException { FileOutputStream fos = new FileOutputStream(targetFile); byte[] bytes = new byte[1024]; while (true) { int n = is.read(bytes); if (n < 0) { break; }/*w w w . j av a 2 s.c om*/ fos.write(bytes, 0, n); } is.close(); fos.close(); }