public void onGotKey(Key key, KeyBlock block, ObjectContainer container, ClientContext context) { if (persistent) { container.activate(this, 1); container.activate(segment, 1); container.activate(blockNums, 1); } if (logMINOR) Logger.minor(this, "onGotKey(" + key + ")"); // Find and remove block if it is on this subsegment. However it may have been // removed already. int blockNo; synchronized (segment) { for (int i = 0; i < blockNums.size(); i++) { Integer token = blockNums.get(i); int num = token; Key k = segment.getBlockNodeKey(num, container); if (k != null && k.equals(key)) { blockNums.remove(i); if (persistent) container.delete(token); break; } } blockNo = segment.getBlockNumber(key, container); } if (blockNo == -1) { Logger.minor(this, "No block found for key " + key + " on " + this); return; } Integer token = Integer.valueOf(blockNo); ClientCHK ckey = segment.getBlockKey(blockNo, container); ClientCHKBlock cb; try { cb = new ClientCHKBlock((CHKBlock) block, ckey); } catch (CHKVerifyException e) { onFailure( new FetchException(FetchException.BLOCK_DECODE_ERROR, e), token, container, context); return; } Bucket data = extract(cb, token, container, context); if (data == null) return; if (!cb.isMetadata()) { onSuccess(data, false, token, (token).intValue(), cb, container, context); } else { onFailure( new FetchException(FetchException.INVALID_METADATA, "Metadata where expected data"), token, container, context); } }
protected static ClientKeyBlock innerEncode( RandomSource random, FreenetURI uri, Bucket sourceData, boolean isMetadata, short compressionCodec, int sourceLength, String compressorDescriptor) throws InsertException, CHKEncodeException, IOException, SSKEncodeException, MalformedURLException, InvalidCompressionCodecException { String uriType = uri.getKeyType(); if (uriType.equals("CHK")) { return ClientCHKBlock.encode( sourceData, isMetadata, compressionCodec == -1, compressionCodec, sourceLength, compressorDescriptor); } else if (uriType.equals("SSK") || uriType.equals("KSK")) { InsertableClientSSK ik = InsertableClientSSK.create(uri); return ik.encode( sourceData, isMetadata, compressionCodec == -1, compressionCodec, sourceLength, random, compressorDescriptor); } else { throw new InsertException(InsertException.INVALID_URI, "Unknown keytype " + uriType, null); } }
public static void main(String[] args) throws Exception { // Setup datastore FreenetStore fs = new FreenetStore("datastore", "headerstore", 1024); // Setup logging Logger.setupStdoutLogging(Logger.DEBUG, ""); printHeader(); // Read command, and data BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String line = reader.readLine(); if (line.toUpperCase().startsWith("GET:")) { // Should have a key next String key = line.substring("GET:".length()); while (key.length() > 0 && key.charAt(0) == ' ') key = key.substring(1); while (key.length() > 0 && key.charAt(key.length() - 1) == ' ') key = key.substring(0, key.length() - 2); Logger.normal(DatastoreTest.class, "Key: " + key); FreenetURI uri = new FreenetURI(key); ClientCHK chk = new ClientCHK(uri); CHKBlock block; try { block = fs.fetch(chk.getNodeCHK()); } catch (CHKVerifyException e1) { Logger.error(DatastoreTest.class, "Did not verify: " + e1, e1); continue; } if (block == null) { System.out.println("Not found in store: " + chk.getURI()); } else { // Decode it byte[] decoded; try { decoded = block.decode(chk); } catch (CHKDecodeException e) { Logger.error(DatastoreTest.class, "Cannot decode: " + e, e); continue; } System.out.println("Decoded data:\n"); System.out.println(new String(decoded)); } } else if (line.toUpperCase().startsWith("QUIT")) { System.out.println("Goodbye."); System.exit(0); } else if (line.toUpperCase().startsWith("PUT:")) { line = line.substring("PUT:".length()); while (line.length() > 0 && line.charAt(0) == ' ') line = line.substring(1); while (line.length() > 0 && line.charAt(line.length() - 1) == ' ') line = line.substring(0, line.length() - 2); String content; if (line.length() > 0) { // Single line insert content = line; } else { // Multiple line insert StringBuilder sb = new StringBuilder(1000); while (true) { line = reader.readLine(); if (line.equals(".")) break; sb.append(line).append('\n'); } content = sb.toString(); } // Insert byte[] data = content.getBytes(); ClientCHKBlock block; try { block = ClientCHKBlock.encode(data); } catch (CHKEncodeException e) { Logger.error(DatastoreTest.class, "Couldn't encode: " + e, e); continue; } ClientCHK chk = block.getClientKey(); FreenetURI uri = chk.getURI(); fs.put(block); // Definitely interface System.out.println("URI: " + uri); } else { } } }