forked from I2P_Developers/i2p.i2p
2004-11-02 jrandom
* Fixed up the configuration overrides for the streaming socket lib integration so that it properly honors env settings. * More memory usage streamlining (last major revamp for now, i promise)
This commit is contained in:
@ -104,13 +104,21 @@ public class Certificate extends DataStructureImpl {
|
||||
return cur - offset;
|
||||
}
|
||||
|
||||
public int readBytes(byte source[], int offset) {
|
||||
public int readBytes(byte source[], int offset) throws DataFormatException {
|
||||
if (source == null) throw new DataFormatException("Cert is null");
|
||||
if (source.length <= offset + 3)
|
||||
throw new DataFormatException("Cert is too small [" + source.length + " off=" + offset + "]");
|
||||
|
||||
int cur = offset;
|
||||
_type = (int)DataHelper.fromLong(source, cur, 1);
|
||||
cur++;
|
||||
int length = (int)DataHelper.fromLong(source, cur, 2);
|
||||
cur += 2;
|
||||
if (length > 0) {
|
||||
if (length + cur > source.length)
|
||||
throw new DataFormatException("Payload on the certificate is insufficient (len="
|
||||
+ source.length + " off=" + offset + " cur=" + cur
|
||||
+ " payloadLen=" + length);
|
||||
_payload = new byte[length];
|
||||
System.arraycopy(source, cur, _payload, 0, length);
|
||||
cur += length;
|
||||
|
@ -10,6 +10,7 @@ package net.i2p.data;
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
@ -255,8 +256,9 @@ public class DataHelper {
|
||||
public static void writeLong(OutputStream rawStream, int numBytes, long value)
|
||||
throws DataFormatException, IOException {
|
||||
try {
|
||||
UnsignedInteger i = new UnsignedInteger(value);
|
||||
rawStream.write(i.getBytes(numBytes));
|
||||
UnsignedInteger.writeBytes(rawStream, numBytes, value);
|
||||
//UnsignedInteger i = new UnsignedInteger(value);
|
||||
//rawStream.write(i.getBytes(numBytes));
|
||||
} catch (IllegalArgumentException iae) {
|
||||
throw new DataFormatException("Invalid value (must be positive)", iae);
|
||||
}
|
||||
@ -340,7 +342,7 @@ public class DataHelper {
|
||||
|
||||
return new Date(date);
|
||||
}
|
||||
|
||||
|
||||
/** Write out a date to the stream as specified by the I2P data structure spec.
|
||||
* @param out stream to write to
|
||||
* @param date date to write (can be null)
|
||||
@ -360,12 +362,18 @@ public class DataHelper {
|
||||
else
|
||||
return toLong(DATE_LENGTH, date.getTime());
|
||||
}
|
||||
public static Date fromDate(byte src[], int offset) throws IllegalArgumentException {
|
||||
long when = fromLong(src, offset, DATE_LENGTH);
|
||||
if (when <= 0)
|
||||
return null;
|
||||
else
|
||||
return new Date(when);
|
||||
public static Date fromDate(byte src[], int offset) throws DataFormatException {
|
||||
if ( (src == null) || (offset + DATE_LENGTH > src.length) )
|
||||
throw new DataFormatException("Not enough data to read a date");
|
||||
try {
|
||||
long when = fromLong(src, offset, DATE_LENGTH);
|
||||
if (when <= 0)
|
||||
return null;
|
||||
else
|
||||
return new Date(when);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
throw new DataFormatException(iae.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static final int DATE_LENGTH = 8;
|
||||
|
@ -90,7 +90,10 @@ public class Destination extends DataStructureImpl {
|
||||
return cur - offset;
|
||||
}
|
||||
|
||||
public int readBytes(byte source[], int offset) {
|
||||
public int readBytes(byte source[], int offset) throws DataFormatException {
|
||||
if (source == null) throw new DataFormatException("Null source");
|
||||
if (source.length <= offset + PublicKey.KEYSIZE_BYTES + SigningPublicKey.KEYSIZE_BYTES)
|
||||
throw new DataFormatException("Not enough data (len=" + source.length + " off=" + offset + ")");
|
||||
int cur = offset;
|
||||
|
||||
_publicKey = new PublicKey();
|
||||
|
@ -378,7 +378,11 @@ public class RouterInfo extends DataStructureImpl {
|
||||
public synchronized void readBytes(InputStream in) throws DataFormatException, IOException {
|
||||
_identity = new RouterIdentity();
|
||||
_identity.readBytes(in);
|
||||
_published = DataHelper.readDate(in).getTime();
|
||||
Date when = DataHelper.readDate(in);
|
||||
if (when == null)
|
||||
_published = 0;
|
||||
else
|
||||
_published = when.getTime();
|
||||
int numAddresses = (int) DataHelper.readLong(in, 1);
|
||||
for (int i = 0; i < numAddresses; i++) {
|
||||
RouterAddress address = new RouterAddress();
|
||||
@ -402,7 +406,7 @@ public class RouterInfo extends DataStructureImpl {
|
||||
|
||||
public synchronized void writeBytes(OutputStream out) throws DataFormatException, IOException {
|
||||
if (_identity == null) throw new DataFormatException("Missing identity");
|
||||
if (_published <= 0) throw new DataFormatException("Invalid published date: " + _published);
|
||||
if (_published < 0) throw new DataFormatException("Invalid published date: " + _published);
|
||||
if (_signature == null) throw new DataFormatException("Signature is null");
|
||||
//if (!isValid())
|
||||
// throw new DataFormatException("Data is not valid");
|
||||
|
@ -9,6 +9,8 @@ package net.i2p.data;
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import net.i2p.util.Log;
|
||||
@ -189,6 +191,16 @@ public class UnsignedInteger {
|
||||
System.arraycopy(_data, 0, data, numBytes - _data.length, _data.length);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
public static void writeBytes(OutputStream rawStream, int numBytes, long value)
|
||||
throws DataFormatException, IOException {
|
||||
if (value < 0) throw new DataFormatException("Invalid value (" + value + ")");
|
||||
for (int i = numBytes - 1; i >= 0; i--) {
|
||||
byte cur = (byte)( (value >>> (i*8) ) & 0xFF);
|
||||
rawStream.write(cur);
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger getBigInteger() {
|
||||
return new BigInteger(1, _data);
|
||||
@ -238,6 +250,7 @@ public class UnsignedInteger {
|
||||
testNum(1024 * 1024 * 1024 * 4L + 1L);
|
||||
_log.debug("Testing MaxLong");
|
||||
testNum(Long.MAX_VALUE);
|
||||
testWrite();
|
||||
} catch (Throwable t) { t.printStackTrace(); }
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
@ -260,4 +273,18 @@ public class UnsignedInteger {
|
||||
BigInteger tbi = new BigInteger(1, calculateBytes(num));
|
||||
_log.debug(num + " As a shifted : 0x" + tbi.toString(16));
|
||||
}
|
||||
|
||||
private static void testWrite() throws Exception {
|
||||
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(8);
|
||||
UnsignedInteger i = new UnsignedInteger(12345);
|
||||
baos.write(i.getBytes(8));
|
||||
byte v1[] = baos.toByteArray();
|
||||
baos.reset();
|
||||
UnsignedInteger.writeBytes(baos, 8, 12345);
|
||||
byte v2[] = baos.toByteArray();
|
||||
System.out.println("v1 len: " + v1.length + " v2 len: " + v2.length);
|
||||
System.out.println("v1: " + DataHelper.toHexString(v1));
|
||||
System.out.println("v2: " + DataHelper.toHexString(v2));
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user