Tunnels: Simplify TunnelId and HopConfig to save space

and reduce object churn and duplication
Fixup tests, javadocs, logging as required
This commit is contained in:
zzz
2020-11-07 13:40:48 +00:00
parent e18708bdbe
commit 5bafdd05a9
14 changed files with 134 additions and 114 deletions

View File

@ -22,9 +22,15 @@ import java.io.OutputStream;
* as the DatabaseStoreMessage uses a zero ID to request
* a direct reply.
*
* 4 bytes, usually of random data.
*
* Not recommended for external use, subject to change.
*
* As of 0.9.48, does NOT extend DataStructureImpl, to save space
*
* @author jrandom
*/
public class TunnelId extends DataStructureImpl {
public class TunnelId {
private long _tunnelId;
public static final long MAX_ID_VALUE = 0xffffffffL;
@ -62,26 +68,6 @@ public class TunnelId extends DataStructureImpl {
DataHelper.writeLong(out, 4, _tunnelId);
}
/**
* Overridden for efficiency.
*/
@Override
public byte[] toByteArray() {
return DataHelper.toLong(4, _tunnelId);
}
/**
* Overridden for efficiency.
* @param data non-null
* @throws DataFormatException if null or wrong length
*/
@Override
public void fromByteArray(byte data[]) throws DataFormatException {
if (data == null) throw new DataFormatException("Null data passed in");
if (data.length != 4) throw new DataFormatException("Bad data length");
_tunnelId = (int) DataHelper.fromLong(data, 0, 4);
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;

View File

@ -16,9 +16,21 @@ package net.i2p.data;
*/
public class TunnelIdTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
TunnelId id = new TunnelId();
TunnelIdStructure id = new TunnelIdStructure();
id.setTunnelId(42);
return id;
}
public DataStructure createStructureToRead() { return new TunnelId(); }
public DataStructure createStructureToRead() { return new TunnelIdStructure(); }
/**
* so we can test it as a structure
* @since 0.9.48 TunnelId no longer extends DataStructureImpl
*/
private class TunnelIdStructure extends TunnelId implements DataStructure {
public Hash calculateHash() { return null; }
public void fromByteArray(byte[] in) {}
public byte[] toByteArray() { return null; }
public void fromBase64(String in) {}
public String toBase64() { return null; }
}
}