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