use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class HostLookupMessage method doWriteMessage.
protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
int len;
if (_lookupType == LOOKUP_HASH) {
if (_hash == null)
throw new I2CPMessageException("Unable to write out the message as there is not enough data");
len = 11 + Hash.HASH_LENGTH;
} else if (_lookupType == LOOKUP_HOST) {
if (_host == null)
throw new I2CPMessageException("Unable to write out the message as there is not enough data");
len = 12 + _host.length();
} else {
throw new I2CPMessageException("bad type");
}
ByteArrayOutputStream os = new ByteArrayOutputStream(len);
try {
_sessionId.writeBytes(os);
DataHelper.writeLong(os, 4, _reqID);
DataHelper.writeLong(os, 4, _timeout);
DataHelper.writeLong(os, 1, _lookupType);
if (_lookupType == LOOKUP_HASH) {
_hash.writeBytes(os);
} else {
DataHelper.writeString(os, _host);
}
} catch (DataFormatException dfe) {
throw new I2CPMessageException("bad data", dfe);
}
return os.toByteArray();
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class HostReplyMessage method doWriteMessage.
protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
int len = 7;
if (_code == RESULT_SUCCESS) {
if (_dest == null)
throw new I2CPMessageException("Unable to write out the message as there is not enough data");
len += _dest.size();
}
ByteArrayOutputStream os = new ByteArrayOutputStream(len);
try {
_sessionId.writeBytes(os);
DataHelper.writeLong(os, 4, _reqID);
DataHelper.writeLong(os, 1, _code);
if (_code == RESULT_SUCCESS)
_dest.writeBytes(os);
} catch (DataFormatException dfe) {
throw new I2CPMessageException("bad data", dfe);
}
return os.toByteArray();
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class I2CPMessageHandler method readMessage.
/**
* Read an I2CPMessage from the stream and return the fully populated object.
*
* @param in I2CP input stream
* @return Fully populated I2CPMessage
* @throws IOException if there is an IO problem reading from the stream
* @throws I2CPMessageException if there is a problem handling the particular
* message - if it is an unknown type or has improper formatting, etc.
*/
public static I2CPMessage readMessage(InputStream in) throws IOException, I2CPMessageException {
int length;
try {
length = (int) DataHelper.readLong(in, 4);
} catch (DataFormatException dfe) {
throw new IOException("Connection closed");
}
if (length > MAX_LENGTH)
throw new I2CPMessageException("Invalid message length specified");
try {
int type = (int) DataHelper.readLong(in, 1);
I2CPMessage msg = createMessage(type);
// Note that the readMessage() calls don't, in general, read and discard
// extra data, so we can't add new fields to the end of messages
// in a compatible way. And the readers could read beyond the length too.
// To fix this we'd have to read into a BAOS/BAIS or use a filter input stream
msg.readMessage(in, length, type);
return msg;
} catch (DataFormatException dfe) {
throw new I2CPMessageException("Error reading the message", dfe);
}
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class MessagePayloadMessage method doReadMessage.
@Override
protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException {
try {
_sessionId = (int) DataHelper.readLong(in, 2);
_messageId = DataHelper.readLong(in, 4);
_payload = new Payload();
_payload.readBytes(in);
} catch (DataFormatException dfe) {
throw new I2CPMessageException("Unable to load the message data", dfe);
}
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class CreateLeaseSetMessage method doWriteMessage.
@Override
protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
if ((_sessionId == null) || (_signingPrivateKey == null) || (_privateKey == null) || (_leaseSet == null))
throw new I2CPMessageException("Unable to write out the message as there is not enough data");
int size = // sessionId
4 + _signingPrivateKey.length() + PrivateKey.KEYSIZE_BYTES + _leaseSet.size();
ByteArrayOutputStream os = new ByteArrayOutputStream(size);
try {
_sessionId.writeBytes(os);
_signingPrivateKey.writeBytes(os);
_privateKey.writeBytes(os);
_leaseSet.writeBytes(os);
} catch (DataFormatException dfe) {
throw new I2CPMessageException("Error writing out the message data", dfe);
}
return os.toByteArray();
}
Aggregations