john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

slideshow oxygen hackathon spacecontains

// 2012-06-07 johnpfeiffer

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2File;
import com.oxygen.sdk.O2FolderItem;
import com.oxygen.sdk.O2Space;

public class SpaceContains
{
    List <O2Space> spaceList = null;
    List <O2FolderItem> listing = null;

    ArrayList <OxygenFile> fileListing = null;

    ArrayList <String> fileIds = null;
    ArrayList <O2File> remoteFiles = null;
    ArrayList <URL> downloadUrls = null;

    SpaceContains( O2Agent agent , String targetSpaceName ) throws Exception
    {
        if( agent == null || targetSpaceName == null )
        {
            throw new IllegalArgumentException( "Oxygen agent or targetSpaceName cannot be null" );
        }
        fileListing = new ArrayList <OxygenFile>();
        spaceList = agent.listSpaces();
        for( O2Space currentSpace : spaceList )
        {
            if( targetSpaceName.equals( currentSpace.getName() ) )
            {
                listing = agent.listContainerItems( currentSpace );
                for( O2FolderItem currentFolderItem : listing )
                {
                    O2File currentFile = (O2File) currentFolderItem;
                    String fileName = currentFile.getName();
                    if( fileName.contains( ".jpg" ) )
                    {
                        String fileId = currentFile.getID();
                        URL currentDownloadUrl = new URL( agent.getDownloadURL( currentFile ) );
                        System.out.println( "DEBUG: " + fileName + " , " + fileId + " , " + currentDownloadUrl );
                        OxygenFile temp = new OxygenFile( fileName , fileId , currentDownloadUrl );
                        fileListing.add( temp );
                    }
                }

            }
        }
        agent.logout();

    }

    protected Boolean download( OxygenFile target , String destination ) throws Exception
    {
        int chunkSize = 16384;
        long bytesRead = 0;
        // long totalBytesRead = 0;
        byte[] buffer = new byte[chunkSize]; // Downloading in chunks of 16K

        if( target == null )
        {
            throw new IllegalArgumentException( "Oxygen target file cannot be null" );
        }

        String saveFilePathName = destination + target.getName();
        System.out.println( "DEBUG: saving to " + saveFilePathName );

        target.getUrl().openConnection();
        InputStream reader = target.getUrl().openStream();
        FileOutputStream writer = new FileOutputStream( saveFilePathName );

        try
        {
            while( ( bytesRead = reader.read( buffer ) ) > 0 )
            {
                writer.write( buffer , 0 , (int) bytesRead );
                buffer = new byte[chunkSize];
                // totalBytesRead += bytesRead;
                System.out.print( "." );
            }

        }finally
        {
            writer.flush();
            writer.close();
            reader.close();
        }

        return true;
    }

    protected ArrayList <OxygenFile> getFileListing()
    {
        return fileListing;
    }

} // end class

  • « slideshow oxygen hackathon imageviewer
  • slideshow oxygen hackathon oxygenlogin »

Published

Jun 18, 2012

Category

java-oxygencloud

~212 words

Tags

  • hackathon 5
  • java 252
  • oxygen 14
  • oxygencloud 19
  • slideshow 8
  • spacecontains 1