Search in sources :

Example 6 with InvalidBEncodingException

use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.

the class PeerCoordinator method sendCommentReq.

/**
 *  Send a commment request message to the peer, if he supports it.
 *  @since 0.9.31
 */
void sendCommentReq(Peer peer) {
    Map<String, BEValue> handshake = peer.getHandshakeMap();
    if (handshake == null)
        return;
    BEValue bev = handshake.get("m");
    if (bev == null)
        return;
    // unless forced at handshake time (see above)
    try {
        if (bev.getMap().get(ExtensionHandler.TYPE_COMMENT) != null) {
            int sz = 0;
            CommentSet comments = snark.getComments();
            if (comments != null) {
                synchronized (comments) {
                    sz = comments.size();
                }
            }
            if (sz >= CommentSet.MAX_SIZE)
                return;
            ExtensionHandler.sendCommentReq(peer, CommentSet.MAX_SIZE - sz);
        }
    } catch (InvalidBEncodingException ibee) {
    }
}
Also used : InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) CommentSet(org.klomp.snark.comments.CommentSet) BEValue(org.klomp.snark.bencode.BEValue)

Example 7 with InvalidBEncodingException

use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.

the class MetaInfo method getNameAndInfoHash.

/**
 * Efficiently returns the name and the 20 byte SHA1 hash of the info dictionary in a torrent file
 * Caller must close stream.
 *
 * @param infoHashOut 20-byte out parameter
 * @since 0.8.5
 */
public static String getNameAndInfoHash(InputStream in, byte[] infoHashOut) throws IOException {
    BDecoder bd = new BDecoder(in);
    Map<String, BEValue> m = bd.bdecodeMap().getMap();
    BEValue ibev = m.get("info");
    if (ibev == null)
        throw new InvalidBEncodingException("Missing info map");
    Map<String, BEValue> i = ibev.getMap();
    BEValue rvbev = i.get("name");
    if (rvbev == null)
        throw new InvalidBEncodingException("Missing name");
    byte[] h = bd.get_special_map_digest();
    System.arraycopy(h, 0, infoHashOut, 0, 20);
    return rvbev.getString();
}
Also used : InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) BDecoder(org.klomp.snark.bencode.BDecoder) BEValue(org.klomp.snark.bencode.BEValue)

Aggregations

InvalidBEncodingException (org.klomp.snark.bencode.InvalidBEncodingException)7 BEValue (org.klomp.snark.bencode.BEValue)6 ArrayList (java.util.ArrayList)2 BDecoder (org.klomp.snark.bencode.BDecoder)2 DHT (org.klomp.snark.dht.DHT)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 I2PSessionException (net.i2p.client.I2PSessionException)1 I2PInvalidDatagramException (net.i2p.client.datagram.I2PInvalidDatagramException)1 DataFormatException (net.i2p.data.DataFormatException)1 Hash (net.i2p.data.Hash)1 ConvertToHash (net.i2p.util.ConvertToHash)1