2016-06-15 18:58:17 +00:00
|
|
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2016-05-31 21:45:07 +00:00
|
|
|
// Copyright (C) 2009-2011, International Business Machines
|
2009-03-06 19:19:00 +00:00
|
|
|
// Corporation and others. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Copyright 2007 Google Inc. All Rights Reserved.
|
|
|
|
// Author: sanjay@google.com (Sanjay Ghemawat)
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/bytestream.h"
|
2009-03-09 20:32:09 +00:00
|
|
|
#include "cmemory.h"
|
2009-03-06 19:19:00 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
ByteSink::~ByteSink() {}
|
|
|
|
|
2009-03-06 19:19:00 +00:00
|
|
|
char* ByteSink::GetAppendBuffer(int32_t min_capacity,
|
2009-03-27 17:08:12 +00:00
|
|
|
int32_t /*desired_capacity_hint*/,
|
2009-03-06 19:19:00 +00:00
|
|
|
char* scratch, int32_t scratch_capacity,
|
|
|
|
int32_t* result_capacity) {
|
|
|
|
if (min_capacity < 1 || scratch_capacity < min_capacity) {
|
|
|
|
*result_capacity = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*result_capacity = scratch_capacity;
|
|
|
|
return scratch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ByteSink::Flush() {}
|
|
|
|
|
|
|
|
CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
|
2010-05-15 23:00:26 +00:00
|
|
|
: outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
|
2010-05-15 23:18:12 +00:00
|
|
|
size_(0), appended_(0), overflowed_(FALSE) {
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
CheckedArrayByteSink::~CheckedArrayByteSink() {}
|
|
|
|
|
2010-05-15 23:18:12 +00:00
|
|
|
CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
|
|
|
|
size_ = appended_ = 0;
|
|
|
|
overflowed_ = FALSE;
|
2010-05-15 23:25:17 +00:00
|
|
|
return *this;
|
2009-03-06 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
|
|
|
|
if (n <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2010-05-15 23:00:26 +00:00
|
|
|
appended_ += n;
|
2009-03-06 19:19:00 +00:00
|
|
|
int32_t available = capacity_ - size_;
|
|
|
|
if (n > available) {
|
|
|
|
n = available;
|
2010-05-15 23:18:12 +00:00
|
|
|
overflowed_ = TRUE;
|
2009-03-06 19:19:00 +00:00
|
|
|
}
|
|
|
|
if (n > 0 && bytes != (outbuf_ + size_)) {
|
2009-03-09 20:32:09 +00:00
|
|
|
uprv_memcpy(outbuf_ + size_, bytes, n);
|
2009-03-06 19:19:00 +00:00
|
|
|
}
|
|
|
|
size_ += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
|
2009-03-27 17:08:12 +00:00
|
|
|
int32_t /*desired_capacity_hint*/,
|
2009-03-06 19:19:00 +00:00
|
|
|
char* scratch,
|
|
|
|
int32_t scratch_capacity,
|
|
|
|
int32_t* result_capacity) {
|
|
|
|
if (min_capacity < 1 || scratch_capacity < min_capacity) {
|
|
|
|
*result_capacity = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
int32_t available = capacity_ - size_;
|
|
|
|
if (available >= min_capacity) {
|
|
|
|
*result_capacity = available;
|
|
|
|
return outbuf_ + size_;
|
|
|
|
} else {
|
|
|
|
*result_capacity = scratch_capacity;
|
|
|
|
return scratch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|