[freenet-cvs] r13536 - trunk/freenet/src/freenet/client/async

toad at freenetproject.org toad at freenetproject.org
Tue Jun 12 19:22:51 UTC 2007


Author: toad
Date: 2007-06-12 19:22:51 +0000 (Tue, 12 Jun 2007)
New Revision: 13536

Added:
   trunk/freenet/src/freenet/client/async/BlockSet.java
   trunk/freenet/src/freenet/client/async/SimpleBlockSet.java
Modified:
   trunk/freenet/src/freenet/client/async/BinaryBlob.java
   trunk/freenet/src/freenet/client/async/BinaryBlobInserter.java
Log:
Refactor BinaryBlobInserter into BinaryBlob.readBinaryBlob and *BlockSet.

Modified: trunk/freenet/src/freenet/client/async/BinaryBlob.java
===================================================================
--- trunk/freenet/src/freenet/client/async/BinaryBlob.java	2007-06-12 17:32:13 UTC (rev 13535)
+++ trunk/freenet/src/freenet/client/async/BinaryBlob.java	2007-06-12 19:22:51 UTC (rev 13536)
@@ -1,10 +1,15 @@
 package freenet.client.async;
 
+import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.EOFException;
 import java.io.IOException;
 
+import com.onionnetworks.util.FileUtil;
+
 import freenet.keys.Key;
 import freenet.keys.KeyBlock;
+import freenet.keys.KeyVerifyException;
 
 public abstract class BinaryBlob {
 
@@ -46,4 +51,70 @@
 		writeBlobHeader(binaryBlobStream, BinaryBlob.BLOB_END, BinaryBlob.BLOB_END_VERSION, 0);
 	}
 
+	public static void readBinaryBlob(DataInputStream dis, BlockSet blocks, boolean tolerant) throws IOException, BinaryBlobFormatException {
+		long magic = dis.readLong();
+		if(magic != BinaryBlob.BINARY_BLOB_MAGIC)
+			throw new BinaryBlobFormatException("Bad magic");
+		short version = dis.readShort();
+		if(version != BinaryBlob.BINARY_BLOB_OVERALL_VERSION)
+			throw new BinaryBlobFormatException("Unknown overall version");
+		
+		int i=0;
+		while(true) {
+			long blobLength;
+			try {
+				blobLength = dis.readInt() & 0xFFFFFFFFL;
+			} catch (EOFException e) {
+				// End of file
+				dis.close();
+				break;
+			}
+			short blobType = dis.readShort();
+			short blobVer = dis.readShort();
+			
+			if(blobType == BinaryBlob.BLOB_END) {
+				dis.close();
+				break;
+			} else if(blobType == BinaryBlob.BLOB_BLOCK) {
+				if(blobVer != BinaryBlob.BLOB_BLOCK_VERSION)
+					// Even if tolerant, if we can't read a blob there probably isn't much we can do.
+					throw new BinaryBlobFormatException("Unknown block blob version");
+				if(blobLength < 9)
+					throw new BinaryBlobFormatException("Block blob too short");
+				short keyType = dis.readShort();
+				int keyLen = dis.readUnsignedByte();
+				int headersLen = dis.readUnsignedShort();
+				int dataLen = dis.readUnsignedShort();
+				int pubkeyLen = dis.readUnsignedShort();
+				int total = 9 + keyLen + headersLen + dataLen + pubkeyLen;
+				if(blobLength != total)
+					throw new BinaryBlobFormatException("Binary blob not same length as data: blobLength="+blobLength+" total="+total);
+				byte[] keyBytes = new byte[keyLen];
+				byte[] headersBytes = new byte[headersLen];
+				byte[] dataBytes = new byte[dataLen];
+				byte[] pubkeyBytes = new byte[pubkeyLen];
+				dis.readFully(keyBytes);
+				dis.readFully(headersBytes);
+				dis.readFully(dataBytes);
+				dis.readFully(pubkeyBytes);
+				KeyBlock block;
+				try {
+					block = Key.createBlock(keyType, keyBytes, headersBytes, dataBytes, pubkeyBytes);
+				} catch (KeyVerifyException e) {
+					throw new BinaryBlobFormatException("Invalid key: "+e.getMessage(), e);
+				}
+				
+				blocks.add(block);
+				
+			} else {
+				if(tolerant) {
+					FileUtil.skipFully(dis, blobLength);
+				} else {
+					throw new BinaryBlobFormatException("Unknown blob type: "+blobType);
+				}
+			}
+			i++;
+		}
+
+	}
 }

Modified: trunk/freenet/src/freenet/client/async/BinaryBlobInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/BinaryBlobInserter.java	2007-06-12 17:32:13 UTC (rev 13535)
+++ trunk/freenet/src/freenet/client/async/BinaryBlobInserter.java	2007-06-12 19:22:51 UTC (rev 13536)
@@ -1,19 +1,16 @@
 package freenet.client.async;
 
 import java.io.DataInputStream;
-import java.io.EOFException;
 import java.io.IOException;
+import java.util.Iterator;
 import java.util.Vector;
 
-import com.onionnetworks.util.FileUtil;
-
 import freenet.client.FailureCodeTracker;
 import freenet.client.InsertContext;
 import freenet.client.InsertException;
 import freenet.keys.CHKBlock;
 import freenet.keys.Key;
 import freenet.keys.KeyBlock;
-import freenet.keys.KeyVerifyException;
 import freenet.keys.SSKBlock;
 import freenet.node.LowLevelPutException;
 import freenet.node.SimpleSendableInsert;
@@ -44,74 +41,24 @@
 		this.parent = parent;
 		this.clientContext = clientContext;
 		this.errors = new FailureCodeTracker(true);
-		Vector myInserters = new Vector();
 		DataInputStream dis = new DataInputStream(blob.getInputStream());
-		long magic = dis.readLong();
-		if(magic != BinaryBlob.BINARY_BLOB_MAGIC)
-			throw new BinaryBlobFormatException("Bad magic");
-		short version = dis.readShort();
-		if(version != BinaryBlob.BINARY_BLOB_OVERALL_VERSION)
-			throw new BinaryBlobFormatException("Unknown overall version");
 		
-		int i=0;
-		while(true) {
-			long blobLength;
-			try {
-				blobLength = dis.readInt() & 0xFFFFFFFFL;
-			} catch (EOFException e) {
-				// End of file
-				dis.close();
-				break;
-			}
-			short blobType = dis.readShort();
-			short blobVer = dis.readShort();
-			
-			if(blobType == BinaryBlob.BLOB_END) {
-				dis.close();
-				break;
-			} else if(blobType == BinaryBlob.BLOB_BLOCK) {
-				if(blobVer != BinaryBlob.BLOB_BLOCK_VERSION)
-					// Even if tolerant, if we can't read a blob there probably isn't much we can do.
-					throw new BinaryBlobFormatException("Unknown block blob version");
-				if(blobLength < 9)
-					throw new BinaryBlobFormatException("Block blob too short");
-				short keyType = dis.readShort();
-				int keyLen = dis.readUnsignedByte();
-				int headersLen = dis.readUnsignedShort();
-				int dataLen = dis.readUnsignedShort();
-				int pubkeyLen = dis.readUnsignedShort();
-				int total = 9 + keyLen + headersLen + dataLen + pubkeyLen;
-				if(blobLength != total)
-					throw new BinaryBlobFormatException("Binary blob not same length as data: blobLength="+blobLength+" total="+total);
-				byte[] keyBytes = new byte[keyLen];
-				byte[] headersBytes = new byte[headersLen];
-				byte[] dataBytes = new byte[dataLen];
-				byte[] pubkeyBytes = new byte[pubkeyLen];
-				dis.readFully(keyBytes);
-				dis.readFully(headersBytes);
-				dis.readFully(dataBytes);
-				dis.readFully(pubkeyBytes);
-				KeyBlock block;
-				try {
-					block = Key.createBlock(keyType, keyBytes, headersBytes, dataBytes, pubkeyBytes);
-				} catch (KeyVerifyException e) {
-					throw new BinaryBlobFormatException("Invalid key: "+e.getMessage(), e);
-				}
-				
-				MySendableInsert inserter =
-					new MySendableInsert(i, block, prioClass, getScheduler(block), clientContext);
-				
-				myInserters.add(inserter);
-				
-			} else {
-				if(tolerant) {
-					FileUtil.skipFully(dis, blobLength);
-				} else {
-					throw new BinaryBlobFormatException("Unknown blob type: "+blobType);
-				}
-			}
-			i++;
+		BlockSet blocks = new SimpleBlockSet();
+		
+		BinaryBlob.readBinaryBlob(dis, blocks, tolerant);
+		
+		Vector myInserters = new Vector();
+		Iterator i = blocks.keys().iterator();
+		
+		int x=0;
+		while(i.hasNext()) {
+			Key key = (Key) i.next();
+			KeyBlock block = blocks.get(key);
+			MySendableInsert inserter =
+				new MySendableInsert(x++, block, prioClass, getScheduler(block), clientContext);
+			myInserters.add(inserter);
 		}
+		
 		inserters = (MySendableInsert[]) myInserters.toArray(new MySendableInsert[myInserters.size()]);
 		parent.addMustSucceedBlocks(inserters.length);
 		parent.notifyClients();

Added: trunk/freenet/src/freenet/client/async/BlockSet.java
===================================================================
--- trunk/freenet/src/freenet/client/async/BlockSet.java	                        (rev 0)
+++ trunk/freenet/src/freenet/client/async/BlockSet.java	2007-06-12 19:22:51 UTC (rev 13536)
@@ -0,0 +1,37 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.client.async;
+
+import java.util.Set;
+
+import freenet.keys.Key;
+import freenet.keys.KeyBlock;
+
+/**
+ * A set of KeyBlock's.
+ * @author toad
+ */
+public interface BlockSet {
+
+	/**
+	 * Get a block by its key.
+	 * @param key The key of the block to get.
+	 * @return A block, or null if there is no block with that key.
+	 */
+	public KeyBlock get(Key key);
+	
+	/**
+	 * Add a block.
+	 * @param block The block to add.
+	 */
+	public void add(KeyBlock block);
+	
+	/**
+	 * Get the set of all the keys of all the blocks.
+	 * @return A set of the keys of the blocks in the BlockSet. Not guaranteed to be
+	 * kept up to date. Read only.
+	 */
+	public Set keys();
+	
+}

Added: trunk/freenet/src/freenet/client/async/SimpleBlockSet.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SimpleBlockSet.java	                        (rev 0)
+++ trunk/freenet/src/freenet/client/async/SimpleBlockSet.java	2007-06-12 19:22:51 UTC (rev 13536)
@@ -0,0 +1,30 @@
+package freenet.client.async;
+
+import java.util.HashMap;
+import java.util.Set;
+
+import freenet.keys.Key;
+import freenet.keys.KeyBlock;
+
+/** 
+ * Simple BlockSet implementation, keeps all keys in RAM.
+ * 
+ * @author toad
+ */
+public class SimpleBlockSet implements BlockSet {
+
+	private final HashMap blocksByKey = new HashMap();
+	
+	public synchronized void add(KeyBlock block) {
+		blocksByKey.put(block.getKey(), block);
+	}
+
+	public KeyBlock get(Key key) {
+		return (KeyBlock) blocksByKey.get(key);
+	}
+
+	public Set keys() {
+		return blocksByKey.keySet();
+	}
+
+}




More information about the cvs mailing list