summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/store/IndexOutput.h
blob: c47ee73a7df1d343e3472a2a2990d623183e036e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#ifndef _lucene_store_IndexOutput_
#define _lucene_store_IndexOutput_
#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

CL_NS_DEF(store)


/** Abstract class for output to a file in a Directory.  A random-access output
* stream.  Used for all Lucene index output operations.
* @see Directory
* @see IndexInput
*/
class IndexOutput:LUCENE_BASE{
	bool isclosed;
public:
	IndexOutput();
	virtual ~IndexOutput();

	/** Writes a single byte.
	* @see IndexInput#readByte()
	*/
	virtual void writeByte(const uint8_t b) = 0;

	/** Writes an array of bytes.
	* @param b the bytes to write
	* @param length the number of bytes to write
	* @see IndexInput#readBytes(byte[],int32_t,int32_t)
	*/
	virtual void writeBytes(const uint8_t* b, const int32_t length) = 0;

	/** Writes an int as four bytes.
	* @see IndexInput#readInt()
	*/
	void writeInt(const int32_t i);

	/** Writes an int in a variable-length format.  Writes between one and
	* five bytes.  Smaller values take fewer bytes.  Negative numbers are not
	* supported.
	* @see IndexInput#readVInt()
	*/
	void writeVInt(const int32_t vi);

	/** Writes a long as eight bytes.
	* @see IndexInput#readLong()
	*/
	void writeLong(const int64_t i);

	/** Writes an long in a variable-length format.  Writes between one and five
	* bytes.  Smaller values take fewer bytes.  Negative numbers are not
	* supported.
	* @see IndexInput#readVLong()
	*/
	void writeVLong(const int64_t vi);

	/** Writes a string.
	* @see IndexInput#readString()
	*/
	void writeString(const TCHAR* s, const int32_t length);

	/** Writes a sequence of UTF-8 encoded characters from a string.
	* @param s the source of the characters
	* @param start the first character in the sequence
	* @param length the number of characters in the sequence
	* @see IndexInput#readChars(char[],int32_t,int32_t)
	*/
	void writeChars(const TCHAR* s, const int32_t start, const int32_t length);

	/** Closes this stream to further operations. */
	virtual void close() = 0;

	/** Returns the current position in this file, where the next write will
	* occur.
	* @see #seek(long)
	*/
	virtual int64_t getFilePointer() const = 0;

	/** Sets current position in this file, where the next write will occur.
	* @see #getFilePointer()
	*/
	virtual void seek(const int64_t pos) = 0;

	/** The number of bytes in the file. */
	virtual int64_t length() = 0;

	/** Forces any buffered output to be written. */
	virtual void flush() = 0;
};

/** Base implementation class for buffered {@link IndexOutput}. */
class BufferedIndexOutput : public IndexOutput{
public:
	LUCENE_STATIC_CONSTANT(int32_t, BUFFER_SIZE=LUCENE_STREAM_BUFFER_SIZE);
private:
	uint8_t* buffer;
	int64_t bufferStart;			  // position in file of buffer
	int32_t bufferPosition;		  // position in buffer

public:
	BufferedIndexOutput();
	virtual ~BufferedIndexOutput();

	/** Writes a single byte.
	* @see IndexInput#readByte()
	*/
	virtual void writeByte(const uint8_t b);

	/** Writes an array of bytes.
	* @param b the bytes to write
	* @param length the number of bytes to write
	* @see IndexInput#readBytes(byte[],int32_t,int32_t)
	*/
	virtual void writeBytes(const uint8_t* b, const int32_t length);

	/** Closes this stream to further operations. */
	virtual void close();

	/** Returns the current position in this file, where the next write will
	* occur.
	* @see #seek(long)
	*/
	int64_t getFilePointer() const;

	/** Sets current position in this file, where the next write will occur.
	* @see #getFilePointer()
	*/
	virtual void seek(const int64_t pos);

	/** The number of bytes in the file. */
	virtual int64_t length() = 0;

	/** Forces any buffered output to be written. */
	void flush();

protected:
	/** Expert: implements buffer write.  Writes bytes at the current position in
	* the output.
	* @param b the bytes to write
	* @param len the number of bytes to write
	*/
	virtual void flushBuffer(const uint8_t* b, const int32_t len) = 0;
};

CL_NS_END
#endif