xref: /haiku/src/add-ons/translators/shared/StreamBuffer.h (revision ac437673494e7a878b2bdc0661480621721b846e)
1e26a1248SJérôme Duval /*
2e26a1248SJérôme Duval  * Copyright 2003-2008, Haiku, Inc. All rights reserved.
3e26a1248SJérôme Duval  * Distributed under the terms of the MIT License.
4e26a1248SJérôme Duval  *
5e26a1248SJérôme Duval  * Authors :
6e26a1248SJérôme Duval  *		Michael Wilber
7e26a1248SJérôme Duval  *		Jérôme Duval
8e26a1248SJé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:
19e26a1248SJé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 
25e26a1248SJérôme Duval 	ssize_t Read(void *buffer, size_t size);
26e26a1248SJérôme Duval 		// copy nbytes from the stream into pinto
27e26a1248SJérôme Duval 
28e26a1248SJérôme Duval 	void Write(void *buffer, size_t size);
29c56079fbSMatthew Wilber 		// copy nbytes from the stream into pinto
30c56079fbSMatthew Wilber 
31*ac437673SJérôme Duval 	off_t Seek(off_t position, uint32 seekMode);
32c56079fbSMatthew Wilber 		// seek the stream to the given position
33c56079fbSMatthew Wilber 
3445ff9decSJérôme Duval 	off_t Position();
3545ff9decSJérôme Duval 		// return the actual position
36c56079fbSMatthew Wilber private:
37e26a1248SJérôme Duval 	ssize_t _ReadStream();
38c56079fbSMatthew Wilber 		// Load the stream buffer from the stream
39c56079fbSMatthew Wilber 
40e26a1248SJérôme Duval 	BPositionIO *fStream;
41c56079fbSMatthew Wilber 		// stream object this object is buffering
42e26a1248SJérôme Duval 	uint8 *fBuffer;
43c56079fbSMatthew Wilber 		// buffered data from fpStream
44e26a1248SJérôme Duval 	size_t fBufferSize;
45c56079fbSMatthew Wilber 		// number of bytes of memory allocated for fpBuffer
46e26a1248SJérôme Duval 	size_t fLen;
47c56079fbSMatthew Wilber 		// number of bytes of actual data in fpBuffer
48e26a1248SJérôme Duval 	size_t fPos;
49c56079fbSMatthew Wilber 		// current position in the buffer
50e26a1248SJérôme Duval 	bool fToRead;
51e26a1248SJérôme Duval 		// whether the stream is to be read.
52c56079fbSMatthew Wilber };
53c56079fbSMatthew Wilber 
54c56079fbSMatthew Wilber #endif
55