1 // Copyright (C) 2009-2010, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 //
4 // Copyright 2007 Google Inc. All Rights Reserved.
5 // Author: sanjay@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
GetAppendBuffer(int32_t min_capacity,int32_t,char * scratch,int32_t scratch_capacity,int32_t * result_capacity)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
Flush()25 void ByteSink::Flush() {}
26
CheckedArrayByteSink(char * outbuf,int32_t capacity)27 CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
28 : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
29 size_(0), appended_(0), overflowed_(FALSE) {
30 }
31
Reset()32 CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
33 size_ = appended_ = 0;
34 overflowed_ = FALSE;
35 return *this;
36 }
37
Append(const char * bytes,int32_t n)38 void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
39 if (n <= 0) {
40 return;
41 }
42 appended_ += n;
43 int32_t available = capacity_ - size_;
44 if (n > available) {
45 n = available;
46 overflowed_ = TRUE;
47 }
48 if (n > 0 && bytes != (outbuf_ + size_)) {
49 uprv_memcpy(outbuf_ + size_, bytes, n);
50 }
51 size_ += n;
52 }
53
GetAppendBuffer(int32_t min_capacity,int32_t,char * scratch,int32_t scratch_capacity,int32_t * result_capacity)54 char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
55 int32_t /*desired_capacity_hint*/,
56 char* scratch,
57 int32_t scratch_capacity,
58 int32_t* result_capacity) {
59 if (min_capacity < 1 || scratch_capacity < min_capacity) {
60 *result_capacity = 0;
61 return NULL;
62 }
63 int32_t available = capacity_ - size_;
64 if (available >= min_capacity) {
65 *result_capacity = available;
66 return outbuf_ + size_;
67 } else {
68 *result_capacity = scratch_capacity;
69 return scratch;
70 }
71 }
72
73 U_NAMESPACE_END
74