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:com.knowbout.epg.processor.Downloader.java

private boolean downloadFile(FTPFile remoteFile) throws IOException {
    boolean success = false;
    File file = new File(destinationFolder + File.separator + remoteFile.getName());
    long lastModified = remoteFile.getTimestamp().getTimeInMillis();
    log.debug("Remote file is " + remoteFile.getName() + " local file is " + file.getAbsoluteFile()
            + " does it exist:" + file.exists());
    if (forceDownload || !file.exists() || (file.lastModified() < lastModified)) {
        log.debug("Downloading " + remoteFile.getName() + " " + remoteFile.getSize() + " to "
                + file.getAbsolutePath());

        FileOutputStream fos = new FileOutputStream(file);
        client.retrieveFile(remoteFile.getName(), fos);
        fos.close();/*from w w  w  . ja  va 2 s  .  c  o m*/
        fos.flush();
        file.setLastModified(lastModified);
        success = true;
    }
    return success;
}

From source file:com.logsniffer.model.file.FileLogTest.java

@Test
public void testReadInt() throws IOException {
    File openFile = File.createTempFile("test", "txt");
    openFile.deleteOnExit();/*from  w w w. ja  v  a2 s.c  o m*/
    FileOutputStream out = new FileOutputStream(openFile);
    FileLog flog = new FileLog(openFile);
    IOUtils.write("line1\n", out);
    out.flush();
    ByteLogInputStream lis = new DirectFileLogAccess(flog).getInputStream(null);
    // Log instanatiated before data is written
    assertEquals(-1, lis.read());
    flog = new FileLog(openFile);
    lis = new DirectFileLogAccess(flog).getInputStream(null);
    assertEquals('l', lis.read());
    assertEquals('i', lis.read());
    assertEquals('n', lis.read());
    assertEquals('e', lis.read());
    assertEquals('1', lis.read());
    assertEquals('\n', lis.read());
    assertEquals(-1, lis.read());

    // Write more, but lis doesn't see the new data due to size limitation
    IOUtils.write("l2\n", out);
    out.flush();
    assertEquals(-1, lis.read());
    lis.close();
}

From source file:com.clueride.dao.FileImageStore.java

@Override
public Integer addNew(Integer locationId, InputStream imageData) throws FileNotFoundException {
    Integer newSequenceId = 1;/*from w  w  w  .j  a  v  a2s  . c  o  m*/
    File locDir;
    File newImageFile;
    // Make sure only one thread at a time is updating the list of image files
    synchronized (locationDirectories) {
        if (locationDirectories.containsKey(locationId)) {
            // Use existing directory; find maximum
            locDir = locationDirectories.get(locationId);
            for (File imageFile : locDir.listFiles()) {
                Integer existingSeqId = getIdFromFileName(imageFile);
                if (newSequenceId <= existingSeqId) {
                    newSequenceId = existingSeqId + 1;
                }
            }
        } else {
            // create new directory
            locDir = new File(BASE_DIR + File.separator + locationId);
            locDir.mkdirs();
            locationDirectories.put(locationId, locDir);
        }
        String newFileName = locDir.getPath() + File.separator + newSequenceId + ".jpg";
        newImageFile = new File(newFileName);
        try {
            newImageFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // Ready to write the file
    try {
        FileOutputStream out = new FileOutputStream(newImageFile);
        IOUtils.copy(imageData, out);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return newSequenceId;
}

From source file:com.ephesoft.dcma.util.XMLUtil.java

/**
 * To transform XML with Stream.//from   ww w .ja  v  a 2s  . co  m
 * 
 * @param fis {@link InputStream}
 * @param pathToTargetXML {@link String}
 * @param xslStream {@link InputStream}
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws FileNotFoundException
 * @throws TransformerException
 * @throws IOException
 */
public static void transformXMLWithStream(final InputStream fis, final String pathToTargetXML,
        final InputStream xslStream) throws TransformerFactoryConfigurationError,
        TransformerConfigurationException, FileNotFoundException, TransformerException, IOException {
    FileOutputStream fileOutputStream = null;
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory
                .newTransformer(new javax.xml.transform.stream.StreamSource(xslStream));
        fileOutputStream = new FileOutputStream(pathToTargetXML);
        transformer.transform(new javax.xml.transform.stream.StreamSource(fis),
                new javax.xml.transform.stream.StreamResult(fileOutputStream));
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.flush();
            fileOutputStream.close();
        }

        if (fis != null) {
            fis.close();
        }
    }
}

From source file:com.logsniffer.model.file.FileLogTest.java

@Test
public void testReadArray() throws IOException {
    File openFile = File.createTempFile("test", "txt");
    openFile.deleteOnExit();/*ww w .  j a v  a  2 s. c  om*/
    FileOutputStream out = new FileOutputStream(openFile);
    FileLog flog = new FileLog(openFile);
    IOUtils.write("line1\n", out);
    out.flush();
    ByteLogInputStream lis = new DirectFileLogAccess(flog).getInputStream(null);
    byte[] buffer = new byte[1024];
    // Log instantiated before data is written
    assertEquals(-1, lis.read(buffer));
    flog = new FileLog(openFile);
    lis = new DirectFileLogAccess(flog).getInputStream(null);

    assertEquals(6, lis.read(buffer));
    assertEquals(-1, lis.read(buffer));

    // Write more, but lis doesn't see the new data due to size limitation
    IOUtils.write("l2\n", out);
    out.flush();
    assertEquals(-1, lis.read(buffer));
    LogPointer pointer = lis.getPointer();

    // Reopen input stream
    flog = new FileLog(openFile);
    lis = new DirectFileLogAccess(flog).getInputStream(pointer);
    assertEquals(3, lis.read(buffer, 0, 3));
    assertEquals(-1, lis.read(buffer, 0, 1));
    assertEquals('l', buffer[0]);
    assertEquals('2', buffer[1]);
    assertEquals('\n', buffer[2]);
}

From source file:org.atmars.action.SendMessageAction.java

private boolean ImageUpload() throws IOException {

    this.imageURI = null;

    if (upload.equals("null")) {
        return false;
    }//from  w  ww. j a va  2s  .  c  o  m
    String user_dir = "image\\" + String.valueOf(currentUserFromSession.getUserId()) + "\\";
    String dir = webRootPath + user_dir;
    if (new File(dir).exists()) {

    } else {
        new File(dir).mkdir();
        System.out.println("user direction created\n");
    }
    int index = uploadFileName.lastIndexOf(".");
    String fileType = uploadFileName.substring(index + 1);
    System.out.println(fileType);
    String filename = String.valueOf(System.currentTimeMillis()) + "." + fileType;
    System.out.println("file name is " + filename);
    String fn = dir + filename;
    BASE64Decoder decoder = new BASE64Decoder();
    String upload2 = new String();
    int index1 = this.upload.indexOf(',');
    upload2 = this.upload.substring(index1 + 1);
    byte[] b = decoder.decodeBuffer(upload2);

    FileOutputStream fos = new FileOutputStream(fn, true);
    fos.write(b);
    fos.flush();
    imageURI = "image/" + String.valueOf(currentUserFromSession.getUserId()) + "/" + filename;
    return true;
}

From source file:com.k42b3.kadabra.handler.System.java

public void uploadFile(String path, byte[] content) throws Exception {
    logger.info(basePath + "/" + path);

    FileOutputStream fos = new FileOutputStream(basePath + "/" + path);

    if (fos != null) {
        fos.write(content);//from   w  ww  . j a v  a 2  s.c o m

        fos.flush();
        fos.close();
    }
}

From source file:corner.encrypt.services.impl.CipherKey.java

public void persistCipher() {
    FileOutputStream fos = null;
    try {/*from   w w w.  j a v a2  s.c  om*/
        fos = new FileOutputStream(persisFile);
        fos.write(Base64.encodeBase64(this.cipher));
        fos.flush();
    } catch (Throwable e) {
        System.err.println("Exception during serialization:" + e);
    } finally {
        InternalUtils.close(fos);
    }
}

From source file:com.fluidops.iwb.cms.fs.ArchiveFile.java

public ArchiveFile(File zip, String path) throws IOException {
    ByteArrayOutputStream buffer = GenUtil.readUrlToBuffer(zip.getInputStream());
    java.io.File tmp = java.io.File.createTempFile("tmp", "zip");
    FileOutputStream out = null;
    try {/* w  w w  .  j a va  2s  . c  o  m*/
        out = new FileOutputStream(tmp);
        out.write(buffer.toByteArray());
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
    this.path = path;
    this.zip = new ZipFile(tmp);
}

From source file:com.vigglet.oei.technician.UploadProfilePhoto.java

@Override
protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {//from ww w . ja va2  s . c  o m
        User user = getUser(req);

        for (Part part : req.getParts()) {
            byte[] b = IOUtils.toByteArray(part.getInputStream());
            String fileName = extractFileName(part);

            File file = new File(Content.FILE_LOCATION + "/" + fileName);
            FileOutputStream fos = new FileOutputStream(file);
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            IOUtils.copy(bais, fos);

            fos.flush();
            fos.close();
            bais.close();

            Content content = new Content();
            content.setCompany(user.getCompany());
            content.setUser(user.getId());
            content.setName(file.getName());
            content.setFilesize((int) file.length());
            content.setLocation(file.getAbsolutePath());

            ContentUtil.getInstance().insertOrUpdate(content);
        }
    } catch (ServletException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
}