1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.MemoryOutputStream;
26 
27 private import gio.OutputStream;
28 private import gio.PollableOutputStreamIF;
29 private import gio.PollableOutputStreamT;
30 private import gio.SeekableIF;
31 private import gio.SeekableT;
32 private import glib.Bytes;
33 private import glib.ConstructionException;
34 private import gobject.ObjectG;
35 private import gtkc.gio;
36 public  import gtkc.giotypes;
37 
38 
39 /**
40  * #GMemoryOutputStream is a class for using arbitrary
41  * memory chunks as output for GIO streaming output operations.
42  * 
43  * As of GLib 2.34, #GMemoryOutputStream trivially implements
44  * #GPollableOutputStream: it always polls as ready.
45  */
46 public class MemoryOutputStream : OutputStream, PollableOutputStreamIF, SeekableIF
47 {
48 	/** the main Gtk struct */
49 	protected GMemoryOutputStream* gMemoryOutputStream;
50 
51 	/** Get the main Gtk struct */
52 	public GMemoryOutputStream* getMemoryOutputStreamStruct(bool transferOwnership = false)
53 	{
54 		if (transferOwnership)
55 			ownedRef = false;
56 		return gMemoryOutputStream;
57 	}
58 
59 	/** the main Gtk struct as a void* */
60 	protected override void* getStruct()
61 	{
62 		return cast(void*)gMemoryOutputStream;
63 	}
64 
65 	protected override void setStruct(GObject* obj)
66 	{
67 		gMemoryOutputStream = cast(GMemoryOutputStream*)obj;
68 		super.setStruct(obj);
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GMemoryOutputStream* gMemoryOutputStream, bool ownedRef = false)
75 	{
76 		this.gMemoryOutputStream = gMemoryOutputStream;
77 		super(cast(GOutputStream*)gMemoryOutputStream, ownedRef);
78 	}
79 
80 	// add the PollableOutputStream capabilities
81 	mixin PollableOutputStreamT!(GMemoryOutputStream);
82 
83 	// add the Seekable capabilities
84 	mixin SeekableT!(GMemoryOutputStream);
85 
86 
87 	/** */
88 	public static GType getType()
89 	{
90 		return g_memory_output_stream_get_type();
91 	}
92 
93 	/**
94 	 * Creates a new #GMemoryOutputStream.
95 	 *
96 	 * In most cases this is not the function you want.  See
97 	 * g_memory_output_stream_new_resizable() instead.
98 	 *
99 	 * If @data is non-%NULL, the stream will use that for its internal storage.
100 	 *
101 	 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
102 	 * storage when necessary and the stream will be considered resizable.
103 	 * In that case, the stream will start out being (conceptually) empty.
104 	 * @size is used only as a hint for how big @data is.  Specifically,
105 	 * seeking to the end of a newly-created stream will seek to zero, not
106 	 * @size.  Seeking past the end of the stream and then writing will
107 	 * introduce a zero-filled gap.
108 	 *
109 	 * If @realloc_fn is %NULL then the stream is fixed-sized.  Seeking to
110 	 * the end will seek to @size exactly.  Writing past the end will give
111 	 * an 'out of space' error.  Attempting to seek past the end will fail.
112 	 * Unlike the resizable case, seeking to an offset within the stream and
113 	 * writing will preserve the bytes passed in as @data before that point
114 	 * and will return them as part of g_memory_output_stream_steal_data().
115 	 * If you intend to seek you should probably therefore ensure that @data
116 	 * is properly initialised.
117 	 *
118 	 * It is probably only meaningful to provide @data and @size in the case
119 	 * that you want a fixed-sized stream.  Put another way: if @realloc_fn
120 	 * is non-%NULL then it makes most sense to give @data as %NULL and
121 	 * @size as 0 (allowing #GMemoryOutputStream to do the initial
122 	 * allocation for itself).
123 	 *
124 	 * |[<!-- language="C" -->
125 	 * // a stream that can grow
126 	 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
127 	 *
128 	 * // another stream that can grow
129 	 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
130 	 *
131 	 * // a fixed-size stream
132 	 * data = malloc (200);
133 	 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
134 	 * ]|
135 	 *
136 	 * Params:
137 	 *     data = pointer to a chunk of memory to use, or %NULL
138 	 *     size = the size of @data
139 	 *     reallocFunction = a function with realloc() semantics (like g_realloc())
140 	 *         to be called when @data needs to be grown, or %NULL
141 	 *     destroyFunction = a function to be called on @data when the stream is
142 	 *         finalized, or %NULL
143 	 *
144 	 * Returns: A newly created #GMemoryOutputStream object.
145 	 *
146 	 * Throws: ConstructionException GTK+ fails to create the object.
147 	 */
148 	public this(void* data, size_t size, GReallocFunc reallocFunction, GDestroyNotify destroyFunction)
149 	{
150 		auto p = g_memory_output_stream_new(data, size, reallocFunction, destroyFunction);
151 		
152 		if(p is null)
153 		{
154 			throw new ConstructionException("null returned by new");
155 		}
156 		
157 		this(cast(GMemoryOutputStream*) p, true);
158 	}
159 
160 	/**
161 	 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
162 	 * for memory allocation.
163 	 *
164 	 * Since: 2.36
165 	 *
166 	 * Throws: ConstructionException GTK+ fails to create the object.
167 	 */
168 	public this()
169 	{
170 		auto p = g_memory_output_stream_new_resizable();
171 		
172 		if(p is null)
173 		{
174 			throw new ConstructionException("null returned by new_resizable");
175 		}
176 		
177 		this(cast(GMemoryOutputStream*) p, true);
178 	}
179 
180 	/**
181 	 * Gets any loaded data from the @ostream.
182 	 *
183 	 * Note that the returned pointer may become invalid on the next
184 	 * write or truncate operation on the stream.
185 	 *
186 	 * Returns: pointer to the stream's data, or %NULL if the data
187 	 *     has been stolen
188 	 */
189 	public void* getData()
190 	{
191 		return g_memory_output_stream_get_data(gMemoryOutputStream);
192 	}
193 
194 	/**
195 	 * Returns the number of bytes from the start up to including the last
196 	 * byte written in the stream that has not been truncated away.
197 	 *
198 	 * Returns: the number of bytes written to the stream
199 	 *
200 	 * Since: 2.18
201 	 */
202 	public size_t getDataSize()
203 	{
204 		return g_memory_output_stream_get_data_size(gMemoryOutputStream);
205 	}
206 
207 	/**
208 	 * Gets the size of the currently allocated data area (available from
209 	 * g_memory_output_stream_get_data()).
210 	 *
211 	 * You probably don't want to use this function on resizable streams.
212 	 * See g_memory_output_stream_get_data_size() instead.  For resizable
213 	 * streams the size returned by this function is an implementation
214 	 * detail and may be change at any time in response to operations on the
215 	 * stream.
216 	 *
217 	 * If the stream is fixed-sized (ie: no realloc was passed to
218 	 * g_memory_output_stream_new()) then this is the maximum size of the
219 	 * stream and further writes will return %G_IO_ERROR_NO_SPACE.
220 	 *
221 	 * In any case, if you want the number of bytes currently written to the
222 	 * stream, use g_memory_output_stream_get_data_size().
223 	 *
224 	 * Returns: the number of bytes allocated for the data buffer
225 	 */
226 	public size_t getSize()
227 	{
228 		return g_memory_output_stream_get_size(gMemoryOutputStream);
229 	}
230 
231 	/**
232 	 * Returns data from the @ostream as a #GBytes. @ostream must be
233 	 * closed before calling this function.
234 	 *
235 	 * Returns: the stream's data
236 	 *
237 	 * Since: 2.34
238 	 */
239 	public Bytes stealAsBytes()
240 	{
241 		auto p = g_memory_output_stream_steal_as_bytes(gMemoryOutputStream);
242 		
243 		if(p is null)
244 		{
245 			return null;
246 		}
247 		
248 		return new Bytes(cast(GBytes*) p, true);
249 	}
250 
251 	/**
252 	 * Gets any loaded data from the @ostream. Ownership of the data
253 	 * is transferred to the caller; when no longer needed it must be
254 	 * freed using the free function set in @ostream's
255 	 * #GMemoryOutputStream:destroy-function property.
256 	 *
257 	 * @ostream must be closed before calling this function.
258 	 *
259 	 * Returns: the stream's data, or %NULL if it has previously
260 	 *     been stolen
261 	 *
262 	 * Since: 2.26
263 	 */
264 	public void* stealData()
265 	{
266 		return g_memory_output_stream_steal_data(gMemoryOutputStream);
267 	}
268 }