package com.parc.ccn.apps;

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

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

public class put_file {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 2) {
			usage();
			return;
		}
		
		try {
			// 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();
			
			if (args.length == 2) {
				
				File theFile = new File(args[1]);
				if (!theFile.exists()) {
					System.out.println("No such file: " + args[1]);
					usage();
					return;
				}
				byte [] contents = Utils.getBytesFromFile(theFile);
				
				// put as name
				// int version = new Random().nextInt(1000);
				// would be version = library.latestVersion(argName) + 1;
				ContentObject result = library.newVersion(argName, contents);
				
				System.out.println("Inserted file " + args[1] + " as " + result.name() + " byte count " + contents.length + ".");
				System.exit(0);
			} else {
				for (int i=1; i < args.length; ++i) {
					
					File theFile = new File(args[i]);
					if (!theFile.exists()) {
						System.out.println("No such file: " + args[i]);
						usage();
						return;
					}
					byte [] contents = Utils.getBytesFromFile(theFile);
					
					FileOutputStream testOut = new FileOutputStream("put_file" + i + ".dat");
					testOut.write(contents);
					testOut.flush();
					testOut.close();
					
					// put as child of name
					ContentName nodeName = ContentName.fromURI(argName, theFile.getName());
					
					// int version = new Random().nextInt(1000);
					// would be version = library.latestVersion(argName) + 1;
					ContentObject result = library.newVersion(nodeName, contents);
					
					System.out.println("Inserted file " + args[i] + " as " + result.name() + " byte count " + contents.length + ".");
					
				}
			}
		} catch (ConfigurationException e) {
			System.out.println("Configuration exception in put: " + 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 read file. " + e.getMessage());
			e.printStackTrace();
		} catch (SignatureException e) {
			System.out.println("Cannnot insert content. " + e.getMessage());
			e.printStackTrace();
		}

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

}

@@ -42,16 +42,48 @@
 o.File;%0A
+import java.io.FileInputStream;%0A
 import j
@@ -177,16 +177,61 @@
 ption;%0A%0A
+import javax.xml.stream.XMLStreamException;%0A%0A
 import c
@@ -457,32 +457,119 @@
 ry;%0A
-%0Apublic class put_file %7B
+import com.parc.ccn.library.io.CCNDescriptor;%0A%0Apublic class put_file %7B%0A%09%0A%09private static int BLOCK_SIZE = 8096;
 %0A%0A%09/
@@ -1219,375 +1219,504 @@
 %09%09%09%09
-byte %5B%5D contents = Utils.getBytesFromFile(theFile);%0A%09%09%09%09%0A%09%09%09%09// put as name%0A%09%09%09%09// int version = new Random().nextInt(1000);%0A%09%09%09%09// would be version = library.latestVersion(argName) + 1;%0A%09%09%09%09ContentObject result = library.newVersion(argName, contents);%0A%09%09%09%09%0A%09%09%09%09System.out.println(%22Inserted file %22 + args%5B1%5D + %22 as %22 + result.name() + %22 byte count %22 + contents.length
+%0A%09%09%09%09CCNDescriptor ccnd = library.open(argName, library.getDefaultPublisher(), null, null);%0A%09%09%09%09FileInputStream fis = new FileInputStream(theFile);%0A%09%09%09%09int size = BLOCK_SIZE;%0A%09%09%09%09byte %5B%5D buffer = new byte%5BBLOCK_SIZE%5D;%0A%09%09%09%09do %7B%0A%09%09%09%09%09if (size %3E fis.available())%0A%09%09%09%09%09%09size = fis.available();%0A%09%09%09%09%09if (size %3E 0) %7B%0A%09%09%09%09%09%09fis.read(buffer, 0, size);%0A%09%09%09%09%09%09ccnd.write(buffer, 0, size);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D while (size %3E fis.available());%0A%09%09%09%09ccnd.close();%0A%09%09%09%09%0A%09%09%09%09System.out.println(%22Inserted file %22 + args%5B1%5D
  + %22
@@ -3151,16 +3151,113 @@
 e();%0A%09%09%7D
+ catch (XMLStreamException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D
 %0A%0A%09%7D%0A%09%0A%09

Use buffered reads/writes in put_file