Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte to this buffered output stream.

Usage

From source file:librarymanagementsystem.Base64Converter.java

/**
 * This method writes byte array content into a file.
 *///from   ww  w  .  jav a2  s  .  c  o  m
public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {

    File file = new File(fileName);
    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
    writer.write(content);
    writer.flush();
    writer.close();

}

From source file:Main.java

public static int copyFile(String fileDir, String fileName, byte[] buffer) {
    if (buffer == null) {
        return -2;
    }/*w  w  w  .ja va  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:hr.softwarecity.osijek.utility.Multimedia.java

/**
 * Method for uploading files// w w w.j av a 2  s .  co  m
 * @param path path to put file
 * @param file file to upload
 * @return path to file or Failed keyword on fail
 * @throws java.nio.file.FileSystemException
 * @throws java.io.FileNotFoundException
 */
public static String handleFileUpload(String path, MultipartFile file) throws IOException {
    Logger.getLogger("Multimedia.java").log(Logger.Level.INFO, "Initiating file upload");
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(path, file.getOriginalFilename())));
        stream.write(bytes);
        stream.close();
        Logger.getLogger("Multimedia.java").log(Logger.Level.INFO, "File uploaded");
        return path;
    } else {
        Logger.getLogger("Multimedia.java").log(Logger.Level.ERROR, "File size 0! ");
        return null;
    }
}

From source file:com.cloudhopper.commons.io.demo.FileServerMain.java

private static void saveFileFromUrl(URL url, String path) throws Exception {
    URLConnection urlc = url.openConnection();
    BufferedInputStream bis = new BufferedInputStream(urlc.getInputStream());
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int i;//  w w w  . j a v a2s  . co  m
    while ((i = bis.read()) != -1) {
        bos.write(i);
    }
    bis.close();
    bos.close();
}

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

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

public static long downloadFileFromUrl(String urlPath, File file) {
    long size = 0;
    try {/*w  w  w .  j a v  a 2  s . co 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 byte2file(byte[] bytes, File file) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;//  w ww. ja  va2s .  c  om
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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;//  ww  w. j  ava  2s  .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:com.useekm.types.AbstractGeo.java

public static byte[] gzip(byte[] bytes) {
    try {/*  w  ww .  ja v  a  2s  . c  o  m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
        bufos.write(bytes);
        bufos.close();
        byte[] retval = bos.toByteArray();
        bos.close();
        return retval;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gzip", e);
    }
}

From source file:Main.java

public static boolean bytesToFile(byte[] bfile, File file) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;//from   w w w.j a  v  a2 s.co m
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bfile);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return false;
}