Home | History | Annotate | Download | only in common
      1 // Copyright (C) 2009, International Business Machines
      2 // Corporation and others. All Rights Reserved.
      3 //
      4 // Copyright 2007 Google Inc. All Rights Reserved.
      5 // Author: sanjay (at) google.com (Sanjay Ghemawat)
      6 
      7 #include "unicode/utypes.h"
      8 #include "unicode/bytestream.h"
      9 #include "cmemory.h"
     10 
     11 U_NAMESPACE_BEGIN
     12 
     13 char* ByteSink::GetAppendBuffer(int32_t min_capacity,
     14                                 int32_t /*desired_capacity_hint*/,
     15                                 char* scratch, int32_t scratch_capacity,
     16                                 int32_t* result_capacity) {
     17   if (min_capacity < 1 || scratch_capacity < min_capacity) {
     18     *result_capacity = 0;
     19     return NULL;
     20   }
     21   *result_capacity = scratch_capacity;
     22   return scratch;
     23 }
     24 
     25 void ByteSink::Flush() {}
     26 
     27 CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
     28     : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity), size_(0), overflowed_(false) {
     29 }
     30 
     31 void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
     32   if (n <= 0) {
     33     return;
     34   }
     35   int32_t available = capacity_ - size_;
     36   if (n > available) {
     37     n = available;
     38     overflowed_ = true;
     39   }
     40   if (n > 0 && bytes != (outbuf_ + size_)) {
     41     uprv_memcpy(outbuf_ + size_, bytes, n);
     42   }
     43   size_ += n;
     44 }
     45 
     46 char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
     47                                             int32_t /*desired_capacity_hint*/,
     48                                             char* scratch,
     49                                             int32_t scratch_capacity,
     50                                             int32_t* result_capacity) {
     51   if (min_capacity < 1 || scratch_capacity < min_capacity) {
     52     *result_capacity = 0;
     53     return NULL;
     54   }
     55   int32_t available = capacity_ - size_;
     56   if (available >= min_capacity) {
     57     *result_capacity = available;
     58     return outbuf_ + size_;
     59   } else {
     60     *result_capacity = scratch_capacity;
     61     return scratch;
     62   }
     63 }
     64 
     65 U_NAMESPACE_END
     66