xref: /haiku/src/add-ons/translators/shared/StreamBuffer.h (revision c56079fb2280536dcae1e0f481f127a78a9acb83)
1*c56079fbSMatthew Wilber /*****************************************************************************/
2*c56079fbSMatthew Wilber // StreamBuffer
3*c56079fbSMatthew Wilber // Written by Michael Wilber, OBOS Translation Kit Team
4*c56079fbSMatthew Wilber //
5*c56079fbSMatthew Wilber // StreamBuffer.h
6*c56079fbSMatthew Wilber //
7*c56079fbSMatthew Wilber // This class is for buffering data from a BPositionIO object in order to
8*c56079fbSMatthew Wilber // improve performance for cases when small amounts of data are frequently
9*c56079fbSMatthew Wilber // read from a BPositionIO object.
10*c56079fbSMatthew Wilber //
11*c56079fbSMatthew Wilber //
12*c56079fbSMatthew Wilber // Copyright (c) 2003 OpenBeOS Project
13*c56079fbSMatthew Wilber //
14*c56079fbSMatthew Wilber // Permission is hereby granted, free of charge, to any person obtaining a
15*c56079fbSMatthew Wilber // copy of this software and associated documentation files (the "Software"),
16*c56079fbSMatthew Wilber // to deal in the Software without restriction, including without limitation
17*c56079fbSMatthew Wilber // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18*c56079fbSMatthew Wilber // and/or sell copies of the Software, and to permit persons to whom the
19*c56079fbSMatthew Wilber // Software is furnished to do so, subject to the following conditions:
20*c56079fbSMatthew Wilber //
21*c56079fbSMatthew Wilber // The above copyright notice and this permission notice shall be included
22*c56079fbSMatthew Wilber // in all copies or substantial portions of the Software.
23*c56079fbSMatthew Wilber //
24*c56079fbSMatthew Wilber // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25*c56079fbSMatthew Wilber // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26*c56079fbSMatthew Wilber // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27*c56079fbSMatthew Wilber // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28*c56079fbSMatthew Wilber // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29*c56079fbSMatthew Wilber // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30*c56079fbSMatthew Wilber // DEALINGS IN THE SOFTWARE.
31*c56079fbSMatthew Wilber /*****************************************************************************/
32*c56079fbSMatthew Wilber 
33*c56079fbSMatthew Wilber #ifndef STREAM_BUFFER_H
34*c56079fbSMatthew Wilber #define STREAM_BUFFER_H
35*c56079fbSMatthew Wilber 
36*c56079fbSMatthew Wilber #include <DataIO.h>
37*c56079fbSMatthew Wilber 
38*c56079fbSMatthew Wilber #define MIN_BUFFER_SIZE 512
39*c56079fbSMatthew Wilber 
40*c56079fbSMatthew Wilber class StreamBuffer {
41*c56079fbSMatthew Wilber public:
42*c56079fbSMatthew Wilber 	StreamBuffer(BPositionIO *pstream, size_t nbuffersize, bool binitialread);
43*c56079fbSMatthew Wilber 	~StreamBuffer();
44*c56079fbSMatthew Wilber 
45*c56079fbSMatthew Wilber 	status_t InitCheck();
46*c56079fbSMatthew Wilber 		// Determines whether the constructor failed or not
47*c56079fbSMatthew Wilber 
48*c56079fbSMatthew Wilber 	ssize_t Read(uint8 *pinto, size_t nbytes);
49*c56079fbSMatthew Wilber 		// copy nbytes from the stream into pinto
50*c56079fbSMatthew Wilber 
51*c56079fbSMatthew Wilber 	bool Seek(off_t position);
52*c56079fbSMatthew Wilber 		// seek the stream to the given position
53*c56079fbSMatthew Wilber 
54*c56079fbSMatthew Wilber private:
55*c56079fbSMatthew Wilber 	ssize_t ReadStream();
56*c56079fbSMatthew Wilber 		// Load the stream buffer from the stream
57*c56079fbSMatthew Wilber 
58*c56079fbSMatthew Wilber 	BPositionIO *fpStream;
59*c56079fbSMatthew Wilber 		// stream object this object is buffering
60*c56079fbSMatthew Wilber 	uint8 *fpBuffer;
61*c56079fbSMatthew Wilber 		// buffered data from fpStream
62*c56079fbSMatthew Wilber 	size_t fnBufferSize;
63*c56079fbSMatthew Wilber 		// number of bytes of memory allocated for fpBuffer
64*c56079fbSMatthew Wilber 	size_t fnLen;
65*c56079fbSMatthew Wilber 		// number of bytes of actual data in fpBuffer
66*c56079fbSMatthew Wilber 	size_t fnPos;
67*c56079fbSMatthew Wilber 		// current position in the buffer
68*c56079fbSMatthew Wilber };
69*c56079fbSMatthew Wilber 
70*c56079fbSMatthew Wilber #endif
71