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 glib.ConstructionException;
34 private import glib.ErrorG;
35 private import glib.GException;
36 private import gobject.ObjectG;
37 private import gtkc.gio;
38 public  import gtkc.giotypes;
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 	protected override void setStruct(GObject* obj)
77 	{
78 		gBufferedInputStream = cast(GBufferedInputStream*)obj;
79 		super.setStruct(obj);
80 	}
81 
82 	/**
83 	 * Sets our main struct and passes it to the parent class.
84 	 */
85 	public this (GBufferedInputStream* gBufferedInputStream, bool ownedRef = false)
86 	{
87 		this.gBufferedInputStream = gBufferedInputStream;
88 		super(cast(GFilterInputStream*)gBufferedInputStream, ownedRef);
89 	}
90 
91 	// add the Seekable capabilities
92 	mixin SeekableT!(GBufferedInputStream);
93 
94 	/**
95 	 * Returns the buffer with the currently available bytes. The returned
96 	 * buffer must not be modified and will become invalid when reading from
97 	 * the stream or filling the buffer.
98 	 *
99 	 * Params:
100 	 *     count = a #gsize to get the number of bytes available in the buffer
101 	 *
102 	 * Return: read-only buffer
103 	 */
104 	public ubyte[] peekBuffer()
105 	{
106 		size_t count;
107 		
108 		auto p = g_buffered_input_stream_peek_buffer(gBufferedInputStream, &count);
109 		
110 		return (cast(ubyte*)p)[0 .. count];
111 	}
112 
113 	/**
114 	 */
115 
116 	/** */
117 	public static GType getType()
118 	{
119 		return g_buffered_input_stream_get_type();
120 	}
121 
122 	/**
123 	 * Creates a new #GInputStream from the given @base_stream, with
124 	 * a buffer set to the default size (4 kilobytes).
125 	 *
126 	 * Params:
127 	 *     baseStream = a #GInputStream
128 	 *
129 	 * Returns: a #GInputStream for the given @base_stream.
130 	 *
131 	 * Throws: ConstructionException GTK+ fails to create the object.
132 	 */
133 	public this(InputStream baseStream)
134 	{
135 		auto p = g_buffered_input_stream_new((baseStream is null) ? null : baseStream.getInputStreamStruct());
136 		
137 		if(p is null)
138 		{
139 			throw new ConstructionException("null returned by new");
140 		}
141 		
142 		this(cast(GBufferedInputStream*) p, true);
143 	}
144 
145 	/**
146 	 * Creates a new #GBufferedInputStream from the given @base_stream,
147 	 * with a buffer set to @size.
148 	 *
149 	 * Params:
150 	 *     baseStream = a #GInputStream
151 	 *     size = a #gsize
152 	 *
153 	 * Returns: a #GInputStream.
154 	 *
155 	 * Throws: ConstructionException GTK+ fails to create the object.
156 	 */
157 	public this(InputStream baseStream, size_t size)
158 	{
159 		auto p = g_buffered_input_stream_new_sized((baseStream is null) ? null : baseStream.getInputStreamStruct(), size);
160 		
161 		if(p is null)
162 		{
163 			throw new ConstructionException("null returned by new_sized");
164 		}
165 		
166 		this(cast(GBufferedInputStream*) p, true);
167 	}
168 
169 	/**
170 	 * Tries to read @count bytes from the stream into the buffer.
171 	 * Will block during this read.
172 	 *
173 	 * If @count is zero, returns zero and does nothing. A value of @count
174 	 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
175 	 *
176 	 * On success, the number of bytes read into the buffer is returned.
177 	 * It is not an error if this is not the same as the requested size, as it
178 	 * can happen e.g. near the end of a file. Zero is returned on end of file
179 	 * (or if @count is zero),  but never otherwise.
180 	 *
181 	 * If @count is -1 then the attempted read size is equal to the number of
182 	 * bytes that are required to fill the buffer.
183 	 *
184 	 * If @cancellable is not %NULL, then the operation can be cancelled by
185 	 * triggering the cancellable object from another thread. If the operation
186 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
187 	 * operation was partially finished when the operation was cancelled the
188 	 * partial result will be returned, without an error.
189 	 *
190 	 * On error -1 is returned and @error is set accordingly.
191 	 *
192 	 * For the asynchronous, non-blocking, version of this function, see
193 	 * g_buffered_input_stream_fill_async().
194 	 *
195 	 * Params:
196 	 *     count = the number of bytes that will be read from the stream
197 	 *     cancellable = optional #GCancellable object, %NULL to ignore
198 	 *
199 	 * Returns: the number of bytes read into @stream's buffer, up to @count,
200 	 *     or -1 on error.
201 	 *
202 	 * Throws: GException on failure.
203 	 */
204 	public ptrdiff_t fill(ptrdiff_t count, Cancellable cancellable)
205 	{
206 		GError* err = null;
207 		
208 		auto p = g_buffered_input_stream_fill(gBufferedInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
209 		
210 		if (err !is null)
211 		{
212 			throw new GException( new ErrorG(err) );
213 		}
214 		
215 		return p;
216 	}
217 
218 	/**
219 	 * Reads data into @stream's buffer asynchronously, up to @count size.
220 	 * @io_priority can be used to prioritize reads. For the synchronous
221 	 * version of this function, see g_buffered_input_stream_fill().
222 	 *
223 	 * If @count is -1 then the attempted read size is equal to the number
224 	 * of bytes that are required to fill the buffer.
225 	 *
226 	 * Params:
227 	 *     count = the number of bytes that will be read from the stream
228 	 *     ioPriority = the [I/O priority][io-priority] of the request
229 	 *     cancellable = optional #GCancellable object
230 	 *     callback = a #GAsyncReadyCallback
231 	 *     userData = a #gpointer
232 	 */
233 	public void fillAsync(ptrdiff_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
234 	{
235 		g_buffered_input_stream_fill_async(gBufferedInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
236 	}
237 
238 	/**
239 	 * Finishes an asynchronous read.
240 	 *
241 	 * Params:
242 	 *     result = a #GAsyncResult
243 	 *
244 	 * Returns: a #gssize of the read stream, or %-1 on an error.
245 	 *
246 	 * Throws: GException on failure.
247 	 */
248 	public ptrdiff_t fillFinish(AsyncResultIF result)
249 	{
250 		GError* err = null;
251 		
252 		auto p = g_buffered_input_stream_fill_finish(gBufferedInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err);
253 		
254 		if (err !is null)
255 		{
256 			throw new GException( new ErrorG(err) );
257 		}
258 		
259 		return p;
260 	}
261 
262 	/**
263 	 * Gets the size of the available data within the stream.
264 	 *
265 	 * Returns: size of the available stream.
266 	 */
267 	public size_t getAvailable()
268 	{
269 		return g_buffered_input_stream_get_available(gBufferedInputStream);
270 	}
271 
272 	/**
273 	 * Gets the size of the input buffer.
274 	 *
275 	 * Returns: the current buffer size.
276 	 */
277 	public size_t getBufferSize()
278 	{
279 		return g_buffered_input_stream_get_buffer_size(gBufferedInputStream);
280 	}
281 
282 	/**
283 	 * Peeks in the buffer, copying data of size @count into @buffer,
284 	 * offset @offset bytes.
285 	 *
286 	 * Params:
287 	 *     buffer = a pointer to
288 	 *         an allocated chunk of memory
289 	 *     offset = a #gsize
290 	 *     count = 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 }