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.BufferedInputStream;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.FilterInputStream;
30 private import gio.InputStream;
31 private import gio.SeekableIF;
32 private import gio.SeekableT;
33 private import gio.c.functions;
34 public  import gio.c.types;
35 private import glib.ConstructionException;
36 private import glib.ErrorG;
37 private import glib.GException;
38 private import gobject.ObjectG;
39 public  import gtkc.giotypes;
40 
41 
42 /**
43  * Buffered input stream implements #GFilterInputStream and provides
44  * for buffered reads.
45  * 
46  * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
47  * 
48  * To create a buffered input stream, use g_buffered_input_stream_new(),
49  * or g_buffered_input_stream_new_sized() to specify the buffer's size at
50  * construction.
51  * 
52  * To get the size of a buffer within a buffered input stream, use
53  * g_buffered_input_stream_get_buffer_size(). To change the size of a
54  * buffered input stream's buffer, use
55  * g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
56  * cannot be reduced below the size of the data within the buffer.
57  */
58 public class BufferedInputStream : FilterInputStream, SeekableIF
59 {
60 	/** the main Gtk struct */
61 	protected GBufferedInputStream* gBufferedInputStream;
62 
63 	/** Get the main Gtk struct */
64 	public GBufferedInputStream* getBufferedInputStreamStruct(bool transferOwnership = false)
65 	{
66 		if (transferOwnership)
67 			ownedRef = false;
68 		return gBufferedInputStream;
69 	}
70 
71 	/** the main Gtk struct as a void* */
72 	protected override void* getStruct()
73 	{
74 		return cast(void*)gBufferedInputStream;
75 	}
76 
77 	protected override void setStruct(GObject* obj)
78 	{
79 		gBufferedInputStream = cast(GBufferedInputStream*)obj;
80 		super.setStruct(obj);
81 	}
82 
83 	/**
84 	 * Sets our main struct and passes it to the parent class.
85 	 */
86 	public this (GBufferedInputStream* gBufferedInputStream, bool ownedRef = false)
87 	{
88 		this.gBufferedInputStream = gBufferedInputStream;
89 		super(cast(GFilterInputStream*)gBufferedInputStream, ownedRef);
90 	}
91 
92 	// add the Seekable capabilities
93 	mixin SeekableT!(GBufferedInputStream);
94 
95 	/**
96 	 * Returns the buffer with the currently available bytes. The returned
97 	 * buffer must not be modified and will become invalid when reading from
98 	 * the stream or filling the buffer.
99 	 *
100 	 * Params:
101 	 *     count = a #gsize to get the number of bytes available in the buffer
102 	 *
103 	 * Return: read-only buffer
104 	 */
105 	public ubyte[] peekBuffer()
106 	{
107 		size_t count;
108 
109 		auto p = g_buffered_input_stream_peek_buffer(gBufferedInputStream, &count);
110 
111 		return (cast(ubyte*)p)[0 .. count];
112 	}
113 
114 	/**
115 	 */
116 
117 	/** */
118 	public static GType getType()
119 	{
120 		return g_buffered_input_stream_get_type();
121 	}
122 
123 	/**
124 	 * Creates a new #GInputStream from the given @base_stream, with
125 	 * a buffer set to the default size (4 kilobytes).
126 	 *
127 	 * Params:
128 	 *     baseStream = a #GInputStream
129 	 *
130 	 * Returns: a #GInputStream for the given @base_stream.
131 	 *
132 	 * Throws: ConstructionException GTK+ fails to create the object.
133 	 */
134 	public this(InputStream baseStream)
135 	{
136 		auto p = g_buffered_input_stream_new((baseStream is null) ? null : baseStream.getInputStreamStruct());
137 
138 		if(p is null)
139 		{
140 			throw new ConstructionException("null returned by new");
141 		}
142 
143 		this(cast(GBufferedInputStream*) p, true);
144 	}
145 
146 	/**
147 	 * Creates a new #GBufferedInputStream from the given @base_stream,
148 	 * with a buffer set to @size.
149 	 *
150 	 * Params:
151 	 *     baseStream = a #GInputStream
152 	 *     size = a #gsize
153 	 *
154 	 * Returns: a #GInputStream.
155 	 *
156 	 * Throws: ConstructionException GTK+ fails to create the object.
157 	 */
158 	public this(InputStream baseStream, size_t size)
159 	{
160 		auto p = g_buffered_input_stream_new_sized((baseStream is null) ? null : baseStream.getInputStreamStruct(), size);
161 
162 		if(p is null)
163 		{
164 			throw new ConstructionException("null returned by new_sized");
165 		}
166 
167 		this(cast(GBufferedInputStream*) p, true);
168 	}
169 
170 	/**
171 	 * Tries to read @count bytes from the stream into the buffer.
172 	 * Will block during this read.
173 	 *
174 	 * If @count is zero, returns zero and does nothing. A value of @count
175 	 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
176 	 *
177 	 * On success, the number of bytes read into the buffer is returned.
178 	 * It is not an error if this is not the same as the requested size, as it
179 	 * can happen e.g. near the end of a file. Zero is returned on end of file
180 	 * (or if @count is zero),  but never otherwise.
181 	 *
182 	 * If @count is -1 then the attempted read size is equal to the number of
183 	 * bytes that are required to fill the buffer.
184 	 *
185 	 * If @cancellable is not %NULL, then the operation can be cancelled by
186 	 * triggering the cancellable object from another thread. If the operation
187 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
188 	 * operation was partially finished when the operation was cancelled the
189 	 * partial result will be returned, without an error.
190 	 *
191 	 * On error -1 is returned and @error is set accordingly.
192 	 *
193 	 * For the asynchronous, non-blocking, version of this function, see
194 	 * g_buffered_input_stream_fill_async().
195 	 *
196 	 * Params:
197 	 *     count = the number of bytes that will be read from the stream
198 	 *     cancellable = optional #GCancellable object, %NULL to ignore
199 	 *
200 	 * Returns: the number of bytes read into @stream's buffer, up to @count,
201 	 *     or -1 on error.
202 	 *
203 	 * Throws: GException on failure.
204 	 */
205 	public ptrdiff_t fill(ptrdiff_t count, Cancellable cancellable)
206 	{
207 		GError* err = null;
208 
209 		auto p = g_buffered_input_stream_fill(gBufferedInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
210 
211 		if (err !is null)
212 		{
213 			throw new GException( new ErrorG(err) );
214 		}
215 
216 		return p;
217 	}
218 
219 	/**
220 	 * Reads data into @stream's buffer asynchronously, up to @count size.
221 	 * @io_priority can be used to prioritize reads. For the synchronous
222 	 * version of this function, see g_buffered_input_stream_fill().
223 	 *
224 	 * If @count is -1 then the attempted read size is equal to the number
225 	 * of bytes that are required to fill the buffer.
226 	 *
227 	 * Params:
228 	 *     count = the number of bytes that will be read from the stream
229 	 *     ioPriority = the [I/O priority][io-priority] of the request
230 	 *     cancellable = optional #GCancellable object
231 	 *     callback = a #GAsyncReadyCallback
232 	 *     userData = a #gpointer
233 	 */
234 	public void fillAsync(ptrdiff_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
235 	{
236 		g_buffered_input_stream_fill_async(gBufferedInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
237 	}
238 
239 	/**
240 	 * Finishes an asynchronous read.
241 	 *
242 	 * Params:
243 	 *     result = a #GAsyncResult
244 	 *
245 	 * Returns: a #gssize of the read stream, or %-1 on an error.
246 	 *
247 	 * Throws: GException on failure.
248 	 */
249 	public ptrdiff_t fillFinish(AsyncResultIF result)
250 	{
251 		GError* err = null;
252 
253 		auto p = g_buffered_input_stream_fill_finish(gBufferedInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err);
254 
255 		if (err !is null)
256 		{
257 			throw new GException( new ErrorG(err) );
258 		}
259 
260 		return p;
261 	}
262 
263 	/**
264 	 * Gets the size of the available data within the stream.
265 	 *
266 	 * Returns: size of the available stream.
267 	 */
268 	public size_t getAvailable()
269 	{
270 		return g_buffered_input_stream_get_available(gBufferedInputStream);
271 	}
272 
273 	/**
274 	 * Gets the size of the input buffer.
275 	 *
276 	 * Returns: the current buffer size.
277 	 */
278 	public size_t getBufferSize()
279 	{
280 		return g_buffered_input_stream_get_buffer_size(gBufferedInputStream);
281 	}
282 
283 	/**
284 	 * Peeks in the buffer, copying data of size @count into @buffer,
285 	 * offset @offset bytes.
286 	 *
287 	 * Params:
288 	 *     buffer = a pointer to
289 	 *         an allocated chunk of memory
290 	 *     offset = a #gsize
291 	 *
292 	 * Returns: a #gsize of the number of bytes peeked, or -1 on error.
293 	 */
294 	public size_t peek(ubyte[] buffer, size_t offset)
295 	{
296 		return g_buffered_input_stream_peek(gBufferedInputStream, buffer.ptr, offset, cast(size_t)buffer.length);
297 	}
298 
299 	/**
300 	 * Tries to read a single byte from the stream or the buffer. Will block
301 	 * during this read.
302 	 *
303 	 * On success, the byte read from the stream is returned. On end of stream
304 	 * -1 is returned but it's not an exceptional error and @error is not set.
305 	 *
306 	 * If @cancellable is not %NULL, then the operation can be cancelled by
307 	 * triggering the cancellable object from another thread. If the operation
308 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
309 	 * operation was partially finished when the operation was cancelled the
310 	 * partial result will be returned, without an error.
311 	 *
312 	 * On error -1 is returned and @error is set accordingly.
313 	 *
314 	 * Params:
315 	 *     cancellable = optional #GCancellable object, %NULL to ignore
316 	 *
317 	 * Returns: the byte read from the @stream, or -1 on end of stream or error.
318 	 *
319 	 * Throws: GException on failure.
320 	 */
321 	public int readByte(Cancellable cancellable)
322 	{
323 		GError* err = null;
324 
325 		auto p = g_buffered_input_stream_read_byte(gBufferedInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
326 
327 		if (err !is null)
328 		{
329 			throw new GException( new ErrorG(err) );
330 		}
331 
332 		return p;
333 	}
334 
335 	/**
336 	 * Sets the size of the internal buffer of @stream to @size, or to the
337 	 * size of the contents of the buffer. The buffer can never be resized
338 	 * smaller than its current contents.
339 	 *
340 	 * Params:
341 	 *     size = a #gsize
342 	 */
343 	public void setBufferSize(size_t size)
344 	{
345 		g_buffered_input_stream_set_buffer_size(gBufferedInputStream, size);
346 	}
347 }