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