183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius// Copyright (C) 2009-2011, International Business Machines 285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Corporation and others. All Rights Reserved. 385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// 485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Copyright 2007 Google Inc. All Rights Reserved. 585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Author: sanjay@google.com (Sanjay Ghemawat) 685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/utypes.h" 885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/bytestream.h" 985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "cmemory.h" 1085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 1185bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_BEGIN 1285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 1383a171d1a62abf406f7f44ae671823d5ec20db7dCraig CorneliusByteSink::~ByteSink() {} 1483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 1585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hochar* ByteSink::GetAppendBuffer(int32_t min_capacity, 1685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t /*desired_capacity_hint*/, 1785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho char* scratch, int32_t scratch_capacity, 1885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t* result_capacity) { 1985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (min_capacity < 1 || scratch_capacity < min_capacity) { 2085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *result_capacity = 0; 2185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return NULL; 2285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 2385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *result_capacity = scratch_capacity; 2485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return scratch; 2585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} 2685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 2785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ByteSink::Flush() {} 2885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 2985bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoCheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity) 3027f654740f2a26ad62a5c155af9199af9e69b889claireho : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity), 3127f654740f2a26ad62a5c155af9199af9e69b889claireho size_(0), appended_(0), overflowed_(FALSE) { 3227f654740f2a26ad62a5c155af9199af9e69b889claireho} 3327f654740f2a26ad62a5c155af9199af9e69b889claireho 3483a171d1a62abf406f7f44ae671823d5ec20db7dCraig CorneliusCheckedArrayByteSink::~CheckedArrayByteSink() {} 3583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 3627f654740f2a26ad62a5c155af9199af9e69b889clairehoCheckedArrayByteSink& CheckedArrayByteSink::Reset() { 3727f654740f2a26ad62a5c155af9199af9e69b889claireho size_ = appended_ = 0; 3827f654740f2a26ad62a5c155af9199af9e69b889claireho overflowed_ = FALSE; 3927f654740f2a26ad62a5c155af9199af9e69b889claireho return *this; 4085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} 4185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 4285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid CheckedArrayByteSink::Append(const char* bytes, int32_t n) { 4385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (n <= 0) { 4485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 4627f654740f2a26ad62a5c155af9199af9e69b889claireho appended_ += n; 4785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t available = capacity_ - size_; 4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (n > available) { 4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho n = available; 5027f654740f2a26ad62a5c155af9199af9e69b889claireho overflowed_ = TRUE; 5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (n > 0 && bytes != (outbuf_ + size_)) { 5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho uprv_memcpy(outbuf_ + size_, bytes, n); 5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho size_ += n; 5685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} 5785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 5885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hochar* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity, 5985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t /*desired_capacity_hint*/, 6085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho char* scratch, 6185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t scratch_capacity, 6285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t* result_capacity) { 6385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (min_capacity < 1 || scratch_capacity < min_capacity) { 6485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *result_capacity = 0; 6585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return NULL; 6685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 6785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t available = capacity_ - size_; 6885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (available >= min_capacity) { 6985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *result_capacity = available; 7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return outbuf_ + size_; 7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } else { 7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *result_capacity = scratch_capacity; 7385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return scratch; 7485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 7585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} 7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 7785bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_END 78