Android Open Source - android-gskbyte-utils Disk Download






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.gskbyte.download;
/*from ww  w. jav a 2 s.com*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Random;

import org.gskbyte.util.IOUtils;

import android.content.Context;

public class DiskDownload
extends Download
{
private FileOutputStream tempOutputStream;
private String tempFilePath;

private final Context context;
private final int localFileLocation;
private final String localFileName;

public static class Request extends Download.Request
{
    private static final long serialVersionUID = 4480930156991551348L;
    
    private final Context context;
    private final int localFileLocation;
    private final String localFileName;

    public Request(URL remoteUrl, Context context, int location, String filePath)
    {
        super(remoteUrl);
        this.context = context;
        this.localFileLocation = location;
        this.localFileName = filePath;
    }
    
    public Request(URL remoteUrl, Context context, int location, String filePath, int tag)
    {
        super(remoteUrl, tag);
        this.context = context;
        this.localFileLocation = location;
        this.localFileName = filePath;
    }
    
    public Request(Request requestToClone)
    {
        super(requestToClone);
        this.context = requestToClone.context;
        this.localFileLocation = requestToClone.localFileLocation;
        this.localFileName = new String(requestToClone.localFileName);
    }

    public Context getContext()
    {return context; }
    
    public int getLocalFileLocation()
    { return localFileLocation; }
    
    public String getLocalFileName()
    { return localFileName;}
    
    @Override
    public boolean savesToDisk()
    { return true; }
}

public DiskDownload(Request request)
{
    super(request);
    this.context = request.context;
    this.localFileLocation = request.localFileLocation;
    this.localFileName = request.localFileName;
}

public DiskDownload(URL remoteURL, Context context, int fileLocation, String localFilePath)
{
    super(remoteURL);
    this.context = context;
    this.localFileLocation = fileLocation;
    this.localFileName = localFilePath;
}

public Context getContext()
{ return context; }

public int getLocalFileLocation()
{ return localFileLocation; }

public String getLocalFileName()
{ return localFileName;}

@Override
public final boolean savesToDisk()
{ return true; }

@Override
protected void resetTemporalStuff()
{
    super.resetTemporalStuff();
    byteArray = null;
    tempOutputStream = null;
}

@Override
public synchronized boolean stop()
{
    if( super.stop() && tempFilePath != null) {
        IOUtils.DeleteFile(localFileLocation, tempFilePath, context);
        tempFilePath = null;
        return true;
    } else {
        return false;
    }
}

@Override
public InputStream getInputStream() throws IOException 
{
    return IOUtils.GetInputStream(localFileLocation, localFileName, context);
}
@Override
protected DownloadTask getDownloadTask()
{
    return new DiskDownload.DownloadTask();
}

public long getDataLength()
{
    File f = IOUtils.GetFile(localFileLocation, localFileName, context);
    return f.length();
}

@Override
public boolean isCorrect()
{ return getDataLength()>0;}

protected class DownloadTask extends Download.DownloadTask
{
    @Override
    protected void readFromStream() throws IOException
    {
        final Random r = new Random(System.currentTimeMillis());
        tempFilePath = localFileName + ".dl_" + r.nextInt(1000000);
        
        tempOutputStream = IOUtils.GetFileOutputStream(localFileLocation, tempFilePath, context);
        
        byte [] tempArray = new byte[REMOTE_READ_BYTES];
        int readBytes = connectionStream.read(tempArray);
        
        while(readBytes>0 && state == State.Running) {
            buffer.append(tempArray, 0, readBytes);
            downloadedSize += readBytes;
            
            readBytes = connectionStream.read(tempArray);
            
            if(totalSize > 0)
                rate = (float)downloadedSize / (float) totalSize;
            
            if(downloadedSize-lastNotificationSize > notificationSizeDif || downloadedSize == totalSize) {
                lastNotificationSize = downloadedSize;
                publishProgress(rate);
                //Logger.error(getClass(), "------> "+readBytes);
            }
            
            if(buffer.length() > DEFAULT_BUFFER_SIZE) {
                tempOutputStream.write(buffer.buffer(), 0, buffer.length());
                buffer.clear();
                tempOutputStream.flush();
            }
        }
        
        tempOutputStream.write(buffer.buffer(), 0, buffer.length());
        tempOutputStream.flush();
        tempOutputStream.close();

        // Release memory!!!
        buffer.clear();
        buffer = null;        
        byteArray = null;
        
        IOUtils.MoveFile(localFileLocation, tempFilePath, localFileName, context);
        tempFilePath = null;
    }

}
}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java