1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 /** \file
10  * Stream abstraction to/from in-memory data.
11  */
12 
13 /*****************************************************************************/
14 
15 #ifndef __dng_memory_stream__
16 #define __dng_memory_stream__
17 
18 /*****************************************************************************/
19 
20 #include "dng_stream.h"
21 
22 /*****************************************************************************/
23 
24 /// \brief A dng_stream which can be read from or written to memory.
25 ///
26 /// Stream is populated via writing and either read or accessed by asking for contents as a pointer.
27 
28 class dng_memory_stream: public dng_stream
29 	{
30 
31 	protected:
32 
33 		dng_memory_allocator &fAllocator;
34 
35 		uint32 fPageSize;
36 
37 		uint32 fPageCount;
38 		uint32 fPagesAllocated;
39 
40 		dng_memory_block **fPageList;
41 
42 		uint64 fMemoryStreamLength;
43 
44         uint64 fLengthLimit;
45 
46 	public:
47 
48 		/// Construct a new memory-based stream.
49 		/// \param allocator Allocator to use to allocate memory in stream as needed.
50 		/// \param sniffer If non-NULL used to check for user cancellation.
51 		/// \param pageSize Unit of allocation for data stored in stream.
52 
53 		dng_memory_stream (dng_memory_allocator &allocator,
54 						   dng_abort_sniffer *sniffer = NULL,
55 						   uint32 pageSize = 64 * 1024);
56 
57 		virtual ~dng_memory_stream ();
58 
59         /// Sets a maximum length limit.
60 
SetLengthLimit(uint64 limit)61         void SetLengthLimit (uint64 limit)
62             {
63             fLengthLimit = limit;
64             }
65 
66 		/// Copy a specified number of bytes to a target stream.
67 		/// \param dstStream The target stream.
68 		/// \param count The number of bytes to copy.
69 
70 		virtual void CopyToStream (dng_stream &dstStream,
71 								   uint64 count);
72 
73 	protected:
74 
75 		virtual uint64 DoGetLength ();
76 
77 		virtual void DoRead (void *data,
78 							 uint32 count,
79 							 uint64 offset);
80 
81 		virtual void DoSetLength (uint64 length);
82 
83 		virtual void DoWrite (const void *data,
84 							  uint32 count,
85 							  uint64 offset);
86 
87 	};
88 
89 /*****************************************************************************/
90 
91 #endif
92 
93 /*****************************************************************************/
94