<commit_before>package com.turn.ttorrent.client.network;

import com.turn.ttorrent.client.SharedTorrent;
import com.turn.ttorrent.common.Peer;
import com.turn.ttorrent.common.PeersStorageFactory;
import com.turn.ttorrent.common.TorrentsStorageFactory;
import com.turn.ttorrent.common.protocol.PeerMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.text.ParseException;

public class WorkingReceiver implements DataProcessor {

  private static final Logger logger = LoggerFactory.getLogger(WorkingReceiver.class);

  private final String peerUID;
  private final PeersStorageFactory peersStorageFactory;
  private final TorrentsStorageFactory torrentsStorageFactory;
  private final ByteBuffer messageBytes;
  private int pstrLength;

  public WorkingReceiver(String uid, PeersStorageFactory peersStorageFactory, TorrentsStorageFactory torrentsStorageFactory) {
    this.peerUID = uid;
    this.peersStorageFactory = peersStorageFactory;
    this.torrentsStorageFactory = torrentsStorageFactory;
    this.messageBytes = ByteBuffer.allocate(2*1024*1024);
    this.pstrLength = -1;
  }

  @Override
  public DataProcessor processAndGetNext(SocketChannel socketChannel) throws IOException {
    logger.trace("received data from " + socketChannel.socket());
    messageBytes.limit(PeerMessage.MESSAGE_LENGTH_FIELD_SIZE);
    if (pstrLength == -1) {
      final int read = socketChannel.read(messageBytes);
      if (read < 0) {
        throw new EOFException(
                "Reached end-of-stream while reading size header");
      }
      if (messageBytes.hasRemaining()) {
        return this;
      }
      this.pstrLength = messageBytes.getInt(0);
    }

    if (PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength > messageBytes.capacity()){
      logger.debug("Proposed limit of {} is larger than capacity of {}",
              PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength, messageBytes.capacity());
    }
    messageBytes.limit(PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength);

    socketChannel.read(messageBytes);
    if (messageBytes.hasRemaining()) {
      return this;
    }
    messageBytes.rewind();
    try {
      Peer peer = peersStorageFactory.getPeersStorage().getPeer(peerUID);
      SharedTorrent torrent = torrentsStorageFactory.getTorrentsStorage().getTorrent(peer.getHexInfoHash());
      PeerMessage message = PeerMessage.parse(messageBytes, torrent);
      // TODO: 11/13/17 process message
    } catch (ParseException e) {
      logger.debug("{}", e.getMessage());
    }
    return this;
  }
}<commit_msg>fixed error that buffer doesn't clear after build message<commit_after>package com.turn.ttorrent.client.network;

import com.turn.ttorrent.client.SharedTorrent;
import com.turn.ttorrent.common.Peer;
import com.turn.ttorrent.common.PeersStorageFactory;
import com.turn.ttorrent.common.TorrentsStorageFactory;
import com.turn.ttorrent.common.protocol.PeerMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.text.ParseException;

public class WorkingReceiver implements DataProcessor {

  private static final Logger logger = LoggerFactory.getLogger(WorkingReceiver.class);

  private final String peerUID;
  private final PeersStorageFactory peersStorageFactory;
  private final TorrentsStorageFactory torrentsStorageFactory;
  private final ByteBuffer messageBytes;
  private int pstrLength;

  public WorkingReceiver(String uid, PeersStorageFactory peersStorageFactory, TorrentsStorageFactory torrentsStorageFactory) {
    this.peerUID = uid;
    this.peersStorageFactory = peersStorageFactory;
    this.torrentsStorageFactory = torrentsStorageFactory;
    this.messageBytes = ByteBuffer.allocate(2 * 1024 * 1024);
    this.pstrLength = -1;
  }

  @Override
  public DataProcessor processAndGetNext(SocketChannel socketChannel) throws IOException {
    logger.trace("received data from " + socketChannel.socket());
    messageBytes.limit(PeerMessage.MESSAGE_LENGTH_FIELD_SIZE);
    if (pstrLength == -1) {
      final int read = socketChannel.read(messageBytes);
      if (read < 0) {
        throw new EOFException(
                "Reached end-of-stream while reading size header");
      }
      if (messageBytes.hasRemaining()) {
        return this;
      }
      this.pstrLength = messageBytes.getInt(0);
    }

    if (PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength > messageBytes.capacity()) {
      logger.debug("Proposed limit of {} is larger than capacity of {}",
              PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength, messageBytes.capacity());
    }
    messageBytes.limit(PeerMessage.MESSAGE_LENGTH_FIELD_SIZE + this.pstrLength);

    socketChannel.read(messageBytes);
    if (messageBytes.hasRemaining()) {
      return this;
    }
    messageBytes.rewind();
    this.pstrLength = -1;
    try {
      Peer peer = peersStorageFactory.getPeersStorage().getPeer(peerUID);
      SharedTorrent torrent = torrentsStorageFactory.getTorrentsStorage().getTorrent(peer.getHexInfoHash());
      PeerMessage message = PeerMessage.parse(messageBytes, torrent);
      logger.trace("get message {} from {}", new Object[]{message, socketChannel.socket()});
      peersStorageFactory.getPeersStorage().getSharingPeer(peer).handleMessage(message);
    } catch (ParseException e) {
      logger.debug("{}", e.getMessage());
    }
    this.messageBytes.rewind();
    return this;
  }
}
{'fraction_non_alphanumeric': 0.09606215786685503, 'fraction_numerical': 0.006533639413738301, 'mean_word_length': 4.288515406162465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4824272', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1580, 'n_words': 326}