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