i2psnark: Cache length of metainfo

This commit is contained in:
zzz
2020-10-10 14:59:13 +00:00
parent 9e36fe090c
commit 98e5908557
3 changed files with 21 additions and 4 deletions

View File

@ -49,7 +49,7 @@ class MagnetState {
infohash = iHash;
if (meta != null) {
metainfo = meta;
initialize(meta.getInfoBytes().length);
initialize(meta.getInfoBytesLength());
complete = true;
}
}

View File

@ -68,6 +68,7 @@ public class MetaInfo
private final String created_by;
private final long creation_date;
private Map<String, BEValue> infoMap;
private int infoBytesLength;
/**
* Called by Storage when creating a new torrent from local data
@ -622,11 +623,27 @@ public class MetaInfo
return BEncoder.bencode(m);
}
/** @since 0.8.4 */
/**
* Side effect: Caches infoBytesLength.
* @since 0.8.4
*/
public synchronized byte[] getInfoBytes() {
if (infoMap == null)
createInfoMap();
return BEncoder.bencode(infoMap);
byte[] rv = BEncoder.bencode(infoMap);
infoBytesLength = rv.length;
return rv;
}
/**
* The size of getInfoBytes().
* Cached.
* @since 0.9.48
*/
public synchronized int getInfoBytesLength() {
if (infoBytesLength > 0)
return infoBytesLength;
return getInfoBytes().length;
}
/** @return an unmodifiable view of the Map */

View File

@ -290,7 +290,7 @@ public class Peer implements Comparable<Peer>
if ((options & OPTION_EXTENSION) != 0) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Peer supports extensions, sending reply message");
int metasize = metainfo != null ? metainfo.getInfoBytes().length : -1;
int metasize = metainfo != null ? metainfo.getInfoBytesLength() : -1;
boolean pexAndMetadata = metainfo == null || !metainfo.isPrivate();
boolean dht = util.getDHT() != null;
boolean comment = util.utCommentsEnabled();