SWIbufferedOutputStream.cpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #include "SWIbufferedOutputStream.hpp"
  23. #include <cstdio>
  24. SWIbufferedOutputStream::SWIbufferedOutputStream(SWIoutputStream *outStream,
  25.                                                  int bufferSize,
  26.                                                  bool ownStream):
  27.   SWIfilterOutputStream(outStream, ownStream),
  28.   _buffer(NULL), _pos(NULL), _end(NULL)
  29. {
  30.   if (bufferSize < 1) bufferSize = 1;
  31.   _buffer = new char[bufferSize];
  32.   _pos = _buffer;
  33.   _end = _pos + bufferSize;
  34. }
  35. SWIbufferedOutputStream::~SWIbufferedOutputStream()
  36. {
  37.   flush();
  38.   if (isOwner()) close();
  39. }
  40. SWIstream::Result SWIbufferedOutputStream::close()
  41. {
  42.   Result rc1 = flush();
  43.   Result rc2 = SWIfilterOutputStream::close();
  44.   delete [] _buffer;
  45.   _pos = _end = _buffer = NULL;
  46.   return rc1 == SUCCESS ? rc2 : rc1;
  47. }
  48. int SWIbufferedOutputStream::writeBytes(const void *data, int dataSize)
  49. {
  50.   if (_buffer == NULL) return SWIstream::ILLEGAL_STATE;
  51.   const char *p = static_cast<const char *>(data);
  52.   const char *q = p + dataSize;
  53.   int nbWritten = 0;
  54.   while (p < q)
  55.   {
  56.     while (_pos < _end && p < q)
  57.     {
  58.       *_pos++ = *p++;
  59.       nbWritten++;
  60.     }
  61.     SWIstream::Result rc;
  62.     if (p < q && (rc = flushBuffer()) != SWIstream::SUCCESS)
  63.       return rc;
  64.   }
  65.   return nbWritten;
  66. }
  67. SWIstream::Result SWIbufferedOutputStream::printString(const char *s)
  68. {
  69.   if (_buffer == NULL) return SWIstream::ILLEGAL_STATE;
  70.   const char *p = s;
  71.   while (*p)
  72.   {
  73.     while (_pos < _end && *p) *_pos++ = *p++;
  74.     SWIstream::Result rc;
  75.     if (*p && (rc = flushBuffer()) != SWIstream::SUCCESS)
  76.       return rc;
  77.   }
  78.   return SWIstream::SUCCESS;
  79. }
  80. SWIstream::Result  SWIbufferedOutputStream::printChar(char c)
  81. {
  82.   if (_buffer == NULL) return SWIstream::ILLEGAL_STATE;
  83.   SWIstream::Result rc;
  84.   if (_pos == _end && (rc = flushBuffer()) != SWIstream::SUCCESS)
  85.     return rc;
  86.   *_pos++ = c;
  87.   return SWIstream::SUCCESS;
  88. }
  89. SWIstream::Result SWIbufferedOutputStream::flush()
  90. {
  91.   if (_buffer == NULL) return SWIstream::ILLEGAL_STATE;
  92.   return flushBuffer();
  93. }
  94. bool SWIbufferedOutputStream::isBuffered() const
  95. {
  96.   return true;
  97. }
  98. SWIstream::Result SWIbufferedOutputStream::flushBuffer()
  99. {
  100.   int len = _pos - _buffer;
  101.   _pos = _buffer;
  102.   if (len == 0)
  103.     return SWIstream::SUCCESS;
  104.   int rc = SWIfilterOutputStream::writeBytes(_buffer, len);
  105.   if (len == rc)
  106.     return SWIstream::SUCCESS;
  107.   if (rc >= 0)
  108.     return SWIstream::WRITE_ERROR;
  109.   return SWIstream::Result(rc);
  110. }