Google APIs Client Library for C++
data_reader.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * 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 
00020 /*
00021  * @defgroup DataLayerRaw Data Layer - Raw Data Management
00022  *
00023  * The raw data management module is responsible for access to and manipulation
00024  * of raw data. It provides abstractions and mechanisms for supplying data for
00025  * messaging payloads, and for getting the data out of those payloads. As a
00026  * rule of thumb it provides data support without being tied to the HTTP
00027  * Transport Layer or involving any inter-process messaging. The concepts it
00028  * defines are used throughout the Google APIs Client Libraries for C++ to
00029  * help facilitate passing data through the various components and subsystems.
00030  */
00031 #ifndef APISERVING_CLIENTS_CPP_DATA_DATA_READER_H_
00032 #define APISERVING_CLIENTS_CPP_DATA_DATA_READER_H_
00033 
00034 #include <istream>  // NOLINT
00035 #include <string>
00036 using std::string;
00037 #include <vector>
00038 using std::vector;
00039 #include "googleapis/base/callback.h"
00040 #include "googleapis/base/integral_types.h"
00041 #include "googleapis/base/macros.h"
00042 #include "googleapis/base/scoped_ptr.h"
00043 #include "googleapis/strings/stringpiece.h"
00044 #include "googleapis/util/status.h"
00045 namespace googleapis {
00046 
00047 namespace client {
00048 
00049 /*
00050  * Interface for reading from an asynchronous binary data stream.
00051  * @ingroup DataLayerRaw
00052  *
00053  * The DataReader is the base class for reading non-trivial data using a
00054  * streaming-like interface. It is simpler and potentially more efficient
00055  * than using standard C++ streams.
00056  *
00057  * The interface primarily consists of 2 methods:
00058  * <ul>
00059  *   <li> Read data from the reader in whole or part.
00060  *   <li> Set the offset into the byte sequence for the next read.
00061  * </ul>
00062  * The done() method indicates there is no more data to be read -- until
00063  * setting the offset back toward (or at) the beginning for the next read.
00064  * The error() method indicates premature ending. Errors are
00065  * considered hard-errors. Setting an error implies it is done. error() can
00066  * be set even if some bytes are read. Soft errors just return fewer bytes
00067  * than requested, including 0.
00068  *
00069  * Readers can get their data from arbitrary sources, and dont necessarily
00070  * buffer everything in memory. Therefore you cannot count on being able
00071  * to read their data over and over again multiple times. Most readers will
00072  * allow you to but if you have a generic reader then you should not make this
00073  * assumption.
00074  *
00075  * The reader API supports incremental reading, analogous to reading from a
00076  * stream or file. When you get data back, the reader advances its offset
00077  * within the data sequence so that the next read will start reading from
00078  * another offset; Reading data modifies the reader instance. If you want
00079  * to get all the data and are not sure where the reader came from, check
00080  * the offset and reset the reader if it is not at the beginning.
00081  *
00082  * The DataReaer abstraction is used throughout the interfaces within the
00083  * Google APIs Client Library for C++ where you might otherwise
00084  * expect to see a <code>string</code> or even StringPiece. <code>string</code>
00085  * and StringPiece are used for efficiency and convienence where small or
00086  * constant data is expected, or the data is consumed by an external library
00087  * that uses standard strings. SataReader is used in interfaces where that
00088  * involve opaque data that can be arbitrarily large or might not naturally
00089  * reside in memory already.
00090  *
00091  * A DataReader can be used in conjunction with a DataWriter, however this
00092  * is not necessary. You can use a DataWriter to write bytes then hvee that
00093  * writer create a reader to read those bytes back. This is useful if you
00094  * dont know what that byte stream is, such as an HTTP payload you are
00095  * receiving from a server. However if you already have access to the bytes
00096  * then it is easier and more efficeint to create a reader directly from those
00097  * bytes.
00098  *
00099  * It may also be that the writer serves no purpose other than to
00100  * create a reader, in which case you may as well just create a reader.
00101  * This is why some objects provide factory methods that return readers
00102  * rather than define serialization methods that take writers.
00103  *
00104  * DataReaders are not thread-safe.
00105  *
00106  * @see DataWriter
00107  * @see NewManagedInMemoryDataReader
00108  * @see NewManageFileDataReader
00109  * @see NewManageCompositeiDataReader
00110  */
00111 class DataReader {
00112  public:
00113   /*
00114    * Standard destructor.
00115    */
00116   virtual ~DataReader();
00117 
00118   /*
00119    * Returns true if the reader is generally seekable.
00120    *
00121    * This does not guarantee that SetOffset will always work, but
00122    * indicates whether it is expected to in general. As a rule of thumb
00123    * this would be true if the backing store is reliable and false if it
00124    * is transient.
00125    *
00126    * This value is meant as a hint for consumers and not as a constraint
00127    * to attempting to SetOffset.
00128    *
00129    * @return The base class assumes false to be conservative.
00130    */
00131   virtual bool seekable() const;
00132 
00133   /*
00134    * Indicates whether unread bytes still remain in the stream or not.
00135    *
00136    * @return true if there will never be more bytes to be read ahead
00137    *              of the current offset.
00138    * To distinguish between a successful read and an error, you must check
00139    * either error() or ok() since hard errors imply done() as well.
00140    */
00141   bool done() const  { return done_; }
00142 
00143   /*
00144    * Determine if we've encountered a hard error or not.
00145    *
00146    * Hard errors are sticky and can only be cleared with Reset(), if at all.
00147    * This is equivalent to !ok().
00148    *
00149    * @return true if a hard error was set, false otherwise.
00150    *
00151    * @see ok
00152    * @see Status
00153    */
00154   bool error() const { return !status_.ok(); }
00155 
00156   /*
00157    * Determine if we've encountered a hard error or not.
00158    *
00159    * This is equivalent to !error().
00160    * @return true if no hard error has been set, false if an error.
00161    * @see error
00162    */
00163   bool ok() const { return status_.ok(); }
00164 
00165   /*
00166    * Returns details for the error on the stream, if any.
00167    *
00168    * @return a successful status if the stream is ok, otherwise the
00169    * error encounteredd.
00170    */
00171   util::Status status() const { return status_; }
00172 
00173   /*
00174    * Returns the current offset in the byte sequence.
00175    * @return The number of bytes read so far. 0 is the beginning.
00176    */
00177   int64 offset() const { return offset_; }
00178 
00179   /*
00180    * Set the offset in the byte sequence for the next read.
00181    *
00182    * @param[in] position The new offset. 0 is the beginning. There is no
00183    * means to specify the end other than by absolute position.
00184    *
00185    * @return offset that was set. -1 indicates complete failure.
00186    * if the result was < offset then the reader will be positioned at the
00187    * end of the sequence.
00188    */
00189   int64 SetOffset(int64 position);
00190 
00191   /*
00192    * Resets the reader back to the beginning of the byte sequence.
00193    *
00194    * If the reset succeeds, the offset will be 0. If the reset fails then the
00195    * reader will be in an error() state preventing any future reads.
00196    *
00197    * @return false on failure. See status() for details.
00198    *
00199    * @see SetOffset
00200    */
00201   bool Reset() { return SetOffset(0) == 0; }
00202 
00203   /*
00204    * Keeps reading synchronously until the request is satisfied.
00205    *
00206    * If the stream is done before the requested bytes are read, then
00207    * this will return a partial read to the point where the stream
00208    * was done. You will need to check ok() or error() if you want
00209    * to distinguish the partial read being due to the end of the sequence
00210    * or a hard error.
00211    *
00212    * Reading will advance the offset by the number of bytes actually read.
00213    *
00214    * @param[in] max_bytes The most bytes to read.
00215    * @param[in] storage The storage to read into must have at
00216    *            least max_bytes of capacity allocated.
00217    * @return the number of bytes actually read might be 0 and the reader may
00218    *         still not be done(). Negative values are never returned.
00219    *
00220    * @see ReadToString
00221    * @see RemainderToString
00222    */
00223   int64 ReadToBuffer(int64 max_bytes, char* storage);
00224 
00225   /*
00226    * Keeps reading synchronously until the request is satisfied.
00227    *
00228    * If the stream is done before the requested bytes are read, then
00229    * this will return a partial read to the point where the stream
00230    * was done. You will need to check ok() or error() if you want
00231    * to distinguish the partial read being due to the end of the sequence
00232    * or a hard error.
00233    *
00234    * Reading will advance the offset by the number of bytes actually read.
00235    *
00236    * @param[in] max_bytes The most bytes to read.
00237    * @param[in,out] append_to The string to append the data into.
00238    *        If you need to look
00239    *        at the raw memory, use string::data() rather than string::c_str()
00240    *        since the byte sequence you've read from may contain binary data.
00241    * @return the number of bytes actually read might be 0 and the reader may
00242    *         still not be done(). Negative values are never returned.
00243    *
00244    * @see ReadToBuffer
00245    * @see RemainderToString
00246    */
00247   int64 ReadToString(int64 max_bytes, string* append_to);
00248 
00249   /*
00250    * Keep reading synchronously until done().
00251    *
00252    * You should check either done() or error() to determine whether you are
00253    * done() because of a hard error or because you read all remaining bytes.
00254    *
00255    * @return the remaining string from the offset.
00256    *
00257    * @see ReadToBuffer
00258    * @see ReadToString
00259    */
00260   string RemainderToString() {
00261     string result;
00262     ReadToString(kint64max, &result);
00263     return result;
00264   }
00265 
00266   /*
00267    * Returns the total length of the byte sequence if it is known.
00268    *
00269    * @return -1 if the length is not known. Otherwise the total length
00270    *         from the beginning regardless of the current offset.
00271    */
00272   int64 TotalLengthIfKnown() const { return total_length_; }
00273 
00274  protected:
00275   /*
00276    * Standard reader constructor.
00277    *
00278    * The deleter determines whether the reader will be characterized
00279    * as begin "managed" or "unmanaged". A NULL reader indicates it is
00280    * "unmanaged".
00281    *
00282    * Managed readers call a closure when they are destroyed. Often this
00283    * closure is used to free up resources consumed by the byte sequence
00284    * the reader is reading from. However the closure may do anything.
00285    *
00286    * @warning The reader instance will not be valid when teh deleter is
00287    *          called
00288    *
00289    * @param[in] deleter If non-NULL then the caller retains ownership.
00290    *            Typically this will be a self-managed deleter that
00291    *            self-destructs once run.
00292    *
00293    * @see NewCallback
00294    */
00295   explicit DataReader(Closure* deleter);
00296 
00297   /*
00298    * Implementation hook to read bytes into storage.
00299    *
00300    * This method is called by the public Read* methods to perform the actual
00301    * data reading. The base class will do the adminstrative stuff, including
00302    * updating the offset. However marking done and error status are left to
00303    * this method.
00304    *
00305    * @param[in] max_bytes Read at most max_bytes into storage.
00306    * @param[in] storage At least max_bytes of allocated storage.
00307    *
00308    * @return Number of bytes actually written into storage. 0 has no special
00309    *         meaning beyond no byets were written for whatever reason.
00310    *         0 does not indicate any kind of failure. This method should
00311    *         never return a negative number.
00312    *
00313    * This method is resopnsible for explicitly marking the reader done() when
00314    * there is no more data to be returned by future calls. It should set the
00315    * status if a hard error is encountered.
00316    *
00317    * This method is required of all concrete readers as the sole point
00318    * of access.
00319    *
00320    * @see set_done
00321    * @see set_etatus
00322    */
00323   virtual int64 DoReadToBuffer(int64 max_bytes, char* storage) = 0;
00324 
00325   /*
00326    * Sets the offset into the byte sequence.
00327    *
00328    * This method is called by the public SetOffset() method. The public method
00329    * will do the administrative stuff including resetting the offset and
00330    * updating the status based on the status returned by this method.
00331    *
00332    * This method will return the actual offset that the sequence is at after
00333    * attempting to set. The intent is that if offset is too big then the
00334    * reader will seek to the end and return the offset of the end. This isnt
00335    * an ideal definition but is compatible with being able to seek into
00336    * readers of unknown size.
00337    *
00338    * In a complete failure the offset will be -1 (invalid) and require a
00339    * successful SetOffset to restore.
00340    *
00341    * @return the offset that was seeked to or -1 on complete failure. The
00342    * seek should not exceed the length of the bytestream.
00343    */
00344   virtual int64 DoSetOffset(int64 position);
00345 
00346   /*
00347    * Sets the status as a means to pass error details back to the caller.
00348    *
00349    * Setting an error implies setting done as well. However clearing an
00350    * error by setting an ok status will not clear done. To clear the done
00351    * state you must explicitly call set_done.
00352    *
00353    * @param[in] status An error implies a hard failure and will mark the reader
00354    *            done.
00355    */
00356   void set_status(util::Status status);
00357 
00358   /*
00359    * Indicates whether there is more data to be read.
00360    *
00361    * @param[in] done true if there si no more data, false if there is.
00362    */
00363   void set_done(bool done)   { done_ = done; }
00364 
00365   /*
00366    * Sets the total number of bytes in the reader.
00367    *
00368    * If calling this method, you should call it from within your constructor if
00369    * possible.
00370    *
00371    * @param[in] length The number of bytes is returned by TotalLengthIfKnown.
00372    *       -1 indicates unknown. 0 Indicates none. The default is -1 (unknown).
00373    *
00374    */
00375   void set_total_length(int64 length);
00376 
00377  private:
00378   Closure* deleter_;       // Can be NULL
00379   int64 total_length_;     // < 0 if unknown
00380   int64 offset_;           // bytes read so far
00381   util::Status status_;  // ok() unless in an error state.
00382   bool done_;              // we'll never return any more data (e.g. eof)
00383 
00384   DISALLOW_COPY_AND_ASSIGN(DataReader);
00385 };
00386 
00387 
00388 /*
00389  * Returns a data reader that is always in an error state.
00390  * @ingroup DataLayerRaw
00391  *
00392  * @param[in] status The permanent status() to give the reader, expaining
00393  *            why any access to it will fail.
00394  * @param[in] deleter If non-NULL this reader will be a "managed"
00395  *            reader and run teh deleter in its destructor.
00396  *
00397  * This reader is meant to be returned by factory methdos that fail.
00398  * It is a placeholder value so that DataReaders are never NULL.
00399  */
00400 DataReader* NewManagedInvalidDataReader(
00401     util::Status status, Closure* deleter);
00402 
00403 /*
00404  * Returns an unmanaged invalid data reader.
00405  * @ingroup DataLayerRaw
00406  *
00407  * @param[in] status The permanent status() to give the reader.
00408  *
00409  * @see NewManagedInvalidDataReader
00410  */
00411 inline DataReader* NewUnmanagedInvalidDataReader(util::Status status) {
00412   return NewManagedInvalidDataReader(status, NULL);
00413 }
00414 
00415 
00416 /*
00417  * Reads from a contiguous byte array.
00418  * @ingroup DataLayerRaw
00419  *
00420  * Managed InMemoryDataReader instances are very low overhead if the memory
00421  * already exists. However, the owner must ensure that the referenced data
00422  * remains valid and unchanged over the lifetime of the reader. Modifying the
00423  * data will corrupt the reader.
00424  *
00425  * Unmanaged instances can pass memory ownership into the instance itself and
00426  * have the instance encapsulate it by being the only remaining direct
00427  * reference. This reduces the chance for the data to get corrupted and
00428  * guarantees that the data will remain vaid for as long, and only as long,
00429  * as the reader remains.
00430  *
00431  * For brevity only StringPiece variations are provided. If you have an
00432  * ordinary char*, you can turn it into a StringPiece by passing
00433  * StringPiece(ptr, len) presuming that you know the length. If you do not
00434  * know the length then you cannot use the InMemoryDataReader anyway.
00435  *
00436  * If your char* is part of a larger object which you only need to support
00437  * the reader, you can have the reader manage it by passing a
00438  * DeletePointerClosure as the reader's managing closure.
00439  *
00440  * <pre>
00441  *   char* data = obj->data()
00442  *   int64 len = obj->data_len()
00443  *   NewManagedInMemoryDataReader(StringPiece(data, len),
00444  *                                DeletePointerClosure(obj))
00445  * </pre>
00446  *
00447  * @param[in] data The data used by the reader must remain valid and unchanged
00448  *            over the lifetime of the readaer.
00449  * @param[in] deleter If non-NULL then this will be a managed reader calling
00450  *        the deletre when this object is destroyed. See the base DataReader
00451  *        class for more information about managed readers.
00452  */
00453 DataReader* NewManagedInMemoryDataReader(
00454     const StringPiece& data, Closure* deleter);
00455 
00456 /*
00457  * Creates an unmanaged InMenoryDataReader
00458  * @ingroup DataLayerRaw
00459  *
00460  * This is shorthand for NewManagedInMemoryDataReader(data, NULL)
00461  * @see NewManagedInMemoryDataReader
00462  */
00463 DataReader* NewUnmanagedInMemoryDataReader(const StringPiece& data);
00464 
00465 /*
00466  * Returns a managed reader that consumes a dynanic input string.
00467  * @ingroup DataLayerRaw
00468  *
00469  * This is a convienence function to create a managed reader from
00470  * a string pinter.
00471  *
00472  * @param[in] data Takes ownership of the string
00473  * @see NewManagedInMemoryDataReader
00474  */
00475 DataReader* NewManagedInMemoryDataReader(string* data);
00476 
00477 /*
00478  * Creates a managed InMemoryDataReader from an existing string.
00479  * @ingroup DataLayerRaw
00480  *
00481  * This function will create a copy of the string then manage the copy.
00482  *
00483  * @see NewManagedInMemoryDataReader
00484  */
00485 inline DataReader* NewManagedInMemoryDataReader(const string& data) {
00486   return NewManagedInMemoryDataReader(new string(data));
00487 }
00488 
00489 
00490 /*
00491  * Returns an InMemoryDataReader that returns the content from another
00492  * data reader.
00493  * @ingroup DataLayerRaw
00494  *
00495  * This reader is only intended to make unreliable readers reliable
00496  * when you need to reset them. It is high overhead since you are creating
00497  * an in-memory copy of the data, which defeats the whole point of having
00498  * a reader and will cost twice the storage.
00499  *
00500  * @param[in] reader Ownership of the wrapped reader will be passed the new
00501  *            instance to manage.
00502  * @param[in] buffer_bytes The total number of bytes to expect,
00503  *            or -1 if unknown.
00504  */
00505 DataReader* NewManagedBufferedDataReader(
00506     DataReader* reader, int64 buffer_bytes);
00507 
00508 /*
00509  * Similar to NewUManagedBufferdDataReader but the caller retains ownership
00510  * of the original reader.
00511  * @ingroup DataLayerRaw
00512  *
00513  * @param[in] reader The caller keeps ownership of the reader being wrapped.
00514  * @param[in] buffer_bytes The total number of bytes to expect,
00515  *            or -1 if unknown.
00516  *
00517  * @see NewManagedBufferedDataReader
00518  */
00519 DataReader* NewUnmanagedBufferedDataReader(
00520     DataReader* reader, int64 buffer_bytes);
00521 
00522 /*
00523  * A general form of a managed BufferedDataReader.
00524  * @ingroup DataLayerRaw
00525  *
00526  * @param[in] reader The caller maintains ownership of this reader,
00527  *            however the caller may have passed ownership to the deleter.
00528  * @param[in] buffer_bytes The number of bytes to expect in the reader, or -1
00529  *            if not known.
00530  * @param[in] deleter The management closure to call when this instance is
00531  *            destroyed.
00532  */
00533 DataReader* NewManagedBufferedDataReader(
00534     DataReader* reader, int64 buffer_bytes, Closure* deleter);
00535 
00536 
00537 /*
00538  * Returns an unmanaged composite DataReader that reads directly from
00539  * one or more other data readers.
00540  * @ingroup DataLayerRaw
00541  *
00542  * The composite readers are not buffered at all by this reader so little
00543  * additional overhead is added. The component readers within this may
00544  * come from different types of sources or might just have different fragments
00545  * of memory.
00546  *
00547  * @param[in] readers The list of readers taht define the byte sequence
00548  *            returned by  this reader. The caller retains ownership of
00549  *            each of these and must guarantee they are kept valid and
00550  *            otherwise unused over the lifetime of this instance.
00551  *            if data is read from any of these outside the composite reader
00552  *            then the compsite reader will be corrupted.
00553  */
00554 DataReader* NewUnmanagedCompositeDataReader(
00555     const vector<DataReader*>& readers);
00556 
00557 /*
00558  * Returns a managed composite DataReader that reads directly from one or
00559  * more other data readers.
00560  * @ingroup DataLayerRaw
00561  *
00562  * The composite readers are not buffered at all by this reader so little
00563  * additional overhead is added. The component readers within this may
00564  * come from different types of sources or might just have different fragments
00565  * of memory.
00566  *
00567  * @param[in] readers The list of readers taht define the byte sequence
00568  *            returned by this reader. The caller retains ownership of each of
00569  *            these readers though may pass their ownership to the deleter.
00570  * @param[in] deleter The management closure to be called when the composite
00571  *            reader is destroyed.
00572  */
00573 DataReader* NewManagedCompositeDataReader(
00574     const vector<DataReader*>& readers, Closure* deleter);
00575 
00576 /*
00577  * Creates a managed closure that deletes an entire vector of readers when run.
00578  * @ingroup DataLayerRaw
00579  *
00580  * This is a convienence function for creating a closure to pass to
00581  * NewManagedCompositeDataReader.
00582  *
00583  * @param[in] readers Takes ownership of the vector and its contents are
00584  *            passed so the vector should hae been allocated with the new
00585  *            operator.
00586  */
00587 Closure* NewCompositeReaderListAndContainerDeleter(
00588     vector<DataReader*>* readers);
00589 
00590 
00591 /*
00592  * Creates a managed DataReader that reads its byte stream from a file on disk.
00593  * @ingroup DataLayerRaw
00594  *
00595  * If the path does not exist, or cannot be read for some other reason then
00596  * this will return a reader in the error state. Check its status() to get
00597  * more details on the error.
00598  *
00599  * The caller should ensure the file remains valid and unchanged over the
00600  * lifetime of the reader. Changing the file contents will corrupt the reader.
00601  *
00602  * @param[in] path The path to the file to read from.
00603  * @param[in] deleter If non-NULL this closure will be called when the reader
00604  *            is destroyed. The reader itself will no longer be valid when the
00605  *            closure is called.
00606  */
00607 DataReader* NewManagedFileDataReader(const StringPiece& path, Closure* deleter);
00608 
00609 /*
00610  * Creates an unmanaged DataReader that reads its byte stream from a file on
00611  * disk.
00612  * @ingroup DataLayerRaw
00613  *
00614  * If the path does not exist, or cannot be read for some other reason then
00615  * this will return a reader in the error state. Check its status() to get
00616  * more details on the error.
00617  *
00618  * The caller should ensure the file remains valid and unchanged over the
00619  * lifetime of the reader. Changing the file contents will corrupt the reader.
00620  *
00621  * @param[in] path The path to the file to read from.
00622  */
00623 DataReader* NewUnmanagedFileDataReader(const StringPiece& path);
00624 
00625 
00626 
00627 /*
00628  * Creates a managed reader that reads its byte stream from a generic
00629  * C++ std::istream of unknown length.
00630  * @ingroup DataLayerRaw
00631  *
00632  * If you know how many bytes are in the stream then you should use
00633  * NewManagedIstreamDataReaderWithLength
00634  *
00635  * @param[in] stream The caller retains ownership of the stream to read from,
00636  *            though the caller may pass ownership to the deleter.
00637  *            The caller must insure it remains valid over the lifetime of the
00638  *            reader, and should not read from it outside the returned reader.
00639  * @param[in] deleter The managing closure is called when the reaer is
00640  *            destroyed. The reader will not be valid when the deleter is
00641  *            called.
00642  *
00643  * @see NewDeleterClosure
00644  * @see NewManagedFileDataReader
00645  */
00646 DataReader* NewManagedIstreamDataReader(std::istream* stream, Closure* deleter);
00647 
00648 /*
00649  * Creates a managed reader that reads its byte stream from a generic
00650  * C++ std::istream of unknown length.
00651  * @ingroup DataLayerRaw
00652  *
00653  * @param[in] stream The caller retains ownership of the stream to read from,
00654  *            though the caller may pass ownership to the deleter.
00655  *            The caller must insure it remains valid over the lifetime of the
00656  *            reader, and should not read from it outside the returned reader.
00657  * @param[in] length The length if known, or -1 can be used if unknown.
00658  *            A length of 0 indicates and empty stream.
00659  * @param[in] deleter The managing closure is called when the reaer is
00660  *            destroyed. The reader will not be valid when the deleter is
00661  *            called.
00662  *
00663  * @see NewDeleterClosure
00664  * @see NewMmanagedFileDataReader
00665  */
00666 DataReader* NewManagedIstreamDataReaderWithLength(
00667     std::istream* stream, int64 length, Closure* deleter);
00668 
00669 /*
00670  * Creates an unmanaged reader that reads its byte stream from a generic
00671  * C++ std::istream of unknown length.
00672  * @ingroup DataLayerRaw
00673  *
00674  * This is similar to NewManagedIstreamDataReader, but with a NULL deleter.
00675  *
00676  * @see NewManagedIStreamDataReader
00677  * @see NewUnmanagedFileDataReader
00678  */
00679 DataReader* NewUnmanagedIstreamDataReader(std::istream* stream);
00680 
00681 /*
00682  * Creates an unmanaged reader that reads its byte stream from a generic
00683  * C++ std::istream of a known length.
00684  * @ingroup DataLayerRaw
00685  *
00686  * This is similar to NewManagedIstreamDataReaderWithLength,
00687  * but with a NULL deleter.
00688  *
00689  * @see NewManagedIStreamDataReaderWIthLength
00690  * @see NewUnmanagedFileDataReader
00691  */
00692 DataReader* NewUnmanagedIstreamDataReaderWithLength(
00693     std::istream* stream, int64 length);
00694 
00695 }  // namespace client
00696 
00697 } // namespace googleapis
00698 #endif  // APISERVING_CLIENTS_CPP_DATA_DATA_READER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines