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