List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:com.ettrema.httpclient.Utils.java
/** * Wraps the outputstream in a bufferedoutputstream and writes to it * * the outputstream is closed and flushed before returning * * @param in/*from w ww .java 2 s . c o m*/ * @param out * @param listener * @throws IOException */ public static long writeBuffered(InputStream in, OutputStream out, final ProgressListener listener) throws IOException { BufferedOutputStream bout = null; try { bout = new BufferedOutputStream(out); long bytes = Utils.write(in, out, listener); bout.flush(); out.flush(); return bytes; } finally { Utils.close(bout); Utils.close(out); } }
From source file:Main.java
private static void writeStreamToFile(InputStream stream, File file) { try {//from w w w . j av a 2 s . co m OutputStream output = null; try { output = new FileOutputStream(file); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { try { final byte[] buffer = new byte[1024]; int read; while ((read = stream.read(buffer)) != -1) output.write(buffer, 0, read); output.flush(); } finally { output.close(); } } catch (Exception e) { e.printStackTrace(); } } finally { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.googlecode.osde.internal.igoogle.IgCredentials.java
/** * Makes a HTTP POST request for authentication. * * @param emailUserName the name of the user * @param password the password of the user * @param loginTokenOfCaptcha CAPTCHA token (Optional) * @param loginCaptchaAnswer answer of CAPTCHA token (Optional) * @return http response as a String//from w w w. j a va2 s . c om * @throws IOException */ // TODO: Refactor requestAuthentication() utilizing HttpPost. private static String requestAuthentication(String emailUserName, String password, String loginTokenOfCaptcha, String loginCaptchaAnswer) throws IOException { // Prepare connection. URL url = new URL(URL_GOOGLE_LOGIN); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); logger.fine("url: " + url); // Form the POST params. StringBuilder params = new StringBuilder(); params.append("Email=").append(emailUserName).append("&Passwd=").append(password) .append("&source=OSDE-01&service=ig&accountType=HOSTED_OR_GOOGLE"); if (loginTokenOfCaptcha != null) { params.append("&logintoken=").append(loginTokenOfCaptcha); } if (loginCaptchaAnswer != null) { params.append("&logincaptcha=").append(loginCaptchaAnswer); } // Send POST via output stream. OutputStream outputStream = null; try { outputStream = urlConnection.getOutputStream(); outputStream.write(params.toString().getBytes(IgHttpUtil.ENCODING)); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } // Retrieve response. // TODO: Should the caller of this method need to know the responseCode? int responseCode = urlConnection.getResponseCode(); logger.fine("responseCode: " + responseCode); InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK) ? urlConnection.getInputStream() : urlConnection.getErrorStream(); logger.fine("inputStream: " + inputStream); try { String response = IOUtils.toString(inputStream, IgHttpUtil.ENCODING); return response; } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:com.photon.phresco.service.dependency.impl.DependencyUtils.java
/** * Extracts the given compressed file (of type tar, targz, and zip) into given location. * See also//from w ww . jav a 2 s .c om * {@link ArchiveType} and {@link ArchiveUtil} * @param contentURL * @param path * @throws PhrescoException */ public static void extractFiles(String contentURL, String folderName, File path) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug( "Entering Method DependencyUtils.extractFiles(String contentURL, String folderName, File path)"); } assert !StringUtils.isEmpty(contentURL); String extension = getExtension(contentURL); File archive = new File(Utility.getPhrescoTemp(), UUID.randomUUID().toString() + extension); FileOutputStream fos = null; OutputStream out = null; try { InputStream inputStream = PhrescoServerFactory.getRepositoryManager().getArtifactAsStream(contentURL); fos = new FileOutputStream(archive); out = new BufferedOutputStream(fos); IOUtils.copy(inputStream, out); out.flush(); out.close(); fos.close(); ArchiveType archiveType = getArchiveType(extension); if (isDebugEnabled) { S_LOGGER.debug("extractFiles() path=" + path.getPath()); } ArchiveUtil.extractArchive(archive.toString(), path.getAbsolutePath(), folderName, archiveType); archive.delete(); } catch (FileNotFoundException e) { return; } catch (IOException e) { throw new PhrescoException(e); } catch (PhrescoException pe) { if (pe.getCause() instanceof FileNotFoundException) { return; } throw pe; } finally { Utility.closeStream(fos); Utility.closeStream(out); } }
From source file:controlador.Red.java
/** * Envio de un fichero desde el Cliente Local al Server FTP. * @param rutaLocal Ruta del Cliente donde se buscara el fichero. * @param name Nombre con el cual se buscara y creara el fichero. * @return Estado de la operacion.//from w ww .j a v a2 s.c o m */ public static boolean sendFile(String rutaLocal, String name) { try { url = new URL(conexion + name); URLConnection urlConn = url.openConnection(); OutputStream os = urlConn.getOutputStream(); FileInputStream fis = new FileInputStream(new File(rutaLocal + name)); byte[] buffer = new byte[BUFFER_LENGTH]; int count; while ((count = fis.read(buffer)) > 0) { os.write(buffer, 0, count); } os.flush(); os.close(); fis.close(); return true; } catch (IOException ex) { ex.printStackTrace(); System.out.println("Problema al enviar un fichero al server: " + ex.getLocalizedMessage()); } return false; }
From source file:Main.java
/** * write file//w w w. j a va2 s . co m * * @param file the file to be opened for writing. * @param stream the input stream * @param append if <code>true</code>, then bytes will be written to the end of * the file rather than the beginning * @return return true * @throws RuntimeException if an error occurs while operator FileOutputStream */ public static boolean writeFile(File file, InputStream stream, boolean append) { OutputStream o = null; try { makeDirs(file.getAbsolutePath()); o = new FileOutputStream(file, append); byte data[] = new byte[1024]; int length = -1; while ((length = stream.read(data)) != -1) { o.write(data, 0, length); } o.flush(); return true; } catch (FileNotFoundException e) { throw new RuntimeException("FileNotFoundException occurred. ", e); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (o != null) { try { o.close(); stream.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } }
From source file:com.iggroup.oss.restdoclet.plugin.io.IOUtils.java
/** * Copies an input-stream to an output-stream. This method lacks performance * as it copies characters one by one./*from ww w . j av a 2 s . c o m*/ * * @param input the input-stream. * @param output the output-stream. * @param close <code>true</code> if the output-stream has to be closed * after copying, <code>false</code> otherwise. * @return the number of characters copied. * @throws IOException if an input-output exception occurs. */ public static long copy(final InputStream input, final OutputStream output, final boolean close) throws IOException { long count = 0; int read = 0; do { read = input.read(); if (read != -1) { output.write(read); count++; } } while (read != -1); input.close(); if (close) { output.flush(); output.close(); } return count; }
From source file:com.mingsoft.util.proxy.Proxy.java
/** * ?/*from w w w .j a v a 2 s .c o m*/ * * @param url * ? * @param path * ??? * @throws HttpException * @throws IOException */ public static void get(String url, String path) { try { HttpClient hc = HttpClientBuilder.create().build(); HttpGet gm = new HttpGet(url); HttpResponse response = hc.execute(gm); InputStream is = response.getEntity().getContent(); OutputStream os = new FileOutputStream(path); int c = -1; while ((c = is.read()) != -1) { os.write(c); } is.close(); os.flush(); os.close(); } catch (IOException e2) { e2.printStackTrace(); } }
From source file:com.hkd.socketclient.IOUtil.java
public static final void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) { Thread reader, writer;/*from w w w.j av a2s . co m*/ reader = new Thread() { @Override public void run() { int ch; try { while (!interrupted() && (ch = localInput.read()) != -1) { remoteOutput.write(ch); remoteOutput.flush(); } } catch (IOException e) { //e.printStackTrace(); } } }; writer = new Thread() { @Override public void run() { try { Util.copyStream(remoteInput, localOutput); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }; writer.setPriority(Thread.currentThread().getPriority() + 1); writer.start(); reader.setDaemon(true); reader.start(); try { writer.join(); reader.interrupt(); } catch (InterruptedException e) { } }
From source file:Main.java
public static void saveInstance(OutputStream outputStream, URL schemaURL, String schemaName, Object instance) throws JAXBException, IOException {/* ww w . j a v a2 s . c om*/ Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaURL + " " + schemaName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(instance, outputStream); outputStream.flush(); }