id
stringlengths
36
36
text
stringlengths
1
1.25M
cb918661-4c21-4b00-ac9f-3c00f4eec165
private boolean writeToFile(List<FifteenSearchNode> path){ try { File statText = new File("tilepuz-yc173.txt"); FileOutputStream is = new FileOutputStream(statText); OutputStreamWriter osw = new OutputStreamWriter(is); Writer w = new BufferedWriter(osw); for(int i = 0; i < path.size(); i++){ List<StringBuffer> wState = ((FifteenState) path.get(i).getState()).printState(); for(int j = 0; j < wState.size(); j++){ w.write(wState.get(j).toString()); } w.write("\n"); } w.close(); } catch (IOException e) { System.err.println("Can not write to the tilepuz-yc173.txt"); } return true; }
0ac24123-1c99-44db-8904-b37016dec7d1
public void addAllClasses(final List<File> classFiles) { for (final File classFile : classFiles) { final ClassNode node = loadClass(classFile); if (!isInnerClass(node)) { classesByName.put(node.name, node); } } }
6538af68-2661-40af-88c6-88fe59a90d9b
private boolean isInnerClass(ClassNode node) { // TODO; strengthen this return node.name.contains("$"); }
78c2e463-28da-486c-b5cc-3ed8c62c0ed6
private ClassNode loadClass(final File classFile) { try (final FileInputStream in = new FileInputStream(classFile)) { final ClassReader reader = new ClassReader(in); final ClassNode cls = new ClassNode(); reader.accept(cls, FLAGS); return cls; } catch (final IOException e) { throw new RuntimeException(e); } }
13236660-9e07-43b8-a1b4-b16740101812
public void analyse() { buildParentLookup(); buildPackageLookup(); }
5212f21d-b58b-4c65-b44f-180cd03fd9ab
private void buildPackageLookup() { for (final ClassNode cls : classesByName.values()) { final int pkgIndex = cls.name.lastIndexOf('/'); final String packageName = cls.name.substring(0, pkgIndex); classesByPackage.put(packageName, cls); } }
2bb832ce-1252-40b8-9a2c-97d5c34e9242
private void buildParentLookup() { for (final ClassNode cls : classesByName.values()) { final ClassNode parent = classesByName.get(cls.superName); if (parent != null) { classesToParent.put(cls, parent); } } }
89725131-f7ab-4fc2-8f51-f573c81bd608
public ClassHierachy getClassHierachy() { return new ClassHierachy(classesToParent, classesByPackage); }
843bd4dd-78aa-40c0-8d0d-0fdb2b8635c4
public void render(final File toFile, final ClassHierachy hierachy) { final Collection<String> packages = transform(hierachy.classesByPackageSorted.entrySet(), makePackage); final String entireGraph = renderGraph(concat(packages, Relationships.of(hierachy))); try { Files.write(entireGraph, toFile, Charset.defaultCharset()); } catch (final IOException e) { throw new RuntimeException(e); } }
6c9c2da9-a688-41e9-b741-637cb46513d3
private String renderGraph(final Iterable<String> children) { final ST graph = new ST("digraph G { \n fontname = \"Helvetica\" \n fontsize = 8\n<inner>\n }"); graph.add("inner", Joiner.on('\n').join(children)); return graph.render(); }
345e9ae6-3ed1-4b34-935a-1d89a7158baa
@Override public String apply(final Entry<String, SortedSet<ClassNode>> pkg) { final String packageName = pkg.getKey(); final List<ClassNode> classes = new ArrayList<>(pkg.getValue()); final String classString = renderClassString(classes); final ST cluster = makeCluster(classString, packageName); return cluster.render(); }
9fea296a-5528-4138-bed8-65a2a5912a9e
private ST makeCluster(final String classString, final String packageName) { final ST cluster = new ST("subgraph cluster_<packageIndex> { node [style=filled]; <classes> label = \"<name>\"; color=blue;fontsize=15 }"); cluster.add("packageIndex", packageIndex++); cluster.add("classes", classString); cluster.add("name", toDottedName(packageName)); return cluster; }
2f062888-ef96-4817-a1bd-449889152915
private String renderClassString(final List<ClassNode> classes) { final StringBuilder classString = new StringBuilder(); for (int classIndex = 0; classIndex < classes.size(); classIndex++) { final ST classNode = new ST("cls_<packageIndex>_<classIndex> [label=\"<name>\"];\n"); classNode.add("packageIndex", packageIndex); classNode.add("classIndex", classIndex); classNode.add("name", getShortName(classes.get(classIndex))); classString.append(classNode.render()); } return classString.toString(); }
af8011f3-854f-48fe-bb7c-b4f17f472d1f
private String toDottedName(String internal) { return internal.replace('/', '.'); }
9b01a48c-3fe9-435a-b2c4-b674d0c9c0f5
private String getShortName(final ClassNode cls) { int lastSlash = cls.name.lastIndexOf('/'); if (lastSlash == -1) return cls.name; return cls.name.substring(lastSlash + 1); }
be17c88a-e376-4353-8391-f036f7f21263
public static List<String> of(ClassHierachy hierachy) { return new Relationships(hierachy).asList(); }
78bd15f4-7a84-44f4-aae6-dca161aae977
private Relationships(ClassHierachy hierachy) { this.hierachy = hierachy; toRender = new ArrayList<>(); buildRelationShips(); }
fcc7e68b-9aa5-4587-bc89-817cda55bd60
@SuppressWarnings("unchecked") private void buildRelationShips() { int packageIndex = 0; for (final SortedSet<ClassNode> pkg : hierachy.classesByPackageSorted.values()) { int classIndex = 0; for (final ClassNode cls : pkg) { final ClassNode parent = hierachy.classesToParent.get(cls); ClassIdentifier id = new ClassIdentifier(packageIndex, classIndex); ClassIdentifier parentId = hierachy.findByNode(parent); newRelationShip(id, parentId, EXTENDS); List<String> interfaces = new ArrayList<>(cls.interfaces); newRelationShips(interfaces, id, IMPLEMENTS); analyseMethods(id, cls.methods); classIndex++; } packageIndex++; } }
855062d8-1936-4bf9-acf0-46ea1448792b
private void analyseMethods(ClassIdentifier cls, List<MethodNode> methods) { Set<String> classes = Sets.newHashSet(); Set<String> called = Sets.newHashSet(); for (MethodNode method : methods) { for (AbstractInsnNode instruction : method.instructions.toArray()) { if (instruction instanceof MethodInsnNode) { MethodInsnNode methodCall = (MethodInsnNode) instruction; called.add(methodCall.owner); } else if (instruction instanceof LdcInsnNode) { LdcInsnNode ldc = (LdcInsnNode) instruction; if (ldc.cst instanceof Type) { Type typeLiteral = (Type) ldc.cst; classes.add(typeLiteral.getInternalName()); } } } } newRelationShips(called, cls, CALLS); newRelationShips(difference(classes, called), cls, REFERS_TO_LITERAL); }
3f3783b6-fe73-4172-b887-9da2af664987
private void newRelationShips(Collection<String> from, ClassIdentifier to, RelationshipType type) { for (String className : from) { newRelationShip(hierachy.findByName(className), to, type); } }
e0da0c76-2377-4eeb-a2ad-551a2fb522cc
private void newRelationShip(ClassIdentifier from, ClassIdentifier to, RelationshipType type) { if (from.isKnown() && to.isKnown()) toRender.add(from + " -> " + to + " [color=" + type.colour + "];\n"); }
6d9663df-58b9-44ef-815d-8d169dc6b9d3
private List<String> asList() { return toRender; }
f748aab2-5544-4444-b22a-cf30b40b5886
public ClassIdentifier(int packageIndex, int classIndex) { known = true; this.packageIndex = packageIndex; this.classIndex = classIndex; }
dc575cfa-4b3a-45d5-8303-23f3bf647f93
public ClassIdentifier() { known = false; packageIndex = -1; classIndex = -1; }
090cd776-759e-4a2a-9057-80ba2069f21a
@Override public String toString() { final ST relation = new ST("cls_<packageIndex>_<classIndex>"); relation.add("packageIndex", packageIndex); relation.add("classIndex", classIndex); return relation.render(); }
d259e0f8-7a0b-4d0f-8709-9a3f95fc2c10
public boolean isKnown() { return known; }
15dd0602-e8d7-4614-a523-d226c4903fef
@Override public int compare(final ClassNode left, final ClassNode right) { return left.name.compareTo(right.name); }
95ce5146-a4fd-45a7-bff9-2c508b4e6bb8
public ClassHierachy(final Map<ClassNode, ClassNode> classesToParent, final Multimap<String, ClassNode> classesByPackage) { this.classesToParent = new HashMap<>(classesToParent); this.classesByPackage = HashMultimap.create(classesByPackage); classesByPackageSorted = new TreeMap<>(); makeSortedPackageLookup(); }
ac961a13-ed1a-4648-966a-c23b7babafd5
public void makeSortedPackageLookup() { for (final Entry<String, Collection<ClassNode>> e : classesByPackage.asMap().entrySet()) { final SortedSet<ClassNode> nodes = new TreeSet<>(compareClassByName); nodes.addAll(e.getValue()); classesByPackageSorted.put(e.getKey(), nodes); } }
728305c2-f8be-4ebf-8916-aa5cd5eb1c5d
public ClassIdentifier findByName(final String toFind) { return find(new Predicate<ClassNode>() { @Override public boolean apply(ClassNode input) { return input.name.equals(toFind); } }); }
68270786-2096-4d43-b729-dc92b687a21b
@Override public boolean apply(ClassNode input) { return input.name.equals(toFind); }
662dde31-32c4-45dd-ad37-d200a8499f8d
public ClassIdentifier findByNode(final ClassNode toFind) { return find(new Predicate<ClassNode>() { @Override public boolean apply(ClassNode input) { return input == toFind; } }); }
5f0452fb-f0bb-4e4c-acf0-eb0d9d2072b0
@Override public boolean apply(ClassNode input) { return input == toFind; }
6247d1c2-5e91-4311-b9b3-a5243d9c3acf
private ClassIdentifier find(final Predicate<ClassNode> toFind) { int packageIndex = 0; for (final SortedSet<ClassNode> pkg : classesByPackageSorted.values()) { int classIndex = 0; for (final ClassNode cls : pkg) { if (toFind.apply(cls)) { return new ClassIdentifier(packageIndex, classIndex); } classIndex++; } packageIndex++; } return new ClassIdentifier(); }
bbc80db2-9e3f-40f8-b8d3-917951179056
public static void main(final String[] args) { final List<File> classFiles = listClasses(new File(args[0])); final ClassHierachyAnalysis analysis = new ClassHierachyAnalysis(); analysis.addAllClasses(classFiles); analysis.analyse(); final DotRenderer renderer = new DotRenderer(); renderer.render(new File("output.dot"), analysis.getClassHierachy()); }
b34e8702-7185-4d4c-a003-f90591222d0b
private static List<File> listClasses(final File dir) { final List<File> files = new ArrayList<>(); listClassesRec(dir, files); return files; }
11981dd3-7667-444d-a34f-b194f26d1b44
private static void listClassesRec(final File dir, final List<File> files) { if (dir.isDirectory()) { for (final File subdir : dir.listFiles(new FileFilter() { @Override public boolean accept(final File file) { return file.isDirectory(); } })) { listClassesRec(subdir, files); } for (final File file : dir.listFiles()) { if (file.isFile() && file.getAbsolutePath().endsWith(".class")) { files.add(file); } } } }
25c337d3-1d8a-44f6-8a46-183cf323485d
@Override public boolean accept(final File file) { return file.isDirectory(); }
210eb9a0-7842-49c4-9620-0284614b63a0
private RelationshipType(final String colour) { this.colour = colour; }
990f7850-74a7-49b6-9e4c-3b2e0d024afd
public ToDoList() { tasks = new ArrayList<Task>(); }
75ade23e-da2a-438d-8702-27cd786fa1b0
public void addTask(Task task) { tasks.add(task); }
bfc6c449-d309-43c7-8ad3-19765fd3c23a
public Task nextTask() { return tasks.get(0); }
6b0af7e4-b2ba-4bb0-b918-66cbaff48332
public Integer getNumberOfTasks() { return tasks.size(); }
98dd9b5d-cd05-4f44-abff-0a4ebac2041a
public void markDone(Task task) { tasks.remove(task); }
8905462f-ba2c-4589-a353-212ae3166e62
public Task(String description) { this.description = description; }
b1a1608b-713c-40f4-ae95-afa82c102be7
public String description() { return description; }
d690781d-83f6-4c32-8fa7-a214dd9b112e
@Override public boolean equals(Object obj) { Task other = (Task) obj; if (!description.equals(other.description)) return false; return true; }
91da6723-9871-4e24-a59a-ab04fba4b366
@Given("^an empty to-do list$") public void createEmptyToDoList() { toDoList = new ToDoList(); }
ffd9a92a-6b01-4faa-bb78-b4b4d21e9e72
@When("^you add the task: (.+)$") public void addTask(String task) { toDoList.addTask(new Task(task)); }
6e3b18ff-ebd2-40c5-9bf3-09ccdd9d0737
@Then("^the to-do list contains the task: (.+)$") public void toDoListMustContainOneTask(String task) { assertThat(toDoList.nextTask(), is(new Task(task))); }
cae72375-515b-4367-a97c-c6e547150438
@And("^the number of tasks into to-do list should be (\\d+)$") public void the_number_of_tasks_into_to_do_list_should_be(int numberOfTasks) { assertThat(toDoList.getNumberOfTasks(), is(numberOfTasks)); }
754bd0b4-c318-4622-9fce-8b23cdd01b53
@Given("^a To-Do list with the task: (.+)$") public void a_To_Do_list_with_the_task(String task) { toDoList = new ToDoList(); toDoList.addTask(new Task(task)); }
5b3961d5-d0bb-43aa-b825-bf177cde617b
@When("^you mark done the task: (.+)$") public void you_mark_done_the_task(String description) { toDoList.markDone(new Task(description)); }
50a99e14-8e9d-4fe2-949b-125df045d79d
@Then("^the number of tasks into to-do list should be (\\d+)$") public void the_number_of_tasks_into_to_do_list_should_be(int numberOfTasks) { assertThat(toDoList.getNumberOfTasks(), is(numberOfTasks)); }
339f0273-2643-4484-bce2-fda0a857b009
public ToDoList() { tasks = new ArrayList<Task>(); }
ea18e1f0-20ff-46d4-a519-079854b73242
public void addTask(Task task) { tasks.add(task); }
eb8c8bf5-d3d3-4421-a8b6-d4b3ee595dfe
public Task nextTask() { return tasks.get(0); }
fe2c07b6-cd1f-4744-b32f-46cda0415ac8
public Integer getNumberOfTasks() { return tasks.size(); }
a959e77b-b953-4e83-bdcf-830c9cdaee1e
public Task(String description) { this.description = description; }
a8dfe12c-32b9-484b-9c7d-00fa3bf5bc4e
public String description() { return description; }
0532e153-5cc0-4897-85ae-c8f9aafc2c15
@Override public boolean equals(Object obj) { Task other = (Task) obj; if (!description.equals(other.description)) return false; return true; }
0fa06dc0-3b13-4460-851c-4311535f6274
public ToDoList() { tasks = new ArrayList<Task>(); }
ba3d1c13-08bb-4a7c-a81a-2ab07a84c4a7
public void addTask(Task task) { tasks.add(task); }
a31bc626-8e8c-447b-a21a-4c7172e32eba
public Task nextTask() { return tasks.get(0); }
093bdfa0-9df7-42c5-b25b-d7f1ca5e91d0
public Integer getNumberOfTasks() { return tasks.size(); }
3ebac26a-9955-48f2-9c92-569311b83059
public Task(String description) { this.description = description; }
098569cb-0a6c-4535-980e-0ae731a41e3d
public String description() { return description; }
3fce3ff2-86e3-400e-8a75-ca21069b8c2a
@Override public boolean equals(Object obj) { Task other = (Task) obj; if (!description.equals(other.description)) return false; return true; }
ed08201d-5cf7-4a93-88bb-d6ba4fc1f431
@Given("an empty to-do list") public void createEmptyToDoList() { toDoList = new ToDoList(); }
30399c77-8693-4143-887a-b33461c0497b
@When("you add the task: $task") public void addTask(String task) { toDoList.addTask(new Task(task)); }
0b447635-8650-4bcb-9171-849a1d3ca672
@Then("the to-do list contains the task: $task") public void toDoListMustContainOneTask(String task) { assertThat(toDoList.nextTask(), is(new Task(task))); }
1a03a273-07a8-4404-bd18-2bee9055aba9
@Then("the number of tasks into to-do list should be $numberOfTasks") public void the_number_of_tasks_into_to_do_list_should_be(int numberOfTasks) { assertThat(toDoList.getNumberOfTasks(), is(numberOfTasks)); }
177a7505-d8d2-4bc6-8132-579fbcfa34da
public AddingTasksStory() { Configuration configuration = new MostUsefulConfiguration() .useStoryLoader(new LoadFromClasspath(this.getClass().getClassLoader())) .useStepMonitor(new PrintStreamStepMonitor()); addSteps(new InstanceStepsFactory(configuration, new AddingTasksSteps()).createCandidateSteps()); }
c5fef2a9-667d-4fb1-a4f4-3228bb55b00b
public HashServer(String file, int blocksize, int errorquote) { byte[] bytes = ADSTool.readByteArray(file); bytes = ADSTool.pad(bytes, blocksize); this.ht = new HashTree(bytes, blocksize); this.errorquote = errorquote; this.blocksize = blocksize; }
ebe95713-5443-48ca-a62f-c4de370280a2
@Override public byte[] get(String path) { if (path.equals("noblocks")) { return ByteBuffer.allocate(4).putInt(ht.getNoBlocks()).array(); } else { if (random.nextInt(1000) < errorquote) { byte[] randoms = new byte[blocksize + 20 * path.length()]; random.nextBytes(randoms); return randoms; } return ht.getTransportBlocks(path); } }
ce1b5102-3bcd-4dd0-a55f-a8447eed67bc
public Hash(byte[] bytes) { this.bytes = bytes; }
7bda1568-d9b2-44a9-8769-c62b71808ac5
public byte[] getBytes() { return bytes; }
811351ad-0fcc-4aac-a437-98b9b60ddf89
public int length() { return bytes.length; }
28c8aa64-51a8-431f-94dc-3eba7849d72e
public Hash chainedTo(Hash hash) { if (hash == null) { byte[] zeroes = new byte[length()]; hash = new Hash(zeroes); } byte[] newBytes = new byte[length() + hash.length()]; System.arraycopy(bytes, 0, newBytes, 0, length()); System.arraycopy(hash.getBytes(), 0, newBytes, length(), hash.length()); return new Hash(ADSTool.sha1(newBytes)); }
4467bf63-e4e4-4102-a998-f20f4faf30bf
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(ADSTool.byteArrayToHexString(new byte[] { b }) + " "); } return sb.toString(); }
16c53c72-380f-44d0-942f-f9bd18ae0913
@Override public boolean equals(Object o) { if (!(o instanceof Hash)) return false; Hash h = (Hash) o; for (int i = 0; i < bytes.length; i++) { if (h.getBytes()[i] != bytes[i]) return false; } return true; }
7a4fb924-6a4d-4a22-9ae3-782ae2f3ad84
public static void main(String[] args) { if (args.length < 22) { System.err.println("Verwendung: <Quell-URL> <Ausgabedatei> <Roothash (Byte 1)> <Roothash (Byte 2)> ... <Roothash (Byte 20)>"); } String src = args[0]; String dest = args[1]; String[] hashStr = new String[20]; byte[] hashBytes = new byte[20]; System.arraycopy(args, 2, hashStr, 0, 20); for (int i = 0; i < hashStr.length; i++) { hashBytes[i] = (byte) Integer.parseInt(hashStr[i], 16); } Hash hash = new Hash(hashBytes); HashClient client = new HashClient(src, dest, hash); client.query(); }
0d47b59a-e7c4-4a3b-ae1c-fdc2ccaf0519
public static void main(String[] args) { if (args.length < 2) { System.err.println("Verwendung: <blocksize> <Generierungsgröße>/<Eingabedatei>"); } int blocksize = 0; try { blocksize = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Blockgröße muss eine Zahl sein!"); System.exit(-1); } boolean generate = false; String inputfile = args[1]; byte[] input = null; try { input = ADSTool.readByteArray(inputfile); } catch (Exception e) { System.out.println("\nDatei konnte nicht gelesen werden. Versuche, als Generierungsgröße zu interpretieren..."); } if (input == null) { generate = true; } if (generate) { int gensize = 0; try { gensize = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Generatorgröße muss eine Zahl sein!"); System.exit(-1); } input = new byte[gensize]; for (int i = 0; i < input.length; i++) { input[i] = (byte) i; } } HashTree ht = new HashTree(input, blocksize); System.out.println("HashTreeData: "); ht.display(); }
dcb6fbcd-54c2-476e-82c5-a2f34bea212a
public HashTree(byte[] field, int blockSize) { this.noblocks = (int) Math.ceil((double) field.length / blockSize); byte[] field2 = new byte[noblocks * blockSize]; System.arraycopy(field, 0, field2, 0, field.length); root = buildHashTree(getLeafNodes(field2, blockSize)); this.blocksize = blockSize; this.fieldlength = field2.length; }
3800abd4-04ba-4308-990b-05f056da324f
public Node getRoot() { return root; }
6bfc0b2c-c482-4ac3-be69-0387a03d2b07
public Node(Hash hash) { this.hash = hash; }
b73402cd-89bb-4b39-b152-bbed2ae36ac3
public InnerNode(Node leftChild, Node rightChild) { super(leftChild.hash.chainedTo((rightChild == null) ? null : rightChild.hash)); this.left = leftChild; this.right = rightChild; }
12c291fc-f867-4133-b3f2-845c71529ce8
public LeafNode(byte[] blocks) { super(new Hash(ADSTool.sha1(blocks))); this.data = blocks; }
ced86f8c-f27c-4b61-8065-6a162d7b9d20
private int getLevels(int numberOfNodes) { int i = 0; for (int j = 1; j < numberOfNodes; i++, j *= 2) ; return i; }
4f0767ae-25ca-4718-bfb1-35def340ab52
private Node buildHashTree(ArrayList<Node> nodes) { if (nodes.size() == 1) { return nodes.get(0); } ArrayList<Node> parentNodes = new ArrayList<>(); if ((nodes.size() % 2) != 0) { nodes.add(null); } for (int i = 1; i < nodes.size(); i += 2) { parentNodes.add(new InnerNode(nodes.get(i - 1), nodes.get(i))); } return buildHashTree(parentNodes); }
a61c481c-5e24-4374-a5bc-db9aa1303aa6
private ArrayList<Node> getLeafNodes(byte[] field, int blockSize) { ArrayList<Node> leafNodes = new ArrayList<>(); int position = 0; for (int i = 0; i < noblocks; i++) { byte[] help = new byte[blockSize]; System.arraycopy(field, position, help, 0, blockSize); leafNodes.add(new LeafNode(help)); position += blockSize; } return leafNodes; }
f75800f5-b4c5-4ac0-9faf-966f93325d40
public void display() { System.out.print("bytes=" + fieldlength); System.out.print(", blocksize=" + blocksize); System.out.print(", noblocks=" + noblocks); System.out.println(", height=" + getLevels(noblocks)); System.out.println(" i Pfad SHA-1 Hash"); System.out.println(); display(root, ""); }
5be0d745-c1f6-49a0-98d8-b29d69732786
private void display(Node node, String s) { String path = ""; if (node instanceof LeafNode) { path = String.valueOf(Integer.parseInt(s, 2)); } System.out.format("%3s ", path); System.out.format("%-6s ", s); System.out.println(node.hash.toString()); // Geht durch alle Knoten rekursiv if (node instanceof InnerNode) { InnerNode nodeInner = (InnerNode) node; if (nodeInner.left != null) display(nodeInner.left, s + 0); if (nodeInner.right != null) display(nodeInner.right, s + 1); } }
bf774f5c-3403-4d60-a904-84d58597fb27
public byte[] getTransportBlocks(String path) { InnerNode walk = (InnerNode) root; ArrayList<Hash> neighbours = new ArrayList<>(); LeafNode leaf = null; // Iteriert von der Wurzel bis zum Blatt // Merkt sich alle Schwesterhashes auf dem Weg for (int i = 0; i < path.length(); i++) { Node needed = null; if (path.charAt(i) == '0') { Hash hash = walk.right == null ? new Hash(new byte[walk.left.hash.length()]) : walk.right.hash; neighbours.add(hash); needed = walk.left; } else { neighbours.add(walk.left.hash); needed = walk.right; } if (needed instanceof InnerNode) { walk = (InnerNode) needed; } else { leaf = (LeafNode) needed; } } byte[] stream = new byte[neighbours.size() * 20 + blocksize]; int offset = 0; for (int i = neighbours.size() - 1; i >= 0; i--) { Hash h = neighbours.get(i); System.arraycopy(h.getBytes(), 0, stream, offset, h.length()); offset += h.length(); } if (leaf == null) { leaf = new LeafNode(new byte[blocksize]); } System.arraycopy(leaf.data, 0, stream, offset, leaf.data.length); return stream; }
486e526d-d4d8-406c-9e1d-83a3d5f58c65
public int getNoBlocks() { return noblocks; }
1562cb7c-b714-4622-9066-2c47e47af170
public HashClient(String url, String outputfile, Hash rootHash) { this.url = url; this.outputfile = outputfile; this.rootHash = rootHash; }
84aafe88-8fcf-47c0-9f47-3e03e5a5b8a9
public void query() { int noBlocks = ADSTool.toInt(ADSTool.getURLBytes(url + "noblocks")); System.out.println("Number of blocks: " + noBlocks); String[] path = getPaths(noBlocks); ArrayList<byte[]> datas = new ArrayList<>(); int size = 0; int errors = 0; for (int i = 0; i < path.length; i++) { byte[] leaf = ADSTool.getURLBytes(url + path[i]); // Blätter int offset = getLevels(noBlocks) * 20; byte[] dataBlocks = new byte[leaf.length - offset]; // Datenblock System.arraycopy(leaf, offset, dataBlocks, 0, dataBlocks.length); // Datenblock // kopieren ArrayList<Hash> neighbours = getNeighbours(leaf, getLevels(noBlocks)); Hash hash = getRootHash(neighbours, new Hash(ADSTool.sha1(dataBlocks)), path[i]); System.out.println("Wurzel " + i + ": " + hash.toString()); if (!hash.equals(rootHash)) { if (errors > 9) { System.out.println("Paket über 9 mal falsch angekommen. Abbruch..."); return; } i--; errors++; System.out.println("Falsch. Versuche erneut..."); } else { datas.add(dataBlocks); size += dataBlocks.length; errors = 0; } } write(datas, size); }
4f7db473-56b7-474d-a305-06b9022bc38a
private Hash getRootHash(ArrayList<Hash> neighbours, Hash dataHash, String path) { Hash hash = dataHash; for (int i = 0; i < neighbours.size(); i++) { char c = path.charAt(neighbours.size() - i - 1); if (c == '0') { hash = hash.chainedTo(neighbours.get(i)); } else { hash = neighbours.get(i).chainedTo(hash); } } return hash; }
9afd7e77-b7f7-41fc-a826-dbaef37dceba
private int getLevels(int numberOfNodes) { int i = 0; for (int j = 1; j < numberOfNodes; i++, j *= 2) ; return i; }
b6bd0a4c-de4a-4773-8e09-b12bba097f05
private String[] getPaths(int noBlocks) { String[] a = new String[noBlocks]; int depth = getLevels(noBlocks); for (int i = 0; i < noBlocks; i++) { String binary = "00000000000000000000000000000000" + Integer.toBinaryString(i); a[i] = binary.substring(binary.length() - depth); } return a; }