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