Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

In this page you can find the example usage for java.io FileOutputStream 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:plugins.marauders.MaraudersDELETEServlet.java

@Override
public HttpResponse handleRequest(HttpRequest request) {
    String body = new String(request.getBody());
    ObjectMapper mapper = new ObjectMapper();
    TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() {
    };//w  ww . j  a  va 2s  . c  o m
    try {
        HashMap<String, Object> map = mapper.readValue(body, typeReference);
        String nameObj = (String) map.get("name");
        String passphraseObj = (String) map.get("passphrase");

        Data d = MaraudersInteractor.getData();
        boolean okPassphrase = passphraseObj.equals(d.passphrase);
        if (okPassphrase) {
            boolean b = MaraudersInteractor.deleteStudent(nameObj);
            if (b) {

                HttpResponse res = HttpResponseFactory.createDelete(Protocol.CLOSE);

                return res;

            } else {
                return HttpResponseFactory.create304NotModified(Protocol.CLOSE);
            }
        } else {
            Random rand = new Random();
            int id = rand.nextInt(d.maxInsultID + 1);

            try {
                Insult i = MaraudersInteractor.getInsult(id + "");
                File f = File.createTempFile("temp", ".txt");

                FileOutputStream fos = new FileOutputStream(f);
                fos.write((i.toString()).getBytes());
                fos.flush();
                fos.close();

                HttpResponse res = HttpResponseFactory.create394Insult(f, Protocol.CLOSE);
                //f.delete();
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                return HttpResponseFactory.create400BadRequest(Protocol.CLOSE);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return HttpResponseFactory.create400BadRequest(Protocol.CLOSE);

}

From source file:com.lee.sdk.utils.Utils.java

/**
 * ???//from  ww  w . j a va  2 s  .  c  o  m
 * 
 * @param is ?
 * @param file 
 * @return true:??false:?
 */
public static boolean streamToFile(InputStream is, File file) {
    boolean bRet = false;
    if (null == is || null == file) {
        return bRet;
    }

    // ?
    File dir = file.getParentFile();
    if (!dir.exists()) {
        dir.mkdirs();
    }

    if (file.exists()) {
        file.delete();
    }

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        byte[] buffer = new byte[STREAM_BUFFER_SIZE];
        int length = -1;
        while ((length = is.read(buffer)) != -1) {
            fos.write(buffer, 0, length);
        }
        fos.flush();
        bRet = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeSafely(fos);
        closeSafely(is);
    }
    return bRet;
}

From source file:plugins.marauders.MaraudersPUTServlet.java

@Override
public HttpResponse handleRequest(HttpRequest request) {
    String body = new String(request.getBody());

    ObjectMapper mapper = new ObjectMapper();
    TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() {
    };/*  w  ww.j  av a2  s.c  o  m*/
    try {
        HashMap<String, Object> map = mapper.readValue(body, typeReference);

        String nameObj = (String) map.get("name");
        String passphraseObj = (String) map.get("passphrase");
        String houseObj = (String) map.get("house");

        String locationObj = (String) map.get("location");
        Student s = new Student();
        s.setHouse(houseObj);
        s.setName(nameObj);
        s.setLocation(locationObj);

        Data d = MaraudersInteractor.getData();
        boolean okPassphrase = passphraseObj.equals(d.passphrase);
        if (okPassphrase) {
            MaraudersInteractor.addStudent(s);

            return HttpResponseFactory.create204NoCon(Protocol.CLOSE);
        } else {
            Random rand = new Random();
            int id = rand.nextInt(d.maxInsultID + 1);

            try {
                Insult i = MaraudersInteractor.getInsult(id + "");
                File f = File.createTempFile("temp", ".txt");

                FileOutputStream fos = new FileOutputStream(f);
                fos.write((i.toString()).getBytes());
                fos.flush();
                fos.close();

                HttpResponse res = HttpResponseFactory.create394Insult(f, Protocol.CLOSE);
                //f.delete();
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                return HttpResponseFactory.create400BadRequest(Protocol.CLOSE);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return HttpResponseFactory.create505NotSupported(Protocol.CLOSE);
}

From source file:org.apache.abdera2.test.client.MultipartRelatedEntityTest.java

License:asdf

public void testMultipartEncoding() throws Exception {
    InputStream input = this.getClass().getResourceAsStream("info.png");
    int BUFF_SIZE = 1024;

    byte[] line = new byte[BUFF_SIZE];
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while (input.read(line) != -1)
        output.write(line);/*from ww w. j  a  v  a2  s .c o  m*/
    input.close();

    Base64 base64 = new Base64();
    byte[] encoded = base64.encode(output.toByteArray());
    ByteArrayInputStream bi = new ByteArrayInputStream(base64.decode(encoded));

    File f = new File("info-out.png");
    if (f.exists())
        f.delete();
    if (f.createNewFile()) {
        FileOutputStream fo = new FileOutputStream(f);
        int end;
        while ((end = bi.read(line)) != -1)
            fo.write(line, 0, end);
        fo.flush();
        fo.close();
    }
}

From source file:com.galenframework.storage.repository.LocalFileStorage.java

@Override
public FileInfo saveImageToStorage(InputStream inputStream) {
    try {/*  www.j  a  v  a2 s.  c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");

        String dirsPath = generateImageDirsPath();

        File dirs = new File(root.getPath() + File.separator + dirsPath);
        dirs.mkdirs();

        String fileName = UUID.randomUUID().toString();
        File file = new File(dirs.getPath() + File.separator + fileName);
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);

        DigestInputStream dis = new DigestInputStream(inputStream, md);

        IOUtils.copy(dis, fos);
        fos.flush();
        fos.close();

        byte[] digest = md.digest();
        String hash = Base64.encodeBase64String(digest);
        return new FileInfo(hash, dirsPath + File.separator + fileName);
    } catch (Exception ex) {
        throw new RuntimeException("Cannot save image to storage", ex);
    }
}

From source file:com.example.android.camera2basic.AgeGender.java

public File bitmapToFile(Bitmap bmp) {

    //create a file to write bitmap data
    //        File f = new File(mContext.getCacheDir(), "CompressedFile.jpeg");
    File f = new File(DirectoryPath + "/CompressedAgeFile.jpeg");
    try {/* www  .j  a  v  a  2 s .co m*/
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return f;
}

From source file:com.example.android.camera2basic.VisualRecognition.java

public File bitmapToFile(Bitmap bmp) {

    //create a file to write bitmap data
    //        File f = new File(mContext.getCacheDir(), "CompressedFile.jpeg");
    File f = new File(DirectoryPath + "/CompressedVisualFile.jpeg");
    try {/*  ww  w.jav  a  2  s.com*/
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return f;
}

From source file:UrlHelper.java

public static void downloadFile(String adresse, File dest) {
    BufferedReader reader = null;
    FileOutputStream fos = null;
    InputStream in = null;//from   w  w  w.  j  av  a 2  s . c o  m
    try {

        // cration de la connection
        URL url = new URL(adresse);
        URLConnection conn = url.openConnection();
        String FileType = conn.getContentType();

        int FileLenght = conn.getContentLength();
        if (FileLenght == -1) {
            throw new IOException("Fichier non valide.");
        }

        // lecture de la rponse
        in = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        if (dest == null) {
            String FileName = url.getFile();
            FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
            dest = new File(FileName);
        }
        fos = new FileOutputStream(dest);
        byte[] buff = new byte[1024];
        int l = in.read(buff);
        while (l > 0) {
            fos.write(buff, 0, l);
            l = in.read(buff);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.example.android.camera2basic.RecognizeText.java

public File bitmapToFile(Bitmap bmp) {

    //create a file to write bitmap data
    //        File f = new File(mContext.getCacheDir(), "CompressedFile.jpeg");
    File f = new File(DirectoryPath + "/CompressedTextFile.jpeg");
    try {/*from w  w  w . ja va 2 s.co m*/
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return f;
}

From source file:com.manning.blogapps.chapter11.rome.DiskFeedInfoCache.java

public void setFeedInfo(URL url, SyndFeedInfo feedInfo) {

    String fileName = cachePath + File.separator + "feed_" + url.hashCode();
    FileOutputStream fos;
    try {//from  w ww .  j a v  a 2s .c  o m
        fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(feedInfo);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        // Error writing to cahce is fatal
        throw new RuntimeException("Attempting to write to cache", e);
    }
}