Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void extractEntryContent(ZipInputStream zis, ZipEntry entry, String unzipdir)
        throws IOException, FileNotFoundException {

    String entryFileName = entry.getName();
    String entryPath = unzipdir + File.separator + entryFileName;

    createFile(entryPath);//from   ww  w  .  j  a  v  a  2s .  co m

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryPath));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = zis.read(buffer)) != -1) {
        bos.write(buffer, 0, count);
    }

    bos.close();
}

From source file:Main.java

public static void extractFile(ZipInputStream in, File outdir, String name) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
    int count = -1;
    while ((count = in.read(buffer)) != -1)
        out.write(buffer, 0, count);//w  w  w.j  a  va 2 s  .  co m
    out.close();
}

From source file:Main.java

public static int copyFile(String fileDir, String fileName, byte[] buffer) {
    if (buffer == null) {
        return -2;
    }// w  w w .  j  a  v a  2  s  . co m

    try {
        File file = new File(fileDir);
        if (!file.exists()) {
            file.mkdirs();
        }
        File resultFile = new File(file, fileName);
        if (!resultFile.exists()) {
            resultFile.createNewFile();
        }
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(resultFile, true));
        bufferedOutputStream.write(buffer);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        return 0;

    } catch (Exception e) {
    }
    return -1;
}

From source file:Main.java

public static void writeToFile(InputStream in, File target) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
    int count;//from  w ww  .j  a  va2s.com
    byte data[] = new byte[BUFFER_SIZE];
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
        bos.write(data, 0, count);
    }
    bos.close();
}

From source file:Main.java

/**
 * marshal document object into outputStream.
 * //from  ww w  .ja va 2  s .c  o  m
 * @param sourceDocument
 * @param targetFile
 * @throws ParserConfigurationException
 * @throws TransformerConfigurationException
 * @throws TransformerException
 * @throws IOException
 */
public static void marshal(Document sourceDocument, File targetFile) throws ParserConfigurationException,
        TransformerConfigurationException, TransformerException, IOException {
    BufferedOutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
        marshal(sourceDocument, outputStream);
    } finally {
        outputStream.close();
    }
}

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;/*from  www . ja v a2  s. c  om*/
    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:com.itbeyond.common.EOTrackMe.java

public static void removeSentLines(String lines) {
    int WriteLock_Timeout = 10;
    while (WriteLock_Timeout > 0) {
        try {//ww w. j av  a  2  s.  c om
            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:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//from ww  w.  j av a  2  s  .c  o m
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

public static long downloadFileFromUrl(String urlPath, File file) {
    long size = 0;
    try {//from  ww w .j  a  va2  s  .  c  o m
        URL url = new URL(urlPath);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream());
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
        int i;
        while ((i = bufferedinputstream.read()) != -1) {
            bufferedoutputstream.write(i);
        }
        bufferedinputstream.close();
        bufferedoutputstream.close();
        httpurlconnection.disconnect();
        size = file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;/*w  w  w . jav  a 2  s. c o  m*/
    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;
    }
}