Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

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

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:com.itbeyond.common.EOTrackMe.java

public static void removeSentLines(String lines) {
    int WriteLock_Timeout = 10;
    while (WriteLock_Timeout > 0) {
        try {/*from   w w w  .ja v a 2 s  .c  o  m*/
            if (StringUtils.countMatches(lines, "|") == getLogFileLines()) {
                // Delete the log file
                EOTrackMe.getLogFile().delete();
            } else {
                File logFile = EOTrackMe.getLogFile();
                // We must remove already processed lines
                // As the file is appended
                String thisline;
                StringBuilder fullfile = new StringBuilder();
                BufferedReader br = new BufferedReader(new FileReader(logFile), 8192);
                while ((thisline = br.readLine()) != null) {
                    if (!StringUtils.contains(lines, thisline)) {
                        fullfile.append(thisline + "\n");
                    }
                }
                br.close();

                logFile.delete();
                logFile.createNewFile();

                FileOutputStream writer = new FileOutputStream(EOTrackMe.getLogFile(), false);
                BufferedOutputStream output = new BufferedOutputStream(writer);

                output.write(fullfile.toString().getBytes());
                output.flush();
                output.close();
            }
            break;
        } catch (IOException e) {
            if (WriteLock_Timeout < 5) {
                Utilities.LogError("EOTrackMe.removeSentLines - Write Lock", e);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
            }
            WriteLock_Timeout -= 1;
        }
    }
}

From source file:com.gmt2001.HttpRequest.java

public static HttpResponse getData(RequestType type, String url, String post, HashMap<String, String> headers) {
    Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());

    HttpResponse r = new HttpResponse();

    r.type = type;// w ww .j  a va2s.  c o m
    r.url = url;
    r.post = post;
    r.headers = headers;

    try {
        URL u = new URL(url);

        HttpURLConnection h = (HttpURLConnection) u.openConnection();

        for (Entry<String, String> e : headers.entrySet()) {
            h.addRequestProperty(e.getKey(), e.getValue());
        }

        h.setRequestMethod(type.name());
        h.setUseCaches(false);
        h.setDefaultUseCaches(false);
        h.setConnectTimeout(timeout);
        h.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015");
        if (!post.isEmpty()) {
            h.setDoOutput(true);
        }

        h.connect();

        if (!post.isEmpty()) {
            BufferedOutputStream stream = new BufferedOutputStream(h.getOutputStream());
            stream.write(post.getBytes());
            stream.flush();
            stream.close();
        }

        if (h.getResponseCode() < 400) {
            r.content = IOUtils.toString(new BufferedInputStream(h.getInputStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = true;
        } else {
            r.content = IOUtils.toString(new BufferedInputStream(h.getErrorStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = false;
        }
    } catch (IOException ex) {
        r.success = false;
        r.httpCode = 0;
        r.exception = ex.getMessage();

        com.gmt2001.Console.err.printStackTrace(ex);
    }

    return r;
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;/*from   w  w w.  j a v  a  2s . c om*/
    try {
        i = new FileInputStream(src);
        BufferedInputStream in = new BufferedInputStream(i);
        FileOutputStream o = new FileOutputStream(dst);
        BufferedOutputStream out = new BufferedOutputStream(o);
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        in.close();
        out.close();
        o.close();
        i.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static boolean bufferedCopyStream(InputStream inStream, OutputStream outStream, boolean closeStream)
        throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }//from  w  w w  .  j  a  v  a 2s  .  c  o m
        bos.write(data);
    }
    bos.flush();
    if (closeStream) {
        bos.close();
    }
    return true;
}

From source file:nz.co.fortytwo.freeboard.installer.ZipUtils.java

/**
 * Unzip a zipFile into a directory/*from ww  w.  j  a v  a  2  s  . com*/
 * @param targetDir
 * @param zipFile
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(File targetDir, File zipFile) throws ZipException, IOException {
    ZipFile zip = new ZipFile(zipFile);

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> z = (Enumeration<ZipEntry>) zip.entries();
    while (z.hasMoreElements()) {
        ZipEntry entry = z.nextElement();
        File f = new File(targetDir, entry.getName());
        if (f.isDirectory()) {
            if (!f.exists()) {
                f.mkdirs();
            }
        } else {
            f.getParentFile().mkdirs();
            InputStream in = zip.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
            IOUtils.copy(in, out);
            in.close();
            out.flush();
            out.close();
        }

    }
    zip.close();
}

From source file:com.eryansky.common.utils.io.IoUtils.java

public static void writeStringToFile(String content, String filePath) {
    BufferedOutputStream outputStream = null;
    try {/*from  ww  w .j a v  a  2s  .co  m*/
        outputStream = new BufferedOutputStream(new FileOutputStream(getFile(filePath)));
        outputStream.write(content.getBytes());
        outputStream.flush();
    } catch (Exception e) {
        //      throw new ServiceException("Couldn't write file " + filePath, e);
    } finally {
        IoUtils.closeSilently(outputStream);
    }
}

From source file:Main.java

/**
 * Method to save audio file to SD card/*from  w w w .j a v a 2 s  .  co m*/
 * @param course_title, title of course
 * @param format, the file format, for example .mp3, .txt
 */
public static boolean copyToStorage(File source_file, String course_title, String format) {

    File output_file = getEmptyFileWithStructuredPath(course_title, format);

    try {
        byte[] buffer = new byte[4096];
        int bytesToHold;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source_file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output_file));

        while ((bytesToHold = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesToHold);
        }

        bos.flush();
        bos.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*from w  ww.ja v  a2 s .co  m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:org.clickframes.testframes.TestResultsParser.java

public static ProjectTestResults parseTestResults(File zip) throws IOException {
    // tmp directory
    File tmpDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    tmpDirectory.mkdirs();/*from   ww  w . java2 s.  c  o  m*/

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
    ZipEntry entry;
    int BUFFER = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        File destinationFile = new File(tmpDirectory, entry.getName());
        destinationFile.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(destinationFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }

    File firefoxDir = new File(tmpDirectory, "firefox");

    if (!firefoxDir.exists()) {
        throw new RuntimeException("Invalid test results zip file uploaded. firefox directory does not exist.");
    }

    File testResultsXmlFile = new File(tmpDirectory, "testResults.xml");

    XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(testResultsXmlFile)));
    ProjectTestResults projectTestResults = (ProjectTestResults) d.readObject();
    d.close();

    return projectTestResults;
}

From source file:Main.java

public static Bitmap GetLocalOrNetBitmap(String url) {
    Bitmap bitmap = null;/*from  w ww.j ava 2s. c  o m*/
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream(), 1024);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024);
        copy(in, out);
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}