Example usage for java.io File setLastModified

List of usage examples for java.io File setLastModified

Introduction

In this page you can find the example usage for java.io File setLastModified.

Prototype

public boolean setLastModified(long time) 

Source Link

Document

Sets the last-modified time of the file or directory named by this abstract pathname.

Usage

From source file:ch.entwine.weblounge.contentrepository.impl.endpoint.PreviewsEndpoint.java

/**
 * Returns the resource with the given identifier and styled using the
 * requested image style or a <code>404</code> if the resource or the resource
 * content could not be found./*ww  w . j a  va 2  s . c  o m*/
 * <p>
 * If the content is not available in the requested language, the original
 * language version is used.
 * 
 * @param request
 *          the request
 * @param resourceId
 *          the resource identifier
 * @param languageId
 *          the language identifier
 * @param styleId
 *          the image style identifier
 * @return the image
 */
@GET
@Path("/{resource}/locales/{language}/styles/{style}")
public Response getPreview(@Context HttpServletRequest request, @PathParam("resource") String resourceId,
        @PathParam("language") String languageId, @PathParam("style") String styleId,
        @QueryParam("version") @DefaultValue("0") long version,
        @QueryParam("force") @DefaultValue("false") boolean force) {

    // Check the parameters
    if (resourceId == null)
        throw new WebApplicationException(Status.BAD_REQUEST);

    // Get the resource
    final Site site = getSite(request);
    final Resource<?> resource = loadResource(request, resourceId, null, version);
    if (resource == null)
        throw new WebApplicationException(Status.NOT_FOUND);

    // Extract the language
    Language language = null;
    try {
        language = LanguageUtils.getLanguage(languageId);
        if (!resource.supportsLanguage(language)) {
            if (!resource.contents().isEmpty())
                language = resource.getOriginalContent().getLanguage();
            else if (resource.supportsLanguage(site.getDefaultLanguage()))
                language = site.getDefaultLanguage();
            else if (resource.languages().size() == 1)
                language = resource.languages().iterator().next();
            else
                throw new WebApplicationException(Status.NOT_FOUND);
        }
    } catch (UnknownLanguageException e) {
        throw new WebApplicationException(Status.BAD_REQUEST);
    }

    // Search the site for the image style
    ImageStyle style = null;
    for (Module m : site.getModules()) {
        style = m.getImageStyle(styleId);
        if (style != null) {
            break;
        }
    }

    // Search the global styles
    if (style == null) {
        for (ImageStyle s : styles) {
            if (s.getIdentifier().equals(styleId)) {
                style = s;
                break;
            }
        }
    }

    // The image style was not found
    if (style == null)
        throw new WebApplicationException(Status.BAD_REQUEST);

    // Load the input stream from the scaled image
    File scaledResourceFile = ImageStyleUtils.getScaledFile(resource, language, style);

    // Is there an up-to-date, cached version on the client side?
    if (!ResourceUtils.hasChanged(request, scaledResourceFile)) {
        return Response.notModified().build();
    }

    ResourceURI resourceURI = resource.getURI();
    final ContentRepository contentRepository = getContentRepository(site, false);

    // When there is no scaling required, just return the original
    if (ImageScalingMode.None.equals(style.getScalingMode())) {
        return getResourceContent(request, resource, language);
    }

    // Find a serializer
    ResourceSerializer<?, ?> serializer = serializerService.getSerializerByType(resourceURI.getType());
    if (serializer == null)
        throw new WebApplicationException(Status.PRECONDITION_FAILED);

    // Does the serializer come with a preview generator?
    PreviewGenerator previewGenerator = serializer.getPreviewGenerator(resource);
    if (previewGenerator == null)
        throw new WebApplicationException(Status.NOT_FOUND);

    // Load the resource contents from the repository
    InputStream resourceInputStream = null;
    long contentLength = -1;

    // Load the input stream from the scaled image
    InputStream contentRepositoryIs = null;
    FileOutputStream fos = null;
    try {
        long resourceLastModified = ResourceUtils.getModificationDate(resource, language).getTime();
        if (!scaledResourceFile.isFile() || scaledResourceFile.lastModified() < resourceLastModified) {
            if (!force)
                throw new WebApplicationException(Response.Status.NOT_FOUND);

            contentRepositoryIs = contentRepository.getContent(resourceURI, language);
            scaledResourceFile = ImageStyleUtils.createScaledFile(resource, language, style);
            scaledResourceFile.setLastModified(Math.max(new Date().getTime(), resourceLastModified));
            fos = new FileOutputStream(scaledResourceFile);
            logger.debug("Creating scaled image '{}' at {}", resource, scaledResourceFile);

            previewGenerator.createPreview(resource, environment, language, style, DEFAULT_PREVIEW_FORMAT,
                    contentRepositoryIs, fos);
            if (scaledResourceFile.length() == 0) {
                logger.debug("Error scaling '{}': file size is 0", resourceURI);
                IOUtils.closeQuietly(resourceInputStream);
                FileUtils.deleteQuietly(scaledResourceFile);
            }
        }

        // Did scaling work? If not, cleanup and tell the user
        if (scaledResourceFile.length() == 0)
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);

        // The scaled resource should now exist
        resourceInputStream = new FileInputStream(scaledResourceFile);
        contentLength = scaledResourceFile.length();

    } catch (WebApplicationException e) {
        IOUtils.closeQuietly(resourceInputStream);
        FileUtils.deleteQuietly(scaledResourceFile);
        if (scaledResourceFile != null)
            deleteIfEmpty(scaledResourceFile.getParentFile());
        throw e;
    } catch (ContentRepositoryException e) {
        logger.error("Error loading {} image '{}' from {}: {}",
                new Object[] { language, resource, contentRepository, e.getMessage() });
        logger.error(e.getMessage(), e);
        IOUtils.closeQuietly(resourceInputStream);
        FileUtils.deleteQuietly(scaledResourceFile);
        if (scaledResourceFile != null)
            deleteIfEmpty(scaledResourceFile.getParentFile());
        throw new WebApplicationException();
    } catch (IOException e) {
        logger.error("Error scaling image '{}': {}", resourceURI, e.getMessage());
        IOUtils.closeQuietly(resourceInputStream);
        FileUtils.deleteQuietly(scaledResourceFile);
        if (scaledResourceFile != null)
            deleteIfEmpty(scaledResourceFile.getParentFile());
        throw new WebApplicationException();
    } catch (IllegalArgumentException e) {
        logger.error("Image '{}' is of unsupported format: {}", resourceURI, e.getMessage());
        IOUtils.closeQuietly(resourceInputStream);
        FileUtils.deleteQuietly(scaledResourceFile);
        if (scaledResourceFile != null)
            deleteIfEmpty(scaledResourceFile.getParentFile());
        throw new WebApplicationException();
    } catch (Throwable t) {
        logger.error("Error scaling image '{}': {}", resourceURI, t.getMessage());
        IOUtils.closeQuietly(resourceInputStream);
        FileUtils.deleteQuietly(scaledResourceFile);
        if (scaledResourceFile != null)
            deleteIfEmpty(scaledResourceFile.getParentFile());
        throw new WebApplicationException();
    } finally {
        IOUtils.closeQuietly(contentRepositoryIs);
        IOUtils.closeQuietly(fos);
    }

    // Create the response
    final InputStream is = resourceInputStream;
    ResponseBuilder response = Response.ok(new StreamingOutput() {
        public void write(OutputStream os) throws IOException, WebApplicationException {
            try {
                IOUtils.copy(is, os);
                os.flush();
            } catch (IOException e) {
                if (!RequestUtils.isCausedByClient(e))
                    logger.error("Error writing preview to client", e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    });

    // Add mime type header
    String mimetype = previewGenerator.getContentType(resource, language, style);
    if (mimetype == null)
        mimetype = MediaType.APPLICATION_OCTET_STREAM;
    response.type(mimetype);

    // Add last modified header
    response.lastModified(new Date(scaledResourceFile.lastModified()));

    // Add ETag header
    String eTag = ResourceUtils.getETagValue(scaledResourceFile);
    response.tag(eTag);

    // Add filename header
    String filename = null;
    ResourceContent resourceContent = resource.getContent(language);
    if (resourceContent != null)
        filename = resourceContent.getFilename();
    if (StringUtils.isBlank(filename))
        filename = scaledResourceFile.getName();
    response.header("Content-Disposition", "inline; filename=" + filename);

    // Content length
    response.header("Content-Length", Long.toString(contentLength));

    // Send the response
    return response.build();
}

From source file:org.apache.cocoon.portlet.CocoonPortlet.java

private File extractLibraries() {
    try {// ww  w  .j a v  a2  s  .  c  om
        URL manifestURL = this.portletContext.getResource("/META-INF/MANIFEST.MF");
        if (manifestURL == null) {
            this.getLogger().fatalError("Unable to get Manifest");
            return null;
        }

        Manifest mf = new Manifest(manifestURL.openStream());
        Attributes attr = mf.getMainAttributes();
        String libValue = attr.getValue("Cocoon-Libs");
        if (libValue == null) {
            this.getLogger().fatalError("Unable to get 'Cocoon-Libs' attribute from the Manifest");
            return null;
        }

        List libList = new ArrayList();
        for (StringTokenizer st = new StringTokenizer(libValue, " "); st.hasMoreTokens();) {
            libList.add(st.nextToken());
        }

        File root = new File(this.workDir, "lib");
        root.mkdirs();

        File[] oldLibs = root.listFiles();
        for (int i = 0; i < oldLibs.length; i++) {
            String oldLib = oldLibs[i].getName();
            if (!libList.contains(oldLib)) {
                this.getLogger().debug("Removing old library " + oldLibs[i]);
                oldLibs[i].delete();
            }
        }

        this.getLogger().warn("Extracting libraries into " + root);
        byte[] buffer = new byte[65536];
        for (Iterator i = libList.iterator(); i.hasNext();) {
            String libName = (String) i.next();

            long lastModified = -1;
            try {
                lastModified = Long.parseLong(attr.getValue("Cocoon-Lib-" + libName.replace('.', '_')));
            } catch (Exception e) {
                this.getLogger().debug("Failed to parse lastModified: "
                        + attr.getValue("Cocoon-Lib-" + libName.replace('.', '_')));
            }

            File lib = new File(root, libName);
            if (lib.exists() && lib.lastModified() != lastModified) {
                this.getLogger().debug("Removing modified library " + lib);
                lib.delete();
            }

            InputStream is = this.portletContext.getResourceAsStream("/WEB-INF/lib/" + libName);
            if (is == null) {
                this.getLogger().warn("Skipping " + libName);
            } else {
                this.getLogger().debug("Extracting " + libName);
                OutputStream os = null;
                try {
                    os = new FileOutputStream(lib);
                    int count;
                    while ((count = is.read(buffer)) > 0) {
                        os.write(buffer, 0, count);
                    }
                } finally {
                    if (is != null)
                        is.close();
                    if (os != null)
                        os.close();
                }
            }

            if (lastModified != -1) {
                lib.setLastModified(lastModified);
            }
        }

        return root;
    } catch (IOException e) {
        this.getLogger().fatalError("Exception while processing Manifest file", e);
        return null;
    }
}

From source file:org.eclipse.che.api.builder.internal.SourcesManagerImpl.java

@Override
public void getSources(BuildLogger logger, String workspace, String project, final String sourcesUrl,
        java.io.File workDir) throws IOException {
    // Directory for sources. Keep sources to avoid download whole project before build.
    // This directory is not permanent and may be removed at any time.
    final java.io.File srcDir = new java.io.File(directory, workspace + java.io.File.separatorChar + project);
    // Temporary directory where we copy sources before build.
    final String key = workspace + project;
    try {/*  w w  w  .j  a v  a  2 s  . c o  m*/
        synchronized (this) {
            while (key.equals(projectKeyHolder.get())) {
                wait();
            }
        }
    } catch (InterruptedException e) {
        LOG.error(e.getMessage(), e);
        Thread.currentThread().interrupt();
    }
    // Avoid multiple threads download source of the same project.
    Future<Void> future = tasks.get(key);
    final ValueHolder<IOException> errorHolder = new ValueHolder<>();
    if (future == null) {
        final FutureTask<Void> newFuture = new FutureTask<>(new Runnable() {
            @Override
            public void run() {
                try {
                    download(sourcesUrl, srcDir);
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                    errorHolder.set(e);
                }
            }
        }, null);
        future = tasks.putIfAbsent(key, newFuture);
        if (future == null) {
            future = newFuture;
            try {
                // Need a bit time before to publish sources download start message via websocket
                // as client may not have already subscribed to the channel so early in build task execution
                Thread.sleep(300);
            } catch (InterruptedException e) {
                LOG.error(e.getMessage(), e);
            }
            logger.writeLine("[INFO] Injecting source code into builder...");
            newFuture.run();
            logger.writeLine("[INFO] Source code injection finished"
                    + "\n[INFO] ------------------------------------------------------------------------");
        }
    }
    try {
        future.get(); // Block thread until download is completed.
        final IOException ioError = errorHolder.get();
        if (ioError != null) {
            throw ioError;
        }
        IoUtil.copy(srcDir, workDir, IoUtil.ANY_FILTER);
        for (SourceManagerListener listener : listeners) {
            listener.afterDownload(new SourceManagerEvent(workspace, project, sourcesUrl, workDir));
        }
        if (!srcDir.setLastModified(System.currentTimeMillis())) {
            LOG.error("Unable update modification date of {} ", srcDir);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        // Runnable does not throw checked exceptions.
        final Throwable cause = e.getCause();
        if (cause instanceof Error) {
            throw (Error) cause;
        } else {
            throw (RuntimeException) cause;
        }
    } finally {
        tasks.remove(key);
    }
}

From source file:cc.arduino.utils.ArchiveExtractor.java

public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite)
        throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    ArchiveInputStream in = null;/* w  w  w .  ja va 2  s  .  c  o  m*/
    try {

        // Create an ArchiveInputStream with the correct archiving algorithm
        if (archiveFile.getName().endsWith("tar.bz2")) {
            in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("zip")) {
            in = new ZipArchiveInputStream(new FileInputStream(archiveFile));
        } else if (archiveFile.getName().endsWith("tar.gz")) {
            in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("tar")) {
            in = new TarArchiveInputStream(new FileInputStream(archiveFile));
        } else {
            throw new IOException("Archive format not supported.");
        }

        String pathPrefix = "";

        Map<File, File> hardLinks = new HashMap<>();
        Map<File, Integer> hardLinksMode = new HashMap<>();
        Map<File, String> symLinks = new HashMap<>();
        Map<File, Long> symLinksModifiedTimes = new HashMap<>();

        // Cycle through all the archive entries
        while (true) {
            ArchiveEntry entry = in.getNextEntry();
            if (entry == null) {
                break;
            }

            // Extract entry info
            long size = entry.getSize();
            String name = entry.getName();
            boolean isDirectory = entry.isDirectory();
            boolean isLink = false;
            boolean isSymLink = false;
            String linkName = null;
            Integer mode = null;
            long modifiedTime = entry.getLastModifiedDate().getTime();

            {
                // Skip MacOSX metadata
                // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
                int slash = name.lastIndexOf('/');
                if (slash == -1) {
                    if (name.startsWith("._")) {
                        continue;
                    }
                } else {
                    if (name.substring(slash + 1).startsWith("._")) {
                        continue;
                    }
                }
            }

            // Skip git metadata
            // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
            if (name.contains("pax_global_header")) {
                continue;
            }

            if (entry instanceof TarArchiveEntry) {
                TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                mode = tarEntry.getMode();
                isLink = tarEntry.isLink();
                isSymLink = tarEntry.isSymbolicLink();
                linkName = tarEntry.getLinkName();
            }

            // On the first archive entry, if requested, detect the common path
            // prefix to be stripped from filenames
            if (stripPath > 0 && pathPrefix.isEmpty()) {
                int slash = 0;
                while (stripPath > 0) {
                    slash = name.indexOf("/", slash);
                    if (slash == -1) {
                        throw new IOException("Invalid archive: it must contain a single root folder");
                    }
                    slash++;
                    stripPath--;
                }
                pathPrefix = name.substring(0, slash);
            }

            // Strip the common path prefix when requested
            if (!name.startsWith(pathPrefix)) {
                throw new IOException("Invalid archive: it must contain a single root folder while file " + name
                        + " is outside " + pathPrefix);
            }
            name = name.substring(pathPrefix.length());
            if (name.isEmpty()) {
                continue;
            }
            File outputFile = new File(destFolder, name);

            File outputLinkedFile = null;
            if (isLink) {
                if (!linkName.startsWith(pathPrefix)) {
                    throw new IOException("Invalid archive: it must contain a single root folder while file "
                            + linkName + " is outside " + pathPrefix);
                }
                linkName = linkName.substring(pathPrefix.length());
                outputLinkedFile = new File(destFolder, linkName);
            }
            if (isSymLink) {
                // Symbolic links are referenced with relative paths
                outputLinkedFile = new File(linkName);
                if (outputLinkedFile.isAbsolute()) {
                    System.err.println(I18n.format(tr("Warning: file {0} links to an absolute path {1}"),
                            outputFile, outputLinkedFile));
                    System.err.println();
                }
            }

            // Safety check
            if (isDirectory) {
                if (outputFile.isFile() && !overwrite) {
                    throw new IOException(
                            "Can't create folder " + outputFile + ", a file with the same name exists!");
                }
            } else {
                // - isLink
                // - isSymLink
                // - anything else
                if (outputFile.exists() && !overwrite) {
                    throw new IOException("Can't extract file " + outputFile + ", file already exists!");
                }
            }

            // Extract the entry
            if (isDirectory) {
                if (!outputFile.exists() && !outputFile.mkdirs()) {
                    throw new IOException("Could not create folder: " + outputFile);
                }
                foldersTimestamps.put(outputFile, modifiedTime);
            } else if (isLink) {
                hardLinks.put(outputFile, outputLinkedFile);
                hardLinksMode.put(outputFile, mode);
            } else if (isSymLink) {
                symLinks.put(outputFile, linkName);
                symLinksModifiedTimes.put(outputFile, modifiedTime);
            } else {
                // Create the containing folder if not exists
                if (!outputFile.getParentFile().isDirectory()) {
                    outputFile.getParentFile().mkdirs();
                }
                copyStreamToFile(in, size, outputFile);
                outputFile.setLastModified(modifiedTime);
            }

            // Set file/folder permission
            if (mode != null && !isSymLink && outputFile.exists()) {
                platform.chmod(outputFile, mode);
            }
        }

        for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.link(entry.getValue(), entry.getKey());
            Integer mode = hardLinksMode.get(entry.getKey());
            if (mode != null) {
                platform.chmod(entry.getKey(), mode);
            }
        }

        for (Map.Entry<File, String> entry : symLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.symlink(entry.getValue(), entry.getKey());
            entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()));
        }

    } finally {
        IOUtils.closeQuietly(in);
    }

    // Set folders timestamps
    for (File folder : foldersTimestamps.keySet()) {
        folder.setLastModified(foldersTimestamps.get(folder));
    }
}

From source file:org.sonatype.flexmojos.plugin.compiler.AbstractFlexCompilerMojo.java

@SuppressWarnings("unchecked")
public boolean isCompilationRequired() {
    if (!quick) {
        // not running at quick mode
        return true;
    }/*from   w w w .  jav  a  2s  .  com*/

    Artifact artifact;
    try {
        artifact = resolve(project.getGroupId(), project.getArtifactId(), project.getVersion(), getClassifier(),
                project.getPackaging());
    } catch (RuntimeMavenResolutionException e) {
        artifact = e.getArtifact();
    }

    if (!artifact.isResolved() || artifact.getFile() == null || !artifact.getFile().exists()) {
        // Recompile, file doesn't exists
        getLog().warn("Can't find any older installed version.");
        return true;
    }

    long lastCompiledArtifact = artifact.getFile().lastModified();

    boolean required = false;
    Set<Artifact> dependencies = getDependencies();
    for (Artifact dependency : dependencies) {
        if (org.apache.commons.io.FileUtils.isFileNewer(dependency.getFile(), lastCompiledArtifact)) {
            // a dependency is newer, recompile
            getLog().warn("Found a updated dependency: " + dependency);
            required = true;
        }
    }

    if (!required) {
        Collection<File> files = org.apache.commons.io.FileUtils.listFiles(
                new File(project.getBuild().getSourceDirectory()),
                new AgeFileFilter(lastCompiledArtifact, false), TrueFileFilter.INSTANCE);

        // If has any newer file
        if (files.size() > 0) {
            getLog().warn("Found some updated files.");
            required = true;
        }
    }

    if (!required) {
        try {
            final File output = new File(getOutput());

            FileUtils.copyFile(artifact.getFile(), output);

            if (!output.setLastModified(artifact.getFile().lastModified())) {
                getLog().warn("Could not set modified on copied artifact. Unnecessary rebuilds will occur.");
            }
        } catch (IOException e) {
            getLog().error("Unable to copy installed version to target folder.", e);
            return true;
        }
    }

    // nothing new was found.
    return required;
}

From source file:org.transitime.utils.HttpGetFile.java

/**
 * Actually gets and stores the file. The User-Agency property is always set
 * to USER_AGENT.//w w  w. java  2  s  . com
 * 
 * @return The http response code such as HttpStatus.SC_OK
 * @throws IOException
 */
public int getFile() throws IOException {
    IntervalTimer timer = new IntervalTimer();

    logger.debug("Getting URL={}", urlStr);
    URL url = new URL(urlStr);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agency", USER_AGENT);

    // Set request properties
    for (int i = 0; i < headerKeys.size(); ++i) {
        connection.setRequestProperty(headerKeys.get(i), headerValues.get(i));
    }

    // Get and log response code
    int responseCode = connection.getResponseCode();
    long expectedContentLength = connection.getContentLengthLong();
    long remoteFileLastModified = connection.getLastModified();
    logger.debug(
            "Response code for getting file {} is {} and file size "
                    + "is {} bytes and remote file lastModified=\"{}\" or {} msec",
            urlStr, responseCode, expectedContentLength, Time.httpDate(remoteFileLastModified),
            remoteFileLastModified);

    // Open file for where results are to be written
    File file = new File(fullFileNameForResult);

    // If file could not be read in or is not newer that lastModified time
    // of the existing file on the server then don't need to continue
    // reading it in.
    if (responseCode != HttpStatus.SC_OK) {
        logger.debug("Response code was {} so not reading in file", responseCode);
        return responseCode;
    }

    // Sometimes a web server will return http status OK (200) even
    // when the remote file is older than the time set for If-Modified-Since
    // header. For this situation still don't want to read in the file
    // so simply return http status NO_MODIFIED (304).
    if (file.lastModified() > 0 && remoteFileLastModified < file.lastModified()) {
        logger.warn("Response code was {} but the local file was modified "
                + "after the remote file so it must be up to date. " + "Therefore remote file not read in.",
                responseCode);
        return HttpStatus.SC_NOT_MODIFIED;
    }

    logger.debug(
            "Actually reading data from URL {} . Local file "
                    + "lastModified={} or {} msec and remoteFileLastModified={} " + "or {} msec.",
            urlStr, Time.httpDate(file.lastModified()), file.lastModified(),
            Time.httpDate(remoteFileLastModified), remoteFileLastModified);

    // Make sure output directory exists
    file.getParentFile().mkdirs();

    // Open input stream for reading data
    InputStream in = connection.getInputStream();

    // Open the stream
    FileOutputStream fos = new FileOutputStream(file);

    IntervalTimer loopTimer = new IntervalTimer();
    long lengthSinceLoggingMsg = 0;

    // Copy contents to file
    byte[] buffer = new byte[4096];
    int length;
    int totalLength = 0;
    while ((length = in.read(buffer)) > 0) {
        fos.write(buffer, 0, length);
        totalLength += length;
        lengthSinceLoggingMsg += length;

        // Every once in a while log progress. Don't want to
        // check timer every loop since that would be expensive.
        // So only check timer for every MB downloaded.
        if (lengthSinceLoggingMsg > 1024 * 1024) {
            lengthSinceLoggingMsg = 0;
            if (loopTimer.elapsedMsec() > 10 * Time.MS_PER_SEC) {
                loopTimer.resetTimer();
                logger.debug("Read in {} bytes or {}% of file {}", totalLength,
                        StringUtils.oneDigitFormat(100.0 * totalLength / expectedContentLength), urlStr);
            }
        }
    }

    // Close things up
    fos.close();

    // Set the last modified time so that it is the same as on the 
    // web server. 
    file.setLastModified(connection.getLastModified());

    if (totalLength == expectedContentLength)
        logger.debug("Successfully copied {} to file {}. Length was {} " + "bytes. Took {} msec.", urlStr,
                fullFileNameForResult, totalLength, timer.elapsedMsec());
    else
        logger.error("When copying {} to file {} the expected length was " + "{} but only copied {} bytes",
                urlStr, fullFileNameForResult, expectedContentLength, totalLength);

    // Return the http response code such as 200 for OK or 304 for 
    // Not Modified
    return connection.getResponseCode();
}

From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java

private void transferChunk(List<FileRecord> chunkList) throws IOException {
    boolean verbose = "true".equals(fs.getProperty("verbose"));

    try (CloseableHttpClient httpClient = HttpClientUtil.get()) {
        HttpPost post = new HttpPost(serverUrl + "get/" + fs.getRemote());

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        new BufferedFileListEncoder(new GZIPOutputStream(b)).writeAll(chunkList).close();

        post.setEntity(new ByteArrayEntity(b.toByteArray()));
        post.setHeader(HttpHeaders.CONTENT_TYPE, Protocol.FILELIST_MIME_TYPE);
        post.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
        addExtraHeaders(post);//w ww .j a  v a 2  s.  c om

        log.info("> " + post.getRequestLine());
        try (CloseableHttpResponse response = httpClient.execute(post)) {
            log.info("< " + response.getStatusLine());

            if (Shutdown.isHappening()) {
                return;
            }

            int status = response.getStatusLine().getStatusCode();
            if (status < 200 || status >= 300) {
                throw new IOException(String.format("Server returned \"%s\" for request \"%s\", body: %s",
                        response.getStatusLine(), post.getRequestLine(),
                        EntityUtils.toString(response.getEntity())));
            }

            try (MultiFileDecoder decoder = new MultiFileDecoder(response.getEntity().getContent())) {
                int i = 0;
                for (MultiFileHeader mfh : decoder) {
                    if (Shutdown.isHappening()) {
                        post.abort();
                        return;
                    }

                    log.trace(String.format("File #%3d: %8d bytes, %s, %s", ++i, mfh.getContentLength(),
                            mfh.getContentType(), mfh.getFilename()));
                    File local;
                    if (mfh.getFilename().equals(".")) {
                        if (mfh.isDirectory()) {
                            // skip root directory
                            continue;
                        } else {
                            // single file sync, write to local file
                            local = new File(fs.getLocal());
                        }
                    } else {
                        local = new File(fs.getLocal() + File.separator + mfh.getFilename());
                        // Detect if server tries to overwrite file in parent of local path
                        if (!local.getCanonicalPath().startsWith(localCanonicalPath)) {
                            throw new IOException("Server returned invalid filename: " + mfh.getFilename());
                        }
                    }

                    if (mfh.isDirectory()) {
                        if (local.exists() && local.isDirectory()) {
                            continue;
                        }
                        if (verbose) {
                            log.info("mkdir     " + mfh.getFilename());
                        }
                        local.mkdirs();
                        directoriesLastModifiedTimes.add(Pair.of(local, mfh.getLastModified().getTime()));
                        continue;
                    }

                    if (local.exists()) {
                        if (verbose) {
                            log.info("overwrite " + mfh.getFilename());
                        }
                    } else {
                        if (verbose) {
                            log.info("write     " + mfh.getFilename());
                        }
                        local.getParentFile().mkdirs();
                    }
                    try (FileOutputStream out = new FileOutputStream(local)) {
                        IOUtils.copy(mfh.getBody(), out);
                        totalBytes += mfh.getContentLength();
                        filesUpdated = true;
                    } catch (IOException e) {
                        log.error(String.format("Error writing to local file \"%s\": %s", fs.getLocal(),
                                ExceptionUtils.getMessage(e)));
                        throw e;
                    }
                    local.setLastModified(mfh.getLastModified().getTime());
                }
                if (decoder.getIOException() != null) {
                    throw decoder.getIOException();
                }
            }
        }
    }
}

From source file:com.example.util.FileUtils.java

/**
 * Internal copy file method./*from  ww w . j av  a 2 s.com*/
 * 
 * @param srcFile  the validated source file, must not be {@code null}
 * @param destFile  the validated destination file, must not be {@code null}
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
            pos += output.transferFrom(input, pos, count);
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}

From source file:info.ajaxplorer.synchro.SyncJob.java

protected Map<String, Object[]> applyChanges(Map<String, Object[]> changes) throws Exception {
    Iterator<Map.Entry<String, Object[]>> it = changes.entrySet().iterator();
    Map<String, Object[]> notApplied = new TreeMap<String, Object[]>();
    // Make sure to apply those one at the end
    Map<String, Object[]> moves = new TreeMap<String, Object[]>();
    Map<String, Object[]> deletes = new TreeMap<String, Object[]>();
    RestRequest rest = new RestRequest();
    while (it.hasNext()) {
        Map.Entry<String, Object[]> entry = it.next();
        String k = entry.getKey();
        Object[] value = entry.getValue().clone();
        Integer v = (Integer) value[0];
        Node n = (Node) value[1];
        if (n == null)
            continue;
        if (this.interruptRequired) {
            value[2] = STATUS_INTERRUPTED;
            notApplied.put(k, value);/* ww w .  j  a  v  a  2 s  . c o m*/
            continue;
        }
        //Thread.sleep(2000);
        try {
            if (n.isLeaf() && value[2].equals(STATUS_CONFLICT_SOLVED)) {
                if (v.equals(TASK_SOLVE_KEEP_MINE)) {
                    v = TASK_REMOTE_PUT_CONTENT;
                } else if (v.equals(TASK_SOLVE_KEEP_THEIR)) {
                    v = TASK_LOCAL_GET_CONTENT;
                } else if (v.equals(TASK_SOLVE_KEEP_BOTH)) {
                    // COPY LOCAL FILE AND GET REMOTE COPY
                    File origFile = new File(currentLocalFolder, k);
                    File targetFile = new File(currentLocalFolder, k + ".mine");
                    InputStream in = new FileInputStream(origFile);
                    OutputStream out = new FileOutputStream(targetFile);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                    v = TASK_LOCAL_GET_CONTENT;
                }
            }

            if (v == TASK_LOCAL_GET_CONTENT) {

                Node node = new Node(Node.NODE_TYPE_ENTRY, "", null);
                node.setPath(k);
                File targetFile = new File(currentLocalFolder, k);
                this.logChange(Manager.getMessage("job_log_downloading"), k);
                try {
                    this.updateNode(node, targetFile, n);
                } catch (IllegalStateException e) {
                    if (this.statRemoteFile(node, "file", rest) == null)
                        continue;
                    else
                        throw e;
                }
                if (!targetFile.exists()
                        || targetFile.length() != Integer.parseInt(n.getPropertyValue("bytesize"))) {
                    throw new Exception("Error while downloading file from server");
                }
                if (n != null) {
                    targetFile.setLastModified(n.getLastModified().getTime());
                }
                countFilesDownloaded++;

            } else if (v == TASK_LOCAL_MKDIR) {

                File f = new File(currentLocalFolder, k);
                if (!f.exists()) {
                    this.logChange(Manager.getMessage("job_log_mkdir"), k);
                    boolean res = f.mkdirs();
                    if (!res) {
                        throw new Exception("Error while creating local folder");
                    }
                    countResourcesSynchronized++;
                }

            } else if (v == TASK_LOCAL_REMOVE) {

                deletes.put(k, value);

            } else if (v == TASK_REMOTE_REMOVE) {

                deletes.put(k, value);

            } else if (v == TASK_REMOTE_MKDIR) {

                this.logChange(Manager.getMessage("job_log_mkdir_remote"), k);
                Node currentDirectory = new Node(Node.NODE_TYPE_ENTRY, "", null);
                int lastSlash = k.lastIndexOf("/");
                currentDirectory.setPath(k.substring(0, lastSlash));
                RestStateHolder.getInstance().setDirectory(currentDirectory);
                rest.getStatusCodeForRequest(AjxpAPI.getInstance().getMkdirUri(k.substring(lastSlash + 1)));
                JSONObject object = rest.getJSonContent(AjxpAPI.getInstance().getStatUri(k));
                if (!object.has("mtime")) {
                    throw new Exception("Could not create remote folder");
                }
                countResourcesSynchronized++;

            } else if (v == TASK_REMOTE_PUT_CONTENT) {

                this.logChange(Manager.getMessage("job_log_uploading"), k);
                Node currentDirectory = new Node(Node.NODE_TYPE_ENTRY, "", null);
                int lastSlash = k.lastIndexOf("/");
                currentDirectory.setPath(k.substring(0, lastSlash));
                RestStateHolder.getInstance().setDirectory(currentDirectory);
                File sourceFile = new File(currentLocalFolder, k);
                if (!sourceFile.exists()) {
                    // Silently ignore, or it will continously try to reupload it.
                    continue;
                }
                boolean checked = false;
                if (sourceFile.length() == 0) {
                    rest.getStringContent(AjxpAPI.getInstance().getMkfileUri(sourceFile.getName()));
                } else {
                    checked = this.synchronousUP(currentDirectory, sourceFile, n);
                }
                if (!checked) {
                    JSONObject object = rest.getJSonContent(AjxpAPI.getInstance().getStatUri(n.getPath(true)));
                    if (!object.has("size")
                            || object.getInt("size") != Integer.parseInt(n.getPropertyValue("bytesize"))) {
                        throw new Exception("Could not upload file to the server");
                    }
                }
                countFilesUploaded++;

            } else if (v == TASK_DO_NOTHING && value[2] == STATUS_CONFLICT) {

                // Recheck that it's a real conflict?

                this.logChange(Manager.getMessage("job_log_conflict"), k);
                notApplied.put(k, value);
                countConflictsDetected++;

            } else if (v == TASK_LOCAL_MOVE_FILE || v == TASK_REMOTE_MOVE_FILE) {
                moves.put(k, value);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            countResourcesErrors++;
            // Do not put in the notApplied again, otherwise it will indefinitely happen.
        } catch (Exception e) {
            Logger.getRootLogger().error("Synchro", e);
            countResourcesErrors++;
            value[2] = STATUS_ERROR;
            notApplied.put(k, value);
        }
    }

    // APPLY MOVES
    Iterator<Map.Entry<String, Object[]>> mIt = moves.entrySet().iterator();
    while (mIt.hasNext()) {
        Map.Entry<String, Object[]> entry = mIt.next();
        String k = entry.getKey();
        Object[] value = entry.getValue().clone();
        Integer v = (Integer) value[0];
        Node n = (Node) value[1];
        if (this.interruptRequired) {
            value[2] = STATUS_INTERRUPTED;
            notApplied.put(k, value);
            continue;
        }
        try {
            if (v == TASK_LOCAL_MOVE_FILE && value.length == 4) {

                this.logChange("Moving resource locally", k);
                Node dest = (Node) value[3];
                File origFile = new File(currentLocalFolder, n.getPath());
                if (!origFile.exists()) {
                    // Cannot move a non-existing file! Download instead!
                    value[0] = TASK_LOCAL_GET_CONTENT;
                    value[1] = dest;
                    value[2] = STATUS_TODO;
                    notApplied.put(dest.getPath(true), value);
                    continue;
                }
                File destFile = new File(currentLocalFolder, dest.getPath());
                origFile.renameTo(destFile);
                if (!destFile.exists()) {
                    throw new Exception("Error while creating " + dest.getPath());
                }
                countResourcesSynchronized++;

            } else if (v == TASK_REMOTE_MOVE_FILE && value.length == 4) {

                this.logChange("Moving resource remotely", k);
                Node dest = (Node) value[3];
                JSONObject object = rest.getJSonContent(AjxpAPI.getInstance().getStatUri(n.getPath()));
                if (!object.has("size")) {
                    value[0] = TASK_REMOTE_PUT_CONTENT;
                    value[1] = dest;
                    value[2] = STATUS_TODO;
                    notApplied.put(dest.getPath(true), value);
                    continue;
                }
                rest.getStatusCodeForRequest(AjxpAPI.getInstance().getRenameUri(n, dest));
                object = rest.getJSonContent(AjxpAPI.getInstance().getStatUri(dest.getPath()));
                if (!object.has("size")) {
                    throw new Exception("Could not move remote file to " + dest.getPath());
                }
                countResourcesSynchronized++;

            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            countResourcesErrors++;
            // Do not put in the notApplied again, otherwise it will indefinitely happen.
        } catch (Exception e) {
            Logger.getRootLogger().error("Synchro", e);
            countResourcesErrors++;
            value[2] = STATUS_ERROR;
            notApplied.put(k, value);
        }
    }

    // APPLY DELETES
    Iterator<Map.Entry<String, Object[]>> dIt = deletes.entrySet().iterator();
    while (dIt.hasNext()) {
        Map.Entry<String, Object[]> entry = dIt.next();
        String k = entry.getKey();
        Object[] value = entry.getValue().clone();
        Integer v = (Integer) value[0];
        //Node n = (Node)value[1];
        if (this.interruptRequired) {
            value[2] = STATUS_INTERRUPTED;
            notApplied.put(k, value);
            continue;
        }
        try {

            if (v == TASK_LOCAL_REMOVE) {

                this.logChange(Manager.getMessage("job_log_rmlocal"), k);
                File f = new File(currentLocalFolder, k);
                if (f.exists()) {
                    boolean res = f.delete();
                    if (!res) {
                        throw new Exception("Error while removing local resource");
                    }
                    countResourcesSynchronized++;
                }

            } else if (v == TASK_REMOTE_REMOVE) {

                this.logChange(Manager.getMessage("job_log_rmremote"), k);
                Node currentDirectory = new Node(Node.NODE_TYPE_ENTRY, "", null);
                int lastSlash = k.lastIndexOf("/");
                currentDirectory.setPath(k.substring(0, lastSlash));
                RestStateHolder.getInstance().setDirectory(currentDirectory);
                rest.getStatusCodeForRequest(AjxpAPI.getInstance().getDeleteUri(k));
                JSONObject object = rest.getJSonContent(AjxpAPI.getInstance().getStatUri(k));
                if (object.has("mtime")) { // Still exists, should be empty!
                    throw new Exception("Could not remove the resource from the server");
                }
                countResourcesSynchronized++;
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            countResourcesErrors++;
            // Do not put in the notApplied again, otherwise it will indefinitely happen.
        } catch (Exception e) {
            Logger.getRootLogger().error("Synchro", e);
            countResourcesErrors++;
            value[2] = STATUS_ERROR;
            notApplied.put(k, value);
        }
    }
    rest.release();
    return notApplied;
}

From source file:com.httrack.android.HTTrackActivity.java

/**
 * Get the resource directory. Create it if necessary. Resources are created
 * in the dedicated cache, so that the files can be uninstalled upon
 * application removal.//from   ww  w .ja va2s  .c o  m
 **/
private File buildResourceFile() {
    final File cache = android.os.Build.VERSION.SDK_INT >= VERSION_CODES.FROYO ? getExternalCacheDir()
            : getCacheDir();
    final File rscPath = new File(cache, "resources");
    final File stampFile = new File(rscPath, "resources.stamp");
    final long stamp = installOrUpdateTime();

    // Check timestamp of resources. If the applicate has been updated,
    // recreated cached resources.
    if (rscPath.exists()) {
        long diskStamp = 0;
        try {
            if (stampFile.exists()) {
                final FileReader reader = new FileReader(stampFile);
                final BufferedReader lreader = new BufferedReader(reader);
                try {
                    diskStamp = Long.parseLong(lreader.readLine());
                } catch (final NumberFormatException nfe) {
                    diskStamp = 0;
                }
                lreader.close();
                reader.close();
            }
        } catch (final IOException io) {
            diskStamp = 0;
        }
        // Different one: wipe and recreate
        if (stamp != diskStamp) {
            Log.i(getClass().getSimpleName(), "deleting old resources " + rscPath.getAbsolutePath()
                    + " (app_stamp=" + stamp + " != disk_stamp=" + diskStamp + ")");
            CleanupActivity.deleteRecursively(rscPath);
        } else {
            Log.i(getClass().getSimpleName(),
                    "keeping resources " + rscPath.getAbsolutePath() + " (app_stamp=disk_stamp=" + stamp + ")");
        }
    }

    // Recreate resources ?
    if (!rscPath.exists()) {
        Log.i(getClass().getSimpleName(),
                "creating resources " + rscPath.getAbsolutePath() + " with stamp " + stamp);
        if (HTTrackActivity.mkdirs(rscPath)) {
            long totalSize = 0;
            int totalFiles = 0;
            try {
                final InputStream zipStream = getResources().openRawResource(R.raw.resources);
                final ZipInputStream file = new ZipInputStream(zipStream);
                ZipEntry entry;
                while ((entry = file.getNextEntry()) != null) {
                    final File dest = new File(rscPath.getAbsoluteFile() + "/" + entry.getName());
                    if (entry.getName().endsWith("/")) {
                        dest.mkdirs();
                    } else {
                        final FileOutputStream writer = new FileOutputStream(dest);
                        final byte[] bytes = new byte[1024];
                        int length;
                        while ((length = file.read(bytes)) >= 0) {
                            writer.write(bytes, 0, length);
                            totalSize += length;
                        }
                        writer.close();
                        totalFiles++;
                        dest.setLastModified(entry.getTime());
                    }
                }
                file.close();
                zipStream.close();
                Log.i(getClass().getSimpleName(), "created resources " + rscPath.getAbsolutePath() + " ("
                        + totalFiles + " files, " + totalSize + " bytes)");

                // Write stamp
                final FileWriter writer = new FileWriter(stampFile);
                final BufferedWriter lwriter = new BufferedWriter(writer);
                lwriter.write(Long.toString(stamp));
                lwriter.close();
                writer.close();

                // Little info
                showNotification(getString(R.string.info_recreated_resources));
            } catch (final IOException io) {
                Log.w(getClass().getSimpleName(), "could not create resources", io);
                CleanupActivity.deleteRecursively(rscPath);
                showNotification(getString(R.string.info_could_not_create_resources), true);
            }
        }
    }

    // Return resources path
    return rscPath;
}