package com.parc.ccn.apps;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.stream.XMLStreamException;

import com.parc.ccn.config.ConfigurationException;
import com.parc.ccn.data.ContentName;
import com.parc.ccn.data.MalformedContentNameStringException;
import com.parc.ccn.library.CCNLibrary;
import com.parc.ccn.library.io.CCNDescriptor;

public class get_file {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 2) {
			usage();
			return;
		}
		
		try {
			int readsize = 1024; // make an argument for testing...
			// If we get one file name, put as the specific name given.
			// If we get more than one, put underneath the first as parent.
			// Ideally want to use newVersion to get latest version. Start
			// with random version.
			ContentName argName = ContentName.fromURI(args[0]);
			
			CCNLibrary library = CCNLibrary.open();

			File theFile = new File(args[1]);
			if (theFile.exists()) {
				System.out.println("Overwriting file: " + args[1]);
			}
			FileOutputStream output = new FileOutputStream(theFile);
			
			CCNDescriptor input = new CCNDescriptor(argName, null, library);
			if (args.length > 2) {
				input.setTimeout(new Integer(args[2]).intValue()); 
			}
			byte [] buffer = new byte[readsize];
			
			int readcount = 0;
			int readtotal = 0;
			while (!input.eof()) {
				readcount = input.read(buffer);
				readtotal += readcount;
				output.write(buffer, 0, readcount);
				output.flush();
			}

			System.out.println("Retrieved content " + args[1] + " got " + readtotal + " bytes.");
			System.exit(0);

		} catch (ConfigurationException e) {
			System.out.println("Configuration exception in get_file: " + e.getMessage());
			e.printStackTrace();
		} catch (MalformedContentNameStringException e) {
			System.out.println("Malformed name: " + args[0] + " " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Cannot write file or read content. " + e.getMessage());
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void usage() {
		System.out.println("usage: get_file <ccnname> <filename> [<timeoutms>]");
	}

}

@@ -1113,24 +1113,72 @@
 eFile);%0A%09%09%09%0A
+%09%09%09long starttime = System.currentTimeMillis();%0A
 %09%09%09CCNDescri
@@ -1233,16 +1233,16 @@
 brary);%0A
-
 %09%09%09if (a
@@ -1411,16 +1411,18 @@
 = 0;%0A%09%09%09
+//
 while (!
@@ -1435,25 +1435,78 @@
 eof()) %7B%0A%09%09%09
-%09
+while ((readcount = input.read(buffer)) != -1)%7B%0A%09%09%09%09//
 readcount = 
@@ -1618,16 +1618,103 @@
 );%0A%09%09%09%7D%0A
+%09%09%09System.out.println(%22get_file took: %22+(System.currentTimeMillis() - starttime)+%22ms%22);
 %0A%09%09%09Syst

add check for eof at loop instead of just writing what was read. threw an exception at the end of the file when the read returned -1