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