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:be.i8c.sag.util.FileUtils.java

/**
 * Extract files from a zipped (and jar) file
 *
 * @param internalDir The directory you want to copy
 * @param zipFile The file that contains the content you want to copy
 * @param to The directory you want to copy to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 *///from w ww  .j a  v  a2  s  . c  o  m
public static void extractDirectory(String internalDir, File zipFile, File to, boolean deleteOnExit)
        throws IOException {
    try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
        while (true) {
            ZipEntry zipEntry = zip.getNextEntry();
            if (zipEntry == null)
                break;

            if (zipEntry.getName().startsWith(internalDir) && !zipEntry.isDirectory()) {
                File f = createFile(new File(to, zipEntry.getName()
                        .replace((internalDir.endsWith("/") ? internalDir : internalDir + "/"), "")));
                if (deleteOnExit)
                    f.deleteOnExit();
                OutputStream bos = new FileOutputStream(f);
                try {
                    IOUtils.copy(zip, bos);
                } finally {
                    bos.flush();
                    bos.close();
                }
            }

            zip.closeEntry();
        }
    }
}

From source file:Main.java

public static void saveXML(Document doc, OutputStream stream) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;//from   ww w .j a va 2 s.  c  om
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stream);
        transformer.transform(source, result);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean writeFile(String filePath, InputStream inputStream, boolean append) {
    OutputStream o = null;
    boolean result = false;
    try {//from www  .  j av a2s  .com
        o = new FileOutputStream(filePath);
        byte data[] = new byte[1024];
        int length = -1;
        while ((length = inputStream.read(data)) != -1) {
            o.write(data, 0, length);
        }
        o.flush();
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    return result;
}

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Save compressed stream.//w w w  .  j av  a2s.c  om
 *
 * @param buffer
 *            the buffer
 * @param out
 *            the out
 * @param len
 *            the len
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void saveCompressedStream(final byte[] buffer, final OutputStream out, final int len)
        throws IOException {
    try {
        out.write(buffer, 0, len);

    } catch (Exception e) {
        out.flush();
        out.close();

        IOException ioe = new IOException("Not valid archive file type.");
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:de.handtwerk.android.IoHelper.java

public static void copy(InputStream pIn, OutputStream pOut) throws IOException {

    byte[] buffer = new byte[1024];
    int read;/*  w  ww  .j ava  2  s.co  m*/
    while ((read = pIn.read(buffer)) != -1) {
        pOut.write(buffer, 0, read);
    }

    pIn.close();
    pOut.flush();
    pOut.close();
}

From source file:com.teleca.jamendo.util.download.DownloadTask.java

private static void downloadCover(DownloadJob job) {

    PlaylistEntry mPlaylistEntry = job.getPlaylistEntry();
    String mDestination = job.getDestination();
    String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination);
    File file = new File(path + "/" + "cover.jpg");
    // check if cover already exists
    if (file.exists()) {
        Log.v(JamendoApplication.TAG, "File exists - nothing to do");
        return;/*from   w ww  . j ava2  s . c  o  m*/
    }

    String albumUrl = mPlaylistEntry.getAlbum().getImage();
    if (albumUrl == null) {
        Log.w(JamendoApplication.TAG, "album Url = null. This should not happened");
        return;
    }
    albumUrl = albumUrl.replace("1.100", "1.500");

    InputStream stream = null;
    URL imageUrl;
    Bitmap bmp = null;

    // download cover
    try {
        imageUrl = new URL(albumUrl);

        try {
            stream = imageUrl.openStream();
            bmp = BitmapFactory.decodeStream(stream);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v(JamendoApplication.TAG, "download Cover IOException");
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        Log.v(JamendoApplication.TAG, "download CoverMalformedURLException");
        e.printStackTrace();
    }

    // save cover to album directory
    if (bmp != null) {

        try {
            file.createNewFile();
            OutputStream outStream = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();

            Log.v(JamendoApplication.TAG, "Album cover saved to sd");

        } catch (FileNotFoundException e) {
            Log.w(JamendoApplication.TAG, "FileNotFoundException");

        } catch (IOException e) {
            Log.w(JamendoApplication.TAG, "IOException");
        }

    }
}

From source file:Main.java

public static void copy(InputStream stream, String path) throws IOException {

    final File file = new File(path);
    if (file.exists()) {
        file.delete();/*from w  w  w  .j a  va2 s.  co m*/
    }
    final File parentFile = file.getParentFile();

    if (!parentFile.exists()) {
        //noinspection ResultOfMethodCallIgnored
        parentFile.mkdir();
    }

    if (file.exists()) {
        return;
    }
    OutputStream myOutput = new FileOutputStream(path, true);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = stream.read(buffer)) >= 0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    stream.close();

}

From source file:Main.java

public static void copy(String src, String dst) throws IOException {
    InputStream is = null;/*from  w  w  w.  ja v  a 2 s  .c om*/
    OutputStream os = null;
    try {
        is = new FileInputStream(src);
        os = new FileOutputStream(dst);

        byte[] buf = new byte[1024];
        for (;;) {
            int n = is.read(buf);
            if (n <= 0)
                break;
            os.write(buf, 0, n);
        }
        os.flush();
    } finally {
        if (is != null)
            is.close();
        if (os != null)
            os.close();
    }
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???//  w w  w  .  jav  a 2s.  c  om
 * @param request
 * @param response
 */
public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 
    int width = 60, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // ?
    Graphics g = image.getGraphics();

    // ??
    Random random = new Random();

    // 
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    // 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    // 
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // ?155?????
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789";
    // ????(4?)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        int start = random.nextInt(base.length());
        String rand = base.substring(start, start + 1);
        sRand = sRand.concat(rand);
        // ??
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);
    }

    // ??SESSION
    request.getSession().setAttribute("entrymrand", sRand);

    // 
    g.dispose();
    OutputStream out = response.getOutputStream();
    // ?
    ImageIO.write(image, "JPEG", out);

    out.flush();
    out.close();
}

From source file:FileUtils.java

public static void writeFile(File file, byte[] data) throws IOException {

    OutputStream stream = new FileOutputStream(file);
    try {// www . j ava 2 s.  co m
        stream.write(data);
    } finally {
        try {
            stream.flush();
        } finally {
            stream.close();
        }
    }
}