Replaced improper ByteBuffer.equals() implementation by Arrays.equals()

Renamed also ByteBuffer.equals() to startsWith() as this is the
appropriate function implementation semantics.
pull/167/head
luccioman 7 years ago
parent 098ee63911
commit 929e0d6eae

@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
@ -42,7 +43,6 @@ import net.yacy.cora.federate.yacy.CacheStrategy;
import net.yacy.cora.protocol.ClientIdentification;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.storage.HandleSet;
import net.yacy.cora.util.ByteBuffer;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.cora.util.SpaceExceededException;
import net.yacy.data.ListManager;
@ -146,7 +146,7 @@ public class IndexControlRWIs_p {
}
if ( post.containsKey("keyhashsearch") ) {
if ( keystring.isEmpty() || !ByteBuffer.equals(Word.word2hash(keystring), keyhash) ) {
if ( keystring.isEmpty() || !Arrays.equals(Word.word2hash(keystring), keyhash) ) {
prop.put("keystring", "<" + errmsg + ">");
}
final SearchEvent theSearch = genSearchresult(prop, sb, keyhash, null);
@ -246,7 +246,7 @@ public class IndexControlRWIs_p {
}
if ( post.containsKey("urllist") ) {
if ( keystring.isEmpty() || !ByteBuffer.equals(Word.word2hash(keystring), keyhash) ) {
if ( keystring.isEmpty() || !Arrays.equals(Word.word2hash(keystring), keyhash) ) {
prop.put("keystring", "<" + errmsg + ">");
}
final Bitfield flags = compileFlags(post);
@ -261,7 +261,7 @@ public class IndexControlRWIs_p {
TransactionManager.checkPostTransaction(header, post);
try {
if ( keystring.isEmpty() || !ByteBuffer.equals(Word.word2hash(keystring), keyhash) ) {
if ( keystring.isEmpty() || !Arrays.equals(Word.word2hash(keystring), keyhash) ) {
prop.put("keystring", "<" + errmsg + ">");
}

@ -31,6 +31,7 @@
// if the shell's current path is HTROOT
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@ -41,7 +42,6 @@ import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.protocol.Domains;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.ByteBuffer;
import net.yacy.data.Diff;
import net.yacy.data.wiki.WikiBoard;
import net.yacy.peers.NewsPool;
@ -118,7 +118,7 @@ public class Wiki {
final Map<String, String> map = new HashMap<String, String>();
map.put("page", pagename);
map.put("author", author.replace(',', ' '));
if (!sb.isRobinsonMode() && post.get("content", "").trim().length() > 0 && !ByteBuffer.equals(page.page(), content)) {
if (!sb.isRobinsonMode() && post.get("content", "").trim().length() > 0 && !Arrays.equals(page.page(), content)) {
sb.peers.newsPool.publishMyNews(sb.peers.mySeed(), NewsPool.CATEGORY_WIKI_UPDATE, map);
}
page = newEntry;

@ -30,6 +30,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
@ -48,7 +49,6 @@ import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.sorting.ScoreMap;
import net.yacy.cora.sorting.WeakPriorityBlockingQueue;
import net.yacy.cora.storage.HandleSet;
import net.yacy.cora.util.ByteBuffer;
import net.yacy.cora.util.SpaceExceededException;
import net.yacy.gui.Audio;
import net.yacy.kelondro.data.meta.URIMetadataNode;
@ -368,7 +368,7 @@ public final class search {
// automatically attach the index abstract for the index that has the most references. This should be our target dht position
indexabstractContainercount += theSearch.abstractsCount(theSearch.getAbstractsMaxCountHash());
indexabstract.append("indexabstract.").append(ASCII.String(theSearch.getAbstractsMaxCountHash())).append("=").append(theSearch.abstractsString(theSearch.getAbstractsMaxCountHash())).append(serverCore.CRLF_STRING);
if ((theSearch.getAbstractsNearDHTHash() != null) && (!(ByteBuffer.equals(theSearch.getAbstractsNearDHTHash(), theSearch.getAbstractsMaxCountHash())))) {
if ((theSearch.getAbstractsNearDHTHash() != null) && (!(Arrays.equals(theSearch.getAbstractsNearDHTHash(), theSearch.getAbstractsMaxCountHash())))) {
// in case that the neardhthash is different from the maxcounthash attach also the neardhthash-container
indexabstractContainercount += theSearch.abstractsCount(theSearch.getAbstractsNearDHTHash());
indexabstract.append("indexabstract.").append(ASCII.String(theSearch.getAbstractsNearDHTHash())).append("=").append(theSearch.abstractsString(theSearch.getAbstractsNearDHTHash())).append(serverCore.CRLF_STRING);

@ -23,6 +23,7 @@ package net.yacy.cora.util;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@ -221,10 +222,20 @@ public final class ByteBuffer extends OutputStream {
return sb;
}
public static boolean equals(final byte[] buffer, final byte[] pattern) {
// compares two byte arrays: true, if pattern appears completely at offset position
if (buffer.length < pattern.length) return false;
for (int i = 0; i < pattern.length; i++) if (buffer[i] != pattern[i]) return false;
/**
* @param buffer the buffer to test. Must not be null.
* @param pattern a pattern to check for presence at the beginning of the buffer. Must not be null.
* @return true when the buffer starts with the given pattern
*/
public static boolean startsWith(final byte[] buffer, final byte[] pattern) {
if (buffer.length < pattern.length) {
return false;
}
for (int i = 0; i < pattern.length; i++) {
if (buffer[i] != pattern[i]) {
return false;
}
}
return true;
}
@ -241,7 +252,7 @@ public final class ByteBuffer extends OutputStream {
public static boolean contains(final Collection<byte[]> collection, final byte[] key) {
for (final byte[] v: collection) {
if (equals(v, key)) return true;
if (Arrays.equals(v, key)) return true;
}
return false;
}
@ -252,7 +263,7 @@ public final class ByteBuffer extends OutputStream {
int c = 0;
while (i.hasNext()) {
v = i.next();
if (equals(v, key)) {
if (Arrays.equals(v, key)) {
i.remove();
c++;
}

@ -5,6 +5,7 @@ package net.yacy;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
@ -15,7 +16,6 @@ import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.order.Base64Order;
import net.yacy.cora.order.CloneableIterator;
import net.yacy.cora.order.NaturalOrder;
import net.yacy.cora.util.ByteBuffer;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.cora.util.SpaceExceededException;
import net.yacy.kelondro.index.Index;
@ -73,7 +73,7 @@ public class dbtest {
final String s = UTF8.String(this.value).trim();
if (s.isEmpty()) return false;
final long source = Long.parseLong(s);
return ByteBuffer.equals(this.key, randomHash(source, source));
return Arrays.equals(this.key, randomHash(source, source));
}
public byte[] getKey() {
@ -340,13 +340,13 @@ public class dbtest {
if (entry == null)
System.out.println("missing value for entry " + UTF8.String(key) + " in test table");
else
if (!ByteBuffer.equals(entry.getColBytes(1, false), key)) System.out.println("wrong value for entry " + UTF8.String(key) + ": " + UTF8.String(entry.getColBytes(1, false)) + " in test table");
if (!Arrays.equals(entry.getColBytes(1, false), key)) System.out.println("wrong value for entry " + UTF8.String(key) + ": " + UTF8.String(entry.getColBytes(1, false)) + " in test table");
if (table_reference != null) {
entry = table_reference.get(key, false);
if (entry == null)
System.out.println("missing value for entry " + UTF8.String(key) + " in reference table");
else
if (!ByteBuffer.equals(entry.getColBytes(1, false), key)) System.out.println("wrong value for entry " + UTF8.String(key) + ": " + UTF8.String(entry.getColBytes(1, false)) + " in reference table");
if (!Arrays.equals(entry.getColBytes(1, false), key)) System.out.println("wrong value for entry " + UTF8.String(key) + ": " + UTF8.String(entry.getColBytes(1, false)) + " in reference table");
}
if (i % 1000 == 0) {

@ -29,6 +29,7 @@ package net.yacy.search.index;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
@ -303,7 +304,7 @@ public class Segment {
byte[] hh = new byte[6]; // host hash
System.arraycopy(refidh, 6, hh, 0, 6);
if (ByteBuffer.equals(hh, 0, id, 6, 6)) {
if (acceptSelfReference || !ByteBuffer.equals(refidh, id)) {
if (acceptSelfReference || !Arrays.equals(refidh, id)) {
internalIDs.put(refidh);
internal++;
}

@ -26,6 +26,7 @@
package net.yacy.search.ranking;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -36,7 +37,6 @@ import java.util.concurrent.Semaphore;
import net.yacy.cora.document.encoding.ASCII;
import net.yacy.cora.document.id.DigestURL;
import net.yacy.cora.sorting.ConcurrentScoreMap;
import net.yacy.cora.util.ByteBuffer;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.document.LargeNumberCache;
import net.yacy.document.Tokenizer;
@ -257,7 +257,7 @@ public class ReferenceOrder {
+ ((flags.get(Tokenizer.flag_cat_hasaudio)) ? 255 << this.ranking.coeff_cathasaudio : 0)
+ ((flags.get(Tokenizer.flag_cat_hasvideo)) ? 255 << this.ranking.coeff_cathasvideo : 0)
+ ((flags.get(Tokenizer.flag_cat_hasapp)) ? 255 << this.ranking.coeff_cathasapp : 0)
+ ((ByteBuffer.equals(t.getLanguage(), ASCII.getBytes(this.language))) ? 255 << this.ranking.coeff_language : 0);
+ ((Arrays.equals(t.getLanguage(), ASCII.getBytes(this.language))) ? 255 << this.ranking.coeff_language : 0);
//if (searchWords != null) r += (yacyURL.probablyWordURL(t.urlHash(), searchWords) != null) ? 256 << ranking.coeff_appurl : 0;

Loading…
Cancel
Save