xref: /haiku/src/add-ons/translators/shared/StreamBuffer.h (revision e26a1248187d784035575172e2dda87237dab8f8)
1*e26a1248SJérôme Duval /*
2*e26a1248SJérôme Duval  * Copyright 2003-2008, Haiku, Inc. All rights reserved.
3*e26a1248SJérôme Duval  * Distributed under the terms of the MIT License.
4*e26a1248SJérôme Duval  *
5*e26a1248SJérôme Duval  * Authors :
6*e26a1248SJérôme Duval  *		Michael Wilber
7*e26a1248SJérôme Duval  *		Jérôme Duval
8*e26a1248SJérôme Duval  */
9c56079fbSMatthew Wilber 
10c56079fbSMatthew Wilber #ifndef STREAM_BUFFER_H
11c56079fbSMatthew Wilber #define STREAM_BUFFER_H
12c56079fbSMatthew Wilber 
13c56079fbSMatthew Wilber #include <DataIO.h>
14c56079fbSMatthew Wilber 
15c56079fbSMatthew Wilber #define MIN_BUFFER_SIZE 512
16c56079fbSMatthew Wilber 
17c56079fbSMatthew Wilber class StreamBuffer {
18c56079fbSMatthew Wilber public:
19*e26a1248SJérôme Duval 	StreamBuffer(BPositionIO *stream, size_t bufferSize, bool toRead = true);
20c56079fbSMatthew Wilber 	~StreamBuffer();
21c56079fbSMatthew Wilber 
22c56079fbSMatthew Wilber 	status_t InitCheck();
23c56079fbSMatthew Wilber 		// Determines whether the constructor failed or not
24c56079fbSMatthew Wilber 
25*e26a1248SJérôme Duval 	ssize_t Read(void *buffer, size_t size);
26*e26a1248SJérôme Duval 		// copy nbytes from the stream into pinto
27*e26a1248SJérôme Duval 
28*e26a1248SJérôme Duval 	void Write(void *buffer, size_t size);
29c56079fbSMatthew Wilber 		// copy nbytes from the stream into pinto
30c56079fbSMatthew Wilber 
31c56079fbSMatthew Wilber 	bool Seek(off_t position);
32c56079fbSMatthew Wilber 		// seek the stream to the given position
33c56079fbSMatthew Wilber 
34c56079fbSMatthew Wilber private:
35*e26a1248SJérôme Duval 	ssize_t _ReadStream();
36c56079fbSMatthew Wilber 		// Load the stream buffer from the stream
37c56079fbSMatthew Wilber 
38*e26a1248SJérôme Duval 	BPositionIO *fStream;
39c56079fbSMatthew Wilber 		// stream object this object is buffering
40*e26a1248SJérôme Duval 	uint8 *fBuffer;
41c56079fbSMatthew Wilber 		// buffered data from fpStream
42*e26a1248SJérôme Duval 	size_t fBufferSize;
43c56079fbSMatthew Wilber 		// number of bytes of memory allocated for fpBuffer
44*e26a1248SJérôme Duval 	size_t fLen;
45c56079fbSMatthew Wilber 		// number of bytes of actual data in fpBuffer
46*e26a1248SJérôme Duval 	size_t fPos;
47c56079fbSMatthew Wilber 		// current position in the buffer
48*e26a1248SJérôme Duval 	bool fToRead;
49*e26a1248SJérôme Duval 		// whether the stream is to be read.
50c56079fbSMatthew Wilber };
51c56079fbSMatthew Wilber 
52c56079fbSMatthew Wilber #endif
53