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:com.heliosapm.tsdblite.json.JSON.java

public static void serialize(final Object object, final OutputStream os) {
    if (object == null)
        throw new IllegalArgumentException("Object was null");
    if (os == null)
        throw new IllegalArgumentException("OutputStream was null");
    try {/* w ww .  j a v a2  s. c  om*/
        jsonMapper.writeValue(os, object);
        os.flush();
    } catch (Exception ex) {
        throw new JSONException(ex);
    }
}

From source file:com.appsimobile.appsii.module.weather.WeatherLoadingService.java

private static boolean downloadFile(Context context, ImageDownloadHelper.PhotoInfo photoInfo, String woeid,
        int idx) {

    WeatherUtils weatherUtils = AppInjector.provideWeatherUtils();

    File cacheDir = weatherUtils.getWeatherPhotoCacheDir(context);
    String fileName = weatherUtils.createPhotoFileName(woeid, idx);
    File photoImage = new File(cacheDir, fileName);
    try {/*from w ww .  jav  a2 s .  com*/
        URL url = new URL(photoInfo.url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(30000);
        InputStream in = new BufferedInputStream(connection.getInputStream());
        try {
            OutputStream out = new BufferedOutputStream(new FileOutputStream(photoImage));

            int totalRead = 0;
            try {
                byte[] bytes = new byte[64 * 1024];
                int read;
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                    totalRead += read;
                }
                out.flush();
            } finally {
                out.close();
            }
            if (BuildConfig.DEBUG) {
                Log.d("WeatherLoadingService", "received " + totalRead + " bytes for: " + photoInfo.url);
            }
        } finally {
            in.close();
        }
        return true;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

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

@Override
public void writeTo(OutputStream out) throws IOException {
    out.write(content);
    out.flush();
}

From source file:fr.landel.utils.io.FileUtils.java

/**
 * Write a stream content into another.//from  w  w w . j  a  va2s  .co m
 * 
 * @param inputStream
 *            The input stream
 * @param outputStream
 *            The output stream
 * @throws IOException
 *             thrown if problems occurs during reading
 */
public static void writeStream(final InputStream inputStream, final OutputStream outputStream)
        throws IOException {
    Assertor.that(inputStream).isNotNull().orElseThrow("The 'inpuStream' parameter cannot be null");
    Assertor.that(outputStream).isNotNull().orElseThrow("The 'outputStream' parameter cannot be null");

    int bufferReadSize;
    final byte[] buffer = new byte[BUFFER_SIZE];

    while ((bufferReadSize = inputStream.read(buffer, 0, BUFFER_SIZE)) >= 0) {
        outputStream.write(buffer, 0, bufferReadSize);
    }

    outputStream.flush();
}

From source file:com.odoko.solrcli.actions.CrawlPostAction.java

/**
 * Pipes everything from the source to the dest.  If dest is null, 
 * then everything is read from source and thrown away.
 *///  w  w  w. j a va2s .com
private static void pipe(InputStream source, OutputStream dest) throws IOException {
  byte[] buf = new byte[1024];
  int read = 0;
  while ( (read = source.read(buf) ) >= 0) {
    if (null != dest) dest.write(buf, 0, read);
  }
  if (null != dest) dest.flush();
}

From source file:com.gallatinsystems.common.util.S3Util.java

public static boolean put(String bucketName, String objectKey, byte[] data, String contentType,
        boolean isPublic, String awsAccessId, String awsSecretKey) throws IOException {

    final byte[] md5Raw = MD5Util.md5(data);
    final String md5Base64 = Base64.encodeBase64String(md5Raw).trim();
    final String md5Hex = MD5Util.toHex(md5Raw);
    final String date = getDate();
    final String payloadStr = isPublic ? PUT_PAYLOAD_PUBLIC : PUT_PAYLOAD_PRIVATE;
    final String payload = String.format(payloadStr, md5Base64, contentType, date, bucketName, objectKey);
    final String signature = MD5Util.generateHMAC(payload, awsSecretKey);
    final URL url = new URL(String.format(S3_URL, bucketName, objectKey));

    OutputStream out = null;
    HttpURLConnection conn = null;
    try {// w w  w  .ja  v  a  2s. c o m
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-MD5", md5Base64);
        conn.setRequestProperty("Content-Type", contentType);
        conn.setRequestProperty("Date", date);

        if (isPublic) {
            // If we don't send this header, the object will be private by default
            conn.setRequestProperty("x-amz-acl", "public-read");
        }

        conn.setRequestProperty("Authorization", "AWS " + awsAccessId + ":" + signature);

        out = new BufferedOutputStream(conn.getOutputStream());

        IOUtils.copy(new ByteArrayInputStream(data), out);
        out.flush();

        int status = conn.getResponseCode();
        if (status != 200 && status != 201) {
            log.severe("Error uploading file: " + url.toString());
            log.severe(IOUtils.toString(conn.getInputStream()));
            return false;
        }
        String etag = conn.getHeaderField("ETag");
        etag = etag != null ? etag.replaceAll("\"", "") : null;// Remove quotes
        if (!md5Hex.equals(etag)) {
            log.severe("ETag comparison failed. Response ETag: " + etag + "Locally computed MD5: " + md5Hex);
            return false;
        }
        return true;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
        IOUtils.closeQuietly(out);
    }
}

From source file:ti.modules.titanium.network.TiJsonBody.java

@Override
public void writeTo(OutputStream out, int mode) throws IOException, MimeException {
    out.write(data);/* w ww  . jav  a  2  s .  com*/
    out.flush();
}

From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.ByteArrayBody.java

@Override
public void writeTo(OutputStream outputStream) throws IOException {
    outputStream.write(byteArray);//w  w  w  .j  a  v a  2  s. co  m
    outputStream.flush();
}

From source file:com.zving.platform.SysInfo.java

public static void downloadDB(HttpServletRequest request, HttpServletResponse response) {
    try {/*from w w  w .jav a2  s.  co  m*/
        request.setCharacterEncoding(Constant.GlobalCharset);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=DB_" + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat");
        OutputStream os = response.getOutputStream();

        String path = Config.getContextRealPath() + "WEB-INF/data/backup/DB_"
                + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat";
        new DBExport().exportDB(path);

        byte[] buffer = new byte[1024];
        int read = -1;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            while ((read = fis.read(buffer)) != -1)
                if (read > 0) {
                    byte[] chunk = (byte[]) null;
                    if (read == 1024) {
                        chunk = buffer;
                    } else {
                        chunk = new byte[read];
                        System.arraycopy(buffer, 0, chunk, 0, read);
                    }
                    os.write(chunk);
                    os.flush();
                }
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.scorpio4.util.io.StreamCopy.java

public void process(InputStream input, OutputStream output) throws IOException {
    IOUtils.copy(input, output);//  w  ww. j ava2 s.c  o m
    output.flush();
}