SWIdataOutputStream.hpp
上传用户: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. #ifndef SWIdataOutputStream_HPP
  23. #define SWIdataOutputStream_HPP
  24. #include "SWIoutputStream.hpp"
  25. #define SWI_BUFFER_OUTPUT_STREAM_DEFAULT_SIZE 512
  26. /**
  27.  * This class represents an output stream that writes to a buffer.  The
  28.  * <code>write</code> method causes the buffer to grow if it cannot hold the
  29.  * new data.  Once the stream is completed, the buffer can be retrieved using
  30.  * the <code>getBuffer</code> method and the number of bytes in the buffer is
  31.  * retrieved using the <code>getSize</code> method.  The memory buffer is
  32.  * deleted once the output stream is deleted.  Therefore, the buffer should be
  33.  * copied, if it is required after the stream is deleted.
  34.  *
  35.  * @doc <p> Copyright 2004 Vocalocity, Inc.
  36.  * All Rights Reserved.
  37.  **/
  38. class SWIUTIL_API_CLASS SWIdataOutputStream: public SWIoutputStream
  39. {
  40.   // ................. CONSTRUCTORS, DESTRUCTOR  ............
  41.   //
  42.   /**
  43.    * Creates an empty SWIdataOutputStream whose initial capacity is equal to
  44.    * the specified capacity.  If the capacity is less than 1, the initial
  45.    * capacity is set to 1.
  46.    *
  47.    * @param capacity The initial capacity required for the internal buffer.
  48.    **/
  49.  public:
  50.   SWIdataOutputStream(int capacity = SWI_BUFFER_OUTPUT_STREAM_DEFAULT_SIZE);
  51.   // ------------------------------------------------------------
  52.   /// destructor
  53.  public:
  54.   virtual ~SWIdataOutputStream();
  55.   //
  56.   // ......... METHODS ..........
  57.   //
  58.   /**
  59.    * Writes <code>bufferSize</code> bytes from the specified buffer.
  60.    *
  61.    * <p> If <code>dataSize</code> is less than <code>0</code>, or
  62.    * <code>data</code> is <code>null</code> and <code>dataSize</code> is not
  63.    * <code>0</code>, then <code>SWI_STREAM_INVALID_ARGUMENT</code> is
  64.    * returned.
  65.    *
  66.    * @param    data   [in] the data to be written.
  67.    * @param    dataSize [in]  the number of bytes to write.
  68.    * @return   the number of bytes written, or a negative number indicating failure.
  69.    **/
  70.  public:
  71.   virtual int writeBytes(const void* data, int dataSize);
  72.   /**
  73.    * Returns a pointer to the internal buffer of the stream.  Since only a
  74.    * pointer is returned, the pointer return should not be modified.
  75.    *
  76.    * @return a pointer to the internal buffer of the stream.
  77.    **/
  78.  public:
  79.   const void *getBuffer() const
  80.   {
  81.     return _buffer;
  82.   }
  83.   /**
  84.    * Returns the buffer as a C-string with a NULL character at the end.
  85.    **/
  86.   const char *getString() const;
  87.  public:
  88.   /**
  89.    * Returns the number of bytes in the internal buffer of the stream.
  90.    *
  91.    * @return the number of bytes in the internal buffer of the stream.
  92.    **/
  93.   int getSize() const
  94.   {
  95.     return _bufferSize;
  96.   }
  97.  public:
  98.   /**
  99.    * Resets the size of the buffer to 0.
  100.    **/
  101.   void reset()
  102.   {
  103.     _bufferSize = 0;
  104.   }
  105.  public:
  106.   virtual Result close();
  107.   virtual bool isBuffered() const;
  108.  private:
  109.   Result ensureCapacity(int requiredCapacity) const;
  110.   // members.
  111.   /**
  112.    * The internal buffer for the stream.
  113.    **/
  114.  private: mutable char *_buffer;
  115.   /**
  116.    *  The current capacity of the buffer.
  117.    **/
  118.  private: mutable int _capacity;
  119.   /**
  120.    * The number of bytes written so far into the buffer.
  121.    **/
  122.  private: mutable int _bufferSize;
  123.   //
  124.   // ......... operators  ........
  125.   //
  126. };
  127. #endif