/projects/azureus-4.7.0.2/com/aelitis/azureus/core/dht/transport/udp/impl/DHTUDPPacketReplyQueryStorage.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 193 lines · 116 code · 53 blank · 24 comment · 16 complexity · c92417c64f8bcc7892152daa7eaff271 MD5 · raw file

  1. /*
  2. * File : PRUDPPacketReplyConnect.java
  3. * Created : 20-Jan-2004
  4. * By : parg
  5. *
  6. * Azureus - a Java Bittorrent client
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details ( see the LICENSE file ).
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. package com.aelitis.azureus.core.dht.transport.udp.impl;
  22. /**
  23. * @author parg
  24. *
  25. */
  26. import java.util.*;
  27. import java.io.*;
  28. import java.net.InetSocketAddress;
  29. import com.aelitis.azureus.core.dht.transport.DHTTransportContact;
  30. import com.aelitis.azureus.core.dht.transport.udp.impl.packethandler.DHTUDPPacketNetworkHandler;
  31. public class
  32. DHTUDPPacketReplyQueryStorage
  33. extends DHTUDPPacketReply
  34. {
  35. private int random_id;
  36. private int header_length;
  37. private List<byte[]> response;
  38. public
  39. DHTUDPPacketReplyQueryStorage(
  40. DHTTransportUDPImpl transport,
  41. int trans_id,
  42. long conn_id,
  43. DHTTransportContact local_contact,
  44. DHTTransportContact remote_contact )
  45. {
  46. super( transport, DHTUDPPacketHelper.ACT_REPLY_QUERY_STORE, trans_id, conn_id, local_contact, remote_contact );
  47. }
  48. protected
  49. DHTUDPPacketReplyQueryStorage(
  50. DHTUDPPacketNetworkHandler network_handler,
  51. InetSocketAddress originator,
  52. DataInputStream is,
  53. int trans_id )
  54. throws IOException
  55. {
  56. super( network_handler, originator, is, DHTUDPPacketHelper.ACT_REPLY_QUERY_STORE, trans_id );
  57. short size = is.readShort();
  58. response = new ArrayList<byte[]>( size );
  59. if ( size > 0 ){
  60. header_length = is.readByte()&0xff;
  61. byte[] bitmap = new byte[size+7/8];
  62. is.read( bitmap );
  63. int pos = 0;
  64. int current = 0;
  65. for (int i=0;i<size;i++){
  66. if ( i % 8 == 0 ){
  67. current = bitmap[pos++]&0xff;
  68. }
  69. if (( current&0x80)!=0 ){
  70. byte[] x = new byte[header_length];
  71. is.read( x );
  72. response.add( x );
  73. }else{
  74. response.add( null );
  75. }
  76. current <<= 1;
  77. }
  78. }
  79. }
  80. public void
  81. serialise(
  82. DataOutputStream os )
  83. throws IOException
  84. {
  85. super.serialise(os);
  86. int size = response.size();
  87. os.writeShort( size );
  88. if ( size > 0 ){
  89. os.writeByte( header_length );
  90. byte[] bitmap = new byte[size+7/8];
  91. int bitmap_pos = 0;
  92. int current_byte = 0;
  93. int pos = 0;
  94. for ( byte[] x: response ){
  95. current_byte = current_byte << 1;
  96. if ( x != null){
  97. current_byte += 1;
  98. }
  99. if (( pos %8 ) == 7 ){
  100. bitmap[bitmap_pos++] = (byte)current_byte;
  101. current_byte = 0;
  102. }
  103. pos++;
  104. }
  105. if (( pos % 8 ) != 0 ){
  106. bitmap[bitmap_pos++] = (byte)(current_byte << (8 - (pos % 8)));
  107. }
  108. os.write( bitmap );
  109. for ( byte[] x: response ){
  110. if ( x != null ){
  111. os.write( x );
  112. }
  113. }
  114. }
  115. }
  116. protected void
  117. setRandomID(
  118. int id )
  119. {
  120. random_id = id;
  121. }
  122. protected int
  123. getRandomID()
  124. {
  125. return( random_id );
  126. }
  127. protected void
  128. setResponse(
  129. int _header_length,
  130. List<byte[]> _response )
  131. {
  132. header_length = _header_length;
  133. response = _response;
  134. }
  135. protected List<byte[]>
  136. getResponse()
  137. {
  138. return( response );
  139. }
  140. }