List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:edu.cwru.jpdg.Dotty.java
public static void graphviz(String name, String graph) { byte[] bytes = graph.getBytes(); try {//w w w . ja v a 2s .c om Path p = Paths.get("./reports/" + name + ".dot"); Files.write(p, bytes); } catch (IOException e) { System.out.println(graph); } try { ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "-o", "reports/" + name + ".png"); pb.redirectError(ProcessBuilder.Redirect.INHERIT); Process p = pb.start(); OutputStream stdin = p.getOutputStream(); stdin.write(bytes); stdin.close(); String line; BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); List<String> stdout_lines = new ArrayList<String>(); line = stdout.readLine(); while (line != null) { stdout_lines.add(line); line = stdout.readLine(); } if (p.waitFor() != 0) { throw new RuntimeException("javac failed"); } } catch (InterruptedException e) { throw new RuntimeException(e.getMessage()); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } }
From source file:Main.java
/** * Do an HTTP POST and return the data as a byte array. *//*from w w w . j a va 2 s . c o m*/ public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties) throws MalformedURLException, IOException { HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(data != null); urlConnection.setDoInput(true); if (requestProperties != null) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue()); } } urlConnection.connect(); if (data != null) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(data); out.close(); } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); return convertInputStreamToByteArray(in); } catch (IOException e) { String details; if (urlConnection != null) { details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage() + ")"; } else { details = ""; } Log.e("ExoplayerUtil", "executePost: Request failed" + details, e); throw e; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:Main.java
static void copyData(InputStream from, OutputStream to) throws IOException { byte[] buffer = new byte[1024]; int length;/*from ww w .j av a2s .c om*/ while ((length = from.read(buffer)) > 0) { to.write(buffer, 0, length); } to.flush(); to.close(); from.close(); }
From source file:io.helixservice.feature.configuration.cloudconfig.CloudConfigDecrypt.java
/** * Decrypt an encrypted string//w w w . j a v a 2 s. c o m * <p> * This method blocks on a HTTP request. * * @param name property or filename for reference/logging * @param encryptedValue Encrypted string * @param cloudConfigUri URI of the Cloud Config server * @param httpBasicHeader HTTP Basic header containing username and password for Cloud Config server * @return */ public static String decrypt(String name, String encryptedValue, String cloudConfigUri, String httpBasicHeader) { String result = encryptedValue; // Remove prefix if needed if (encryptedValue.startsWith(CIPHER_PREFIX)) { encryptedValue = encryptedValue.substring(CIPHER_PREFIX.length()); } String decryptUrl = cloudConfigUri + "/decrypt"; try { HttpURLConnection connection = (HttpURLConnection) new URL(decryptUrl).openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(AUTHORIZATION_HEADER, httpBasicHeader); connection.setRequestProperty("Content-Type", "text/plain"); connection.setRequestProperty("Content-Length", Integer.toString(encryptedValue.getBytes().length)); connection.setRequestProperty("Accept", "*/*"); // Write body OutputStream outputStream = connection.getOutputStream(); outputStream.write(encryptedValue.getBytes()); outputStream.close(); if (connection.getResponseCode() == 200) { InputStream inputStream = connection.getInputStream(); result = IOUtils.toString(inputStream); inputStream.close(); } else { LOG.error("Unable to Decrypt name=" + name + " due to httpStatusCode=" + connection.getResponseCode() + " for decryptUrl=" + decryptUrl); } } catch (IOException e) { LOG.error("Unable to connect to Cloud Config server at decryptUrl=" + decryptUrl, e); } return result; }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); byte[] buf = new byte[1024]; int len;// w ww. j a va 2 s. c om while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
From source file:Main.java
public static void copyInputStreamToFile(InputStream in, File file) { try {//from w w w . ja v a 2 s .co m OutputStream out = new FileOutputStream(file); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void pushFileFromRAW(Context mContext, File outputFile, int RAW, boolean Override) throws IOException { if (!outputFile.exists() || Override) { if (Override && outputFile.exists()) if (!outputFile.delete()) { throw new IOException(outputFile.getName() + " can't be deleted!"); }/* w w w . j ava 2s. c om*/ InputStream is = mContext.getResources().openRawResource(RAW); OutputStream os = new FileOutputStream(outputFile); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); } }
From source file:Main.java
public static void savePicture(byte[] picture) throws IOException { OutputStream out = null; try {//from w ww .j a v a 2s . c o m out = new FileOutputStream(PICTURE_TEMP_PATH); out.write(picture); out.flush(); } finally { if (out != null) { out.close(); } } }
From source file:Main.java
public static void DocumentToStream(final Document doc, OutputStream stream) { Result l_s = new StreamResult(stream); doc.normalize();/*from www. j av a2 s .c o m*/ try { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s); stream.close(); } catch (Exception e) { System.err.println(e); return; } }
From source file:filtros.histograma.Histograma.java
public static void Apply(BufferedImage image, String savePath) { //Chamada do metodo que gera os dados do histograma GetData(image);/*from w w w .j a v a 2 s . com*/ //Dataset que gera o grafico DefaultCategoryDataset ds = new DefaultCategoryDataset(); //Loop que percorre o array de dados do histograma for (int k = 0; k < data.length; k++) { //Adiciona o valor ao dataset ds.setValue(data[k], "Imagem", Integer.toString(data[k])); } //Gera o grafico JFreeChart grafico = ChartFactory.createLineChart("Histograma", "Tons de cinza", "Valor", ds, PlotOrientation.VERTICAL, true, true, false); //Salva o grafico em um arquivo de png try { OutputStream arquivo = new FileOutputStream(savePath); ChartUtilities.writeChartAsPNG(arquivo, grafico, 550, 400); arquivo.close(); } catch (IOException e) { e.printStackTrace(); } }