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:io.druid.java.util.common.CompressionUtils.java

/**
 * Copy inputStream to out while wrapping out in a GZIPOutputStream
 * Closes both input and output/*from   w w  w  . j a v a 2s . c o  m*/
 *
 * @param inputStream The input stream to copy data from. This stream is closed
 * @param out         The output stream to wrap in a GZIPOutputStream before copying. This stream is closed
 *
 * @return The size of the data copied
 *
 * @throws IOException
 */
public static long gzip(InputStream inputStream, OutputStream out) throws IOException {
    try (GZIPOutputStream outputStream = new GZIPOutputStream(out)) {
        final long result = ByteStreams.copy(inputStream, outputStream);
        out.flush();
        return result;
    } finally {
        inputStream.close();
    }
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * byte[]??/* w ww  .j  av a 2  s  . co m*/
 */
public static void write(byte[] data, OutputStream output) throws IOException {
    ByteArrayInputStream input = null;
    try {
        input = new ByteArrayInputStream(data);
        copy(input, output);
        output.flush();
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:most.voip.api.Utils.java

/**
 * Copy the specified resource file from the assets folder into the "files dir" of this application, so that this resource
 * can be opened by the Voip Lib by providing it the absolute path of the copied resource
 * @param ctx The application context//from w w  w .j  a va 2 s.  c  o m
 * @param assetPath The path of the resource (e.g on_hold.wav or sounds/on_hold.wav)
 * @return the absolute path of the copied resource, or null if no file was copied.
 */
public static String getResourcePathByAssetCopy(Context ctx, String assetSubFolder, String fileToCopy) {
    Log.d(TAG, "getResourcePathByAssetCopy on folder *" + assetSubFolder + "* for file:" + fileToCopy);
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;
    String filename = null;

    try {
        files = assetManager.list(assetSubFolder);
        Log.d(TAG, "Found " + files.length + " files");
        if (files.length > 0) {
            for (String f : files) {
                Log.d(TAG, "Found resource:" + f);
                if (f == null)
                    continue;
                if (f.equals(fileToCopy)) {

                    filename = f;
                    break;
                }
            }

            Log.d(TAG, "Found:" + filename);
            if (filename == null)
                return null;

            InputStream in = null;
            OutputStream out = null;
            try {

                in = assetManager.open(filename);
                File outFile = new File(ctx.getExternalFilesDir(null), filename);
                Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath());
                out = new FileOutputStream(outFile);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                return outFile.getAbsolutePath();
            } catch (IOException e) {
                Log.e(TAG, "Failed to copy asset file: " + filename, e);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    return null;
}

From source file:com.clutch.ClutchSync.java

private static void complete(File tempDir, JSONObject files) {
    String versionName = "";
    try {// w w  w.  j  a  v  a 2s  .  c om
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not get bundle version");
        e.printStackTrace();
    }

    if (newFilesDownloaded) {
        File dir = context.getDir("_clutch" + versionName, 0);

        // Write out __files.json
        OutputStream outFilesList;
        try {
            outFilesList = new FileOutputStream(new File(tempDir, "__files.json"));
            outFilesList.write(files.toString().getBytes());
            outFilesList.flush();
            outFilesList.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Could not write out the Clutch files hash cache: " + e);
            thisIsHappening = false;
            return;
        } catch (IOException e) {
            Log.e(TAG, "Could not write out the Clutch files hash cache: " + e);
            thisIsHappening = false;
            return;
        }

        deleteDir(dir);
        if (!tempDir.renameTo(dir)) {
            Log.e(TAG, "Could not rename " + tempDir.getPath() + " to " + dir.getPath());
        }

        ClutchConf.setConf(null); // In ObjC we actually write the contents of the conf in setConf
                                  // Let's see if this works.
        ClutchConf.getConf();

        if (conf.optBoolean("_dev") || newFilesDownloaded) {
            pendingReload = true;
        }

        if (conf.optBoolean("_dev")) {
            for (ClutchView clutchView : clutchViews) {
                clutchView.contentChanged();
            }
            if (!conf.optBoolean("_toolbar")) {
                shouldWatchForChanges = true;
                watchForChanges();
            }
        }
    }

    thisIsHappening = false;
}

From source file:com.ushahidi.android.app.net.MainHttpClient.java

public static byte[] fetchImage(String address) throws MalformedURLException, IOException {
    InputStream in = null;/*from w w  w . j  a  v a2 s .c  om*/
    OutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(address).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 4 * 1024);
        copy(in, out);
        out.flush();

        // need to close stream before return statement
        closeStream(in);
        closeStream(out);

        return dataStream.toByteArray();
    } catch (IOException e) {
        // android.util.Log.e("IO", "Could not load buddy icon: " + this,
        // e);

    } finally {
        closeStream(in);
        closeStream(out);

    }
    return null;

}

From source file:com.netsteadfast.greenstep.util.UploadSupportUtils.java

public static File getRealFile(String uploadOid) throws ServiceException, IOException, Exception {
    if (StringUtils.isBlank(uploadOid)) {
        throw new Exception("parameter is blank!");
    }//from w w  w  .ja  v a  2 s  .c om
    SysUploadVO uploadObj = findUpload(uploadOid);
    File packageFile = null;
    if (!YesNo.YES.equals(uploadObj.getIsFile())) {
        File dir = new File(Constants.getWorkTmpDir() + "/" + UploadSupportUtils.class.getSimpleName());
        if (!dir.exists() || !dir.isDirectory()) {
            FileUtils.forceMkdir(dir);
        }
        String tmpFileName = dir.getPath() + "/" + SimpleUtils.getUUIDStr() + "."
                + getFileExtensionName(uploadObj.getShowName());
        dir = null;
        OutputStream fos = null;
        try {
            packageFile = new File(tmpFileName);
            fos = new FileOutputStream(packageFile);
            IOUtils.write(uploadObj.getContent(), fos);
            fos.flush();
        } catch (IOException e) {
            throw e;
        } finally {
            if (fos != null) {
                fos.close();
            }
            fos = null;
        }
    } else {
        String uploadDir = getUploadFileDir(uploadObj.getSystem(), uploadObj.getSubDir(), uploadObj.getType());
        packageFile = new File(uploadDir + "/" + uploadObj.getFileName());
    }
    if (!packageFile.exists()) {
        throw new Exception("File is missing: " + uploadObj.getFileName());
    }
    return packageFile;
}

From source file:com.sap.cloudlabs.connectivity.proxy.ProxyServlet.java

/**
 * Pipes a given <code>InputStream</code> into the given
 * <code>OutputStream</code>
 * //  ww  w  . java2 s. c  o m
 * @param in
 *            <code>InputStream</code>
 * @param out
 *            <code>OutputStream</code>
 * @throws IOException
 */
private static void pipe(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
    in.close();
    out.flush();
    out.close();
}

From source file:net.dorokhov.pony.web.server.common.StreamingViewRenderer.java

/**
 * Copy the given byte range of the given input to the given output.
 * @param input The input to copy the given range to the given output for.
 * @param output The output to copy the given range from the given input for.
 * @param start Start of the byte range.
 * @param length Length of the byte range.
 * @throws IOException If something fails at I/O level.
 *//*from  w  w  w  . j  a va2s .  c o  m*/
private static void copy(InputStream input, OutputStream output, long inputSize, long start, long length)
        throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int read;

    if (inputSize == length) {
        // Write full range.
        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
            output.flush();
        }
    } else {
        //noinspection ResultOfMethodCallIgnored
        input.skip(start);
        long toRead = length;

        while ((read = input.read(buffer)) > 0) {
            if ((toRead -= read) > 0) {
                output.write(buffer, 0, read);
                output.flush();
            } else {
                output.write(buffer, 0, (int) toRead + read);
                output.flush();
                break;
            }
        }
    }
}

From source file:com.concursive.connect.web.modules.documents.beans.FileDownload.java

public static void send(OutputStream outputStream, InputStream is) throws Exception {
    BufferedInputStream inputStream = new BufferedInputStream(is);
    byte[] buf = new byte[4 * 1024];
    // 4K buffer/* www .j a v  a2  s . c om*/
    int len;
    while ((len = inputStream.read(buf, 0, buf.length)) != -1) {
        outputStream.write(buf, 0, len);
    }
    outputStream.flush();
    outputStream.close();
    inputStream.close();
}

From source file:jp.terasoluna.fw.web.struts.actions.FileDownloadUtil.java

/**
 * uEU_E??[h?B/*from  w ww . ja  v  a2s  .  c o m*/
 * @param downloadObject _E??[h??B
 * @param request NGXg?B
 * @param response X|X?B
 *
 * @throws IOException _E??[h?oO????B
 */
public static void download(AbstractDownloadObject downloadObject, HttpServletRequest request,
        HttpServletResponse response, boolean forceDownload) throws IOException {

    // downloadObjectnull???A??
    if (downloadObject == null) {
        if (log.isWarnEnabled()) {
            log.warn("No download object.");
        }
        return;
    }

    // wb_??B
    Map<String, List<String>> additionalHeaders = downloadObject.getAdditionalHeaders();

    // wb_?null???A??
    if (additionalHeaders == null) {
        if (log.isWarnEnabled()) {
            log.warn("Header must not be null.");
        }
        return;
    }

    // wb_??B
    Set<Entry<String, List<String>>> entrySet = additionalHeaders.entrySet();
    for (Entry<String, List<String>> entry : entrySet) {
        String headerName = entry.getKey();
        List<String> headerValues = entry.getValue();

        // wb_?L?[lXgnull???A??
        if (headerValues == null || headerName == null) {
            if (log.isWarnEnabled()) {
                log.warn("Header name and value must not be null.");
            }
            return;
        }
        for (String headerValue : headerValues) {
            // wb_?lnull
            if (headerValue == null) {
                headerValue = "";
            }
            response.addHeader(headerName, headerValue);
        }
    }

    // GR?[fBO?
    String charSet = downloadObject.getCharset();
    if (StringUtils.isNotEmpty(charSet)) {
        response.setCharacterEncoding(downloadObject.getCharset());
    }

    // Reg^Cv?
    String contentType = downloadObject.getContentType();
    if (StringUtils.isNotEmpty(contentType)) {
        response.setContentType(downloadObject.getContentType());
    }

    // f?[^TCY?
    int contentLength = downloadObject.getLengthOfData();
    if (contentLength > 0) {
        response.setContentLength(downloadObject.getLengthOfData());
    }

    // t@C????A??B
    // ?????B
    String name = downloadObject.getName();
    if (name != null) {
        name = encoder.encode(name, request, response);
    } else {
        name = encoder.encode("", request, response);
    }
    setFileName(response, name, forceDownload);

    InputStream inputStream = downloadObject.getStream();
    OutputStream outputStream = null;

    try {
        // _E??[h???s
        outputStream = response.getOutputStream();
        Streams.copy(inputStream, outputStream, false);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.flush();
            outputStream.close();
        }
    }

}