Mega Code Archive

 
Categories / Android / Date Type
 

Generate the client id, which is a fixed string of length 8 concatenated with 12 random bytes

import java.nio.ByteBuffer; import java.util.Random; /*  * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol  * This project contains two packages:  * 1. jBittorrentAPI is the "client" part, i.e. it implements all classes needed to publish  *    files, share them and download them.  *    This package also contains example classes on how a developer could create new applications.  * 2. trackerBT is the "tracker" part, i.e. it implements a all classes needed to run  *    a Bittorrent tracker that coordinates peers exchanges. *  *  * Copyright (C) 2007 Baptiste Dubuis, Artificial Intelligence Laboratory, EPFL  *  * This file is part of jbittorrentapi-v1.0.zip  *  * Java Bittorrent API is free software and a free user study set-up;  * you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * Java Bittorrent API is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with Java Bittorrent API; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *  * @version 1.0  * @author Baptiste Dubuis  * To contact the author:  * email: baptiste.dubuis@gmail.com  *  * More information about Java Bittorrent API:  *    http://sourceforge.net/projects/bitext/  */ //package atorrentapi; class Main {   /**    * Generate the client id, which is a fixed string of length 8 concatenated    * with 12 random bytes    *     * @return byte[]    */   public static byte[] generateID() {     byte[] id = new byte[12];     Random r = new Random(System.currentTimeMillis());     r.nextBytes(id);     return concat("-ANDRTR-".getBytes(), id);   }   /**    * Concatenate the 2 byte arrays    *     * @param a    *            byte[]    * @param b    *            byte[]    * @return byte[]    */   public static byte[] concat2(byte[] a, byte[] b) {     ByteBuffer bb = ByteBuffer.allocate(a.length + b.length);     bb.put(a);     bb.put(b);     return bb.array();   }   /**    * Concatenate the 2 byte arrays    *     * @param b1    *            byte[]    * @param b2    *            byte[]    * @return byte[]    */   public static byte[] concat(byte[] b1, byte[] b2) {     // Log.v("AndroidTor","size of the new byte array is "+(b1.length +     // b2.length));     byte[] b3 = new byte[b1.length + b2.length];     System.arraycopy(b1, 0, b3, 0, b1.length);     System.arraycopy(b2, 0, b3, b1.length, b2.length);     return b3;   }   /**    * Concatenate the byte array and the byte    *     * @param b1    *            byte[]    * @param b2    *            byte    * @return byte[]    */   public static byte[] concat(byte[] b1, byte b2) {     byte[] b3 = new byte[b1.length + 1];     byte[] temp = new byte[] { b2 };     System.arraycopy(b1, 0, b3, 0, b1.length);     System.arraycopy(temp, 0, b3, b1.length, 1);     return b3;   } }