GME  13
BinMemInputStream.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: BinMemInputStream.cpp 670359 2008-06-22 13:43:45Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/BinMemInputStream.hpp>
00027 #include <xercesc/framework/MemoryManager.hpp>
00028 #include <string.h>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  BinMemInputStream: Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 BinMemInputStream::BinMemInputStream( const XMLByte* const       initData
00036                                     , const XMLSize_t            capacity
00037                                     , const BufOpts              bufOpt
00038                                     ,       MemoryManager* const manager) :
00039     fBuffer(0)
00040     , fBufOpt(bufOpt)
00041     , fCapacity(capacity)
00042     , fCurIndex(0)
00043     , fMemoryManager(manager)
00044 {
00045     // According to the buffer option, do the right thing
00046     if (fBufOpt == BufOpt_Copy)
00047     {
00048         XMLByte* tmpBuf = (XMLByte*) fMemoryManager->allocate
00049         (
00050             fCapacity * sizeof(XMLByte)
00051         );//new XMLByte[fCapacity];
00052         memcpy(tmpBuf, initData, capacity);
00053         fBuffer = tmpBuf;
00054     }
00055      else
00056     {
00057         fBuffer = initData;
00058     }
00059 }
00060 
00061 BinMemInputStream::~BinMemInputStream()
00062 {
00063     //
00064     //  If we adopted or copied the buffer, then clean it up. We have to
00065     //  cast off the const'ness in that case in order to delete it.
00066     //
00067     if ((fBufOpt == BufOpt_Adopt) || (fBufOpt == BufOpt_Copy))
00068         fMemoryManager->deallocate((XMLByte*)fBuffer);//delete [] (XMLByte*)fBuffer;
00069 }
00070 
00071 
00072 // ---------------------------------------------------------------------------
00073 //  MemBinInputStream: Implementation of the input stream interface
00074 // ---------------------------------------------------------------------------
00075 XMLSize_t BinMemInputStream::readBytes(       XMLByte* const  toFill
00076                                       , const XMLSize_t       maxToRead)
00077 {
00078     //
00079     //  Figure out how much we can really read. Its the smaller of the
00080     //  amount available and the amount asked for.
00081     //
00082     const XMLSize_t available = (fCapacity - fCurIndex);
00083     if (!available)
00084         return 0;
00085 
00086     const XMLSize_t actualToRead = available < maxToRead ? available : maxToRead;
00087 
00088     memcpy(toFill, &fBuffer[fCurIndex], actualToRead);
00089     fCurIndex += actualToRead;
00090     return actualToRead;
00091 }
00092 
00093 const XMLCh* BinMemInputStream::getContentType() const
00094 {
00095     return 0;
00096 }
00097 
00098 XERCES_CPP_NAMESPACE_END