Search in sources :

Example 26 with DataFormatException

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();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 27 with DataFormatException

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();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 28 with DataFormatException

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);
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 29 with DataFormatException

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);
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Payload(net.i2p.data.Payload)

Example 30 with DataFormatException

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();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8