All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.util.DownloadableImpl Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: DownloadableImpl.java,v 523e997730e9 2010/06/26 16:21:59 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.util;

import java.net.MalformedURLException;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.FileSystem;
import it.tidalwave.util.Removable;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class DownloadableImpl extends Downloadable implements Removable
  {
    private static final String CLASS = DownloadableImpl.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    public final URL url; // FIXME

    /* package */ File cachedFile;
    
    /* package */ final Provider fileSystem = Locator.createProviderFor(FileSystem.class);

    /* package */ int contentLength = 0;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DownloadableImpl (final @Nonnull URL url)
      throws MalformedURLException
      {
        this.url = new URL(url.toExternalForm().replaceAll(" ", "%20"));
        computeCachedFile();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void download()
      {
        download(false);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void refresh()
      {
        download(true);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void remove() 
      throws IOException
      {
        logger.info("Deleting %s", cachedFile);

        if (!cachedFile.delete())
          {
            throw new IOException("Not deleted: " + cachedFile);
          }

        setStatus(Status.NOT_DOWNLOADED);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public File getFile()
      {
        return cachedFile;
      }

    /*******************************************************************************************************************
     *
     * Can be overridden for testing (URLs are not mockable).
     *
     ******************************************************************************************************************/
    @Nonnull
    protected InputStream createInputStream()
      throws IOException
      {
        final URLConnection connection = url.openConnection();
        connection.connect();
        contentLength = connection.getContentLength(); // TODO: handle the case in which it's unknown size
        return connection.getInputStream();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    private synchronized void download (final boolean always)
      {
        logger.info("download() - %s", url);
        computeCachedFile();
        logger.info(">>>> will save to %s", cachedFile);

        if (always || (status == Status.NOT_DOWNLOADED))
          {
            setStatus(Status.DOWNLOADING);
            new Thread() // TODO: use a pool, with QUEUED state
              {
                @Override
                public void run()
                  {
                    try
                      {
                        load();
                      }
                    catch (Exception e)
                      {
                        setStatus(Status.BROKEN);
                        logger.severe("While loading %s: %s", url, e);
                        logger.throwing("download()", CLASS, e);
                      }
                  }
              }.start();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void load()
      throws IOException
      {
        final InputStream is = createInputStream();
        final byte[] buffer = new byte[64 * 1024];
        cachedFile.getParentFile().mkdirs();
        final OutputStream os = new FileOutputStream(cachedFile);
//        final OutputStream os = fileSystem.get().openFileOutput(getCachedFile());
        // FIXME: first download in cache, at the end copy to the real resource.
        int loaded = 0;

        for (;;)
          {
            final int n = is.read(buffer);

            if (n < 0)
              {
                break;
              }

            os.write(buffer, 0, n);
            loaded += n;

            if (contentLength > 0)
              {
                setDownloadProgress((1.0f * loaded) / contentLength);
                logger.finest("Loaded %d bytes, so far: %d bytes, progress: %f", n, loaded, downloadProgress);
              }
          }

        setStatus(Status.DOWNLOADED);
        is.close();
        os.close();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    /* package */ static String normalized (final @Nonnull URL url)
      {
        return url.toExternalForm().replaceAll("://", "/").replaceAll("[:;#$?&=]", "_");
//        return url.toExternalForm().replaceAll("[:/;#@\\$\\?\\&]", "_").replaceAll("_*", "_");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void computeCachedFile()
      {
        Status newStatus = null;

        synchronized (this)
          {
            if (cachedFile == null)
              {
                try
                  {
                    cachedFile = fileSystem.get().getFile("/Media/" + normalized(url));

                    if (cachedFile.exists())
                      {
                        newStatus = Status.DOWNLOADED;
                      }
                    else
                      {
                        newStatus = Status.NOT_DOWNLOADED;
                      }
                  }
                catch (IOException e)
                  {
                    newStatus = Status.BROKEN;
                    logger.severe("Cannot map to external storage: %s", e);
                    logger.throwing("", CLASS, e);
                  }
              }
          }

        if (newStatus != null)
          {
            setStatus(newStatus);
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy