Example usage for java.io OutputStream flush

List of usage examples for java.io OutputStream flush

Introduction

In this page you can find the example usage for java.io OutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:net.netheos.pcsapi.request.JSONEntity.java

@Override
public void writeTo(OutputStream out) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//from   www.j a  v  a 2 s.c o m
    out.write(content);
    out.flush();
}

From source file:cn.com.cennavi.visualizer.service.ExecuteController.java

private String createFile(byte[] bs, String filenamekey, String ext) throws IOException {
    String filename = System.currentTimeMillis() + "_" + filenamekey + "." + ext;
    String path = System.getProperty("TEMP_TPG_PATH") + File.separator + filename;
    File dir = new File(System.getProperty("TEMP_TPG_PATH"));
    if (!dir.exists() || !dir.isDirectory()) {
        dir.mkdirs();//from   w  w  w  .  j  a v a 2  s . c om
    }

    OutputStream out = new FileOutputStream(path);
    out.write(bs);
    out.flush();
    out.close();
    return filename;
}

From source file:com.ejisto.modules.dao.remote.BaseRemoteDao.java

String remoteCall(String request, String requestPath, String method) {
    boolean acquired = false;
    try {/*ww  w  . j  a va2s.  c  om*/
        CONCURRENT_REQUEST_MANAGER.acquire();
        acquired = true;
        HttpURLConnection connection = openConnection(requestPath, method);
        OutputStream out = connection.getOutputStream();
        out.write(request.getBytes(ConfigurationManager.UTF_8));
        out.flush();
        out.close();
        return new String(readInputStream(connection.getInputStream()), Charset.forName("UTF-8"));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException("thread interrupted", e);
    } catch (IOException e) {
        throw new IllegalStateException("IOException", e);
    } finally {
        if (acquired) {
            CONCURRENT_REQUEST_MANAGER.release();
        }
    }
}

From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java

private String getRawResponse(String rawRequest, int timeoutMillis) throws IOException {
    URLConnection connection = apiUrl.openConnection();
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-type", "application/json");
    connection.setRequestProperty("User-Agent", client);
    connection.setDoOutput(true);/*from   www  . ja va  2  s .co m*/
    connection.setReadTimeout(timeoutMillis);

    final OutputStream out = connection.getOutputStream();
    out.write(rawRequest.getBytes(StandardCharsets.UTF_8));
    out.flush();
    out.close();

    log.debug("wrote request " + rawRequest + " (timeout wanted: " + timeoutMillis + ", actual: "
            + connection.getReadTimeout() + ")");

    String response = null;
    try (final InputStream in = connection.getInputStream()) {
        response = IOUtils.toString(in, StandardCharsets.UTF_8);
    }

    return response;
}

From source file:edu.vt.middleware.crypt.AbstractCli.java

/**
 * Attempts to close the given output stream.
 *
 * @param  out  output stream to close.//from w  ww. j a va  2s .  co m
 */
protected void closeStream(final OutputStream out) {
    try {
        out.flush();
        if (out != System.out) {
            out.close();
        }
    } catch (IOException ioex) {
        System.err.println("Error closing output stream.");
    }
}

From source file:io.confluent.kafkarest.tools.ConsumerPerformance.java

private <T> T request(String target, String method, byte[] entity, String entityLength,
        TypeReference<T> responseFormat) {
    HttpURLConnection connection = null;
    try {/*from   w  w  w  .  ja v  a  2s  .  c  om*/
        URL url = new URL(target);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method);
        if (entity != null) {
            connection.setRequestProperty("Content-Type", Versions.KAFKA_MOST_SPECIFIC_DEFAULT);
            connection.setRequestProperty("Content-Length", entityLength);
        }
        connection.setDoInput(true);
        connection.setUseCaches(false);
        if (entity != null) {
            connection.setDoOutput(true);
            OutputStream os = connection.getOutputStream();
            os.write(entity);
            os.flush();
            os.close();
        }

        int responseStatus = connection.getResponseCode();
        if (responseStatus >= 400) {
            InputStream es = connection.getErrorStream();
            ErrorMessage errorMessage = jsonDeserializer.readValue(es, ErrorMessage.class);
            es.close();
            throw new RuntimeException(String.format("Unexpected HTTP error status %d for %s request to %s: %s",
                    responseStatus, method, target, errorMessage.getMessage()));
        }
        if (responseStatus != HttpURLConnection.HTTP_NO_CONTENT) {
            InputStream is = connection.getInputStream();
            T result = serializer.readValue(is, responseFormat);
            is.close();
            return result;
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:com.pursuer.reader.easyrss.network.NetworkClient.java

public InputStream doPostStream(final String url, final String params) throws IOException, NetworkException {
    final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(40 * 1000);/*from www . ja  va 2 s .  com*/
    conn.setReadTimeout(30 * 1000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    if (auth != null) {
        conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth);
    }
    conn.setDoInput(true);
    conn.setDoOutput(true);
    final OutputStream output = conn.getOutputStream();
    output.write(params.getBytes());
    output.flush();
    output.close();

    conn.connect();
    try {
        final int resStatus = conn.getResponseCode();
        if (resStatus == HttpStatus.SC_UNAUTHORIZED) {
            ReaderAccountMgr.getInstance().invalidateAuth();
        }
        if (resStatus != HttpStatus.SC_OK) {
            throw new NetworkException("Invalid HTTP status " + resStatus + ": " + url + ".");
        }
    } catch (final IOException exception) {
        if (exception.getMessage() != null && exception.getMessage().contains("authentication")) {
            ReaderAccountMgr.getInstance().invalidateAuth();
        }
        throw exception;
    }
    return conn.getInputStream();
}

From source file:net.sf.jsog.spring.JsogView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // This is the string that will ultimately be rendered.
    String responseString;//from  www .  jav a 2 s .co m

    // Build the result object
    JSOG result;
    if ((model.size() == 1 || model.size() == 2) && model.containsKey("JSOG")) {
        result = (JSOG) model.get("JSOG");
    } else {
        result = modelToJsog(model);
    }

    // If the JSONP callback parameter is specified, grab it
    String callback = request.getParameter(jsonpCallbackParam);
    if (callback != null) {
        responseString = callback + "(" + result.toString() + ")";
    } else {
        responseString = result.toString();
    }

    // Setup the response
    byte[] responseBytes = responseString.getBytes(encoding);
    response.setContentType(outputContentType.toString());
    response.setCharacterEncoding(encoding.name());
    response.setContentLength(responseBytes.length);

    // Write the response
    OutputStream out = response.getOutputStream();
    out.write(responseBytes);
    out.flush();
    out.close();
}

From source file:io.github.kitarek.elasthttpd.plugins.consumers.file.consumer.HttpFileWriteRequestConsumer.java

private void writeAndFlushUnchecked(HttpEntity entity, OutputStream outputStream) throws IOException {
    entity.writeTo(outputStream);//from   ww  w . ja va 2s . c o  m
    outputStream.flush();
}

From source file:ExtraDotOutputStream.java

public void testMain() throws IOException {
        String data = ".This is a test\r\nof the thing.\r\nWe should not have much trouble.\r\n.doubled?\r\nor not?\n.doubled\nor not?\r\n\r\n\n\n\r\r\r\n";
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        OutputStream os = new ExtraDotOutputStream(bOut);
        os.write(data.getBytes());//from   w  w  w  .ja v a2 s  .com
        os.flush();
        String expected = "..This is a test\r\nof the thing.\r\nWe should not have much trouble.\r\n..doubled?\r\nor not?\r\n..doubled\r\nor not?\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
        assertEquals(expected, bOut.toString());
    }