<commit_before>/*
 * Copyright (c) 2007-2009 Digital Bazaar, Inc. All rights reserved.
 */
#include "db/net/ConnectionInputStream.h"

#include "db/net/Connection.h"
#include "db/util/Math.h"

#include <cstring>

using namespace std;
using namespace db::io;
using namespace db::net;
using namespace db::rt;
using namespace db::util;

#define MAX_READ_SIZE   1023

ConnectionInputStream::ConnectionInputStream(Connection* c) :
   mConnection(c),
   mBytesRead(0),
   mPeekBuffer(0),
   mPeeking(false)
{
}

ConnectionInputStream::~ConnectionInputStream()
{
}

inline int ConnectionInputStream::read(char* b, int length)
{
   int rval = 0;

   if(mPeeking || mPeekBuffer.isEmpty())
   {
      // throttle the read as appropriate
      BandwidthThrottler* bt = mConnection->getBandwidthThrottler(true);
      if(bt != NULL)
      {
         bt->requestBytes(length, length);
      }

      // read from the socket input stream
      InputStream* is = mConnection->getSocket()->getInputStream();
      if(is != NULL)
      {
         rval = is->read(b, length);
      }
      else
      {
         ExceptionRef e = new Exception(
            "Could not read from connection. Socket closed.",
            "db.net.Socket.Closed");
         Exception::set(e);
         rval = -1;
      }
   }
   else
   {
      // read from peek buffer
      rval = mPeekBuffer.get(b, length);
   }

   if(rval > 0 && !mPeeking)
   {
      // update bytes read (reset as necessary)
      if(mBytesRead > Math::HALF_MAX_LONG_VALUE)
      {
         mBytesRead = 0;
      }

      mBytesRead += rval;
   }

   return rval;
}

int ConnectionInputStream::readFully(char* b, int length)
{
   int rval = 0;

   // keep reading until eos, error, or length reached
   int remaining = length;
   int offset = 0;
   int numBytes = 0;
   while(remaining > 0 && (numBytes = read(b + offset, remaining)) > 0)
   {
      remaining -= numBytes;
      offset += numBytes;
      rval += numBytes;
   }

   if(numBytes == -1)
   {
      rval = -1;
   }

   return rval;
}

int ConnectionInputStream::readLine(string& line)
{
   int rval = 0;

   line.erase();

   // read one character at a time
   char c;
   int numBytes;
   while((numBytes = read(&c, 1)) > 0 && c != '\n')
   {
      // see if the character is a carriage return
      if(c == '\r')
      {
         // see if the next character is an eol -- and we've found a CRLF
         if(peek(&c, 1) > 0 && c == '\n')
         {
            // read the character in and discard it
            read(&c, 1);
         }

         // set character to an eol since a carriage return is treated the same
         c = '\n';
      }
      else
      {
         // append the character
         line.push_back(c);

         // a character was appended, so not end of stream
         rval = 1;
      }
   }

   if(numBytes == -1)
   {
      rval = -1;
   }

   return rval;
}

int ConnectionInputStream::readCrlf(string& line)<commit_msg>Minor optimization.<commit_after>/*
 * Copyright (c) 2007-2009 Digital Bazaar, Inc. All rights reserved.
 */
#include "db/net/ConnectionInputStream.h"

#include "db/net/Connection.h"
#include "db/util/Math.h"

#include <cstring>

using namespace std;
using namespace db::io;
using namespace db::net;
using namespace db::rt;
using namespace db::util;

#define MAX_READ_SIZE   1023

ConnectionInputStream::ConnectionInputStream(Connection* c) :
   mConnection(c),
   mBytesRead(0),
   mPeekBuffer(0),
   mPeeking(false)
{
}

ConnectionInputStream::~ConnectionInputStream()
{
}

inline int ConnectionInputStream::read(char* b, int length)
{
   int rval = 0;

   if(mPeeking || mPeekBuffer.isEmpty())
   {
      // throttle the read as appropriate
      BandwidthThrottler* bt = mConnection->getBandwidthThrottler(true);
      if(bt != NULL)
      {
         bt->requestBytes(length, length);
      }

      // read from the socket input stream
      InputStream* is = mConnection->getSocket()->getInputStream();
      if(is != NULL)
      {
         rval = is->read(b, length);
      }
      else
      {
         ExceptionRef e = new Exception(
            "Could not read from connection. Socket closed.",
            "db.net.Socket.Closed");
         Exception::set(e);
         rval = -1;
      }
   }
   else
   {
      // read from peek buffer
      rval = mPeekBuffer.get(b, length);
   }

   if(rval > 0 && !mPeeking)
   {
      // update bytes read (reset as necessary)
      if(mBytesRead > Math::HALF_MAX_LONG_VALUE)
      {
         mBytesRead = 0;
      }

      mBytesRead += rval;
   }

   return rval;
}

int ConnectionInputStream::readFully(char* b, int length)
{
   int rval = 0;

   // keep reading until eos, error, or length reached
   int remaining = length;
   int offset = 0;
   int numBytes = 0;
   while(remaining > 0 && (numBytes = read(b + offset, remaining)) > 0)
   {
      remaining -= numBytes;
      offset += numBytes;
      rval += numBytes;
   }

   if(numBytes == -1)
   {
      rval = -1;
   }

   return rval;
}

int ConnectionInputStream::readLine(string& line)
{
   int rval = 0;

   line.erase();

   // read one character at a time
   char c;
   int numBytes;
   while((numBytes = read(&c, 1)) > 0 && c != '\n')
   {
      // see if the character is a carriage return
      if(c == '\r')
      {
         // see if the next character is an eol -- and we've found a CRLF
         if(peek(&c, 1) > 0 && c == '\n')
         {
            // discard the character
            mPeekBuffer.clear(1);
         }

         // set character to an eol since a carriage return is treated the same
         c = '\n';
      }
      else
      {
         // append the character
         line.push_back(c);

         // a character was appended, so not end of stream
         rval = 1;
      }
   }

   if(numBytes == -1)
   {
      rval = -1;
   }

   return rval;
}

int ConnectionInputStream::readCrlf(string& line)
{'fraction_non_alphanumeric': 0.12432065217391304, 'fraction_numerical': 0.01154891304347826, 'mean_word_length': 2.0913385826771655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '2866135', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1882, 'n_words': 525}