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