1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #ifndef _lucene_store_IndexOutput_
8 #define _lucene_store_IndexOutput_
9 
CL_NS_DEF(store)10 CL_NS_DEF(store)
11 
12 class IndexInput;
13 
14 /** Abstract class for output to a file in a Directory.  A random-access output
15 * stream.  Used for all Lucene index output operations.
16 * @see Directory
17 * @see IndexInput
18 */
19 class CLUCENE_EXPORT IndexOutput:LUCENE_BASE{
20 	bool isclosed;
21 public:
22 	IndexOutput();
23 	virtual ~IndexOutput();
24 
25 	/** Writes a single byte.
26 	* @see IndexInput#readByte()
27 	*/
28 	virtual void writeByte(const uint8_t b) = 0;
29 
30 	/** Writes an array of bytes.
31 	* @param b the bytes to write
32 	* @param length the number of bytes to write
33 	* @see IndexInput#readBytes(uint8_t*,int32_t)
34 	*/
35 	virtual void writeBytes(const uint8_t* b, const int32_t length) = 0;
36 
37 	/** Writes an int as four bytes.
38 	* @see IndexInput#readInt()
39 	*/
40 	void writeInt(const int32_t i);
41 
42 	/** Writes an int in a variable-length format.  Writes between one and
43 	* five bytes.  Smaller values take fewer bytes.  Negative numbers are not
44 	* supported.
45 	* @see IndexInput#readVInt()
46 	*/
47 	void writeVInt(const int32_t vi);
48 
49 	/** Writes a long as eight bytes.
50 	* @see IndexInput#readLong()
51 	*/
52 	void writeLong(const int64_t i);
53 
54 	/** Writes an long in a variable-length format.  Writes between one and five
55 	* bytes.  Smaller values take fewer bytes.  Negative numbers are not
56 	* supported.
57 	* @see IndexInput#readVLong()
58 	*/
59 	void writeVLong(const int64_t vi);
60 
61 	/** Writes a string.
62 	* @see IndexInput#readString()
63 	*/
64 	void writeString(const TCHAR* s, const int32_t length);
65     void writeString(const std::string& s);
66 
67 	#ifdef _UCS2
68 	/** Writes an ascii string. converts to TCHAR* before writing
69 	* @see IndexInput#readString()
70 	*/
71 	void writeString(const char* s, const int32_t length);
72 	#endif
73 
74 	/** Writes a sequence of UTF-8 encoded characters from a string.
75 	* @param s the source of the characters
76 	* @param start the first character in the sequence
77 	* @param length the number of characters in the sequence
78 	* @see IndexInput#readChars(char[],int32_t,int32_t)
79 	*/
80 	void writeChars(const TCHAR* s, const int32_t length);
81 
82 	/** Closes this stream to further operations. */
83 	virtual void close() = 0;
84 
85 	/** Returns the current position in this file, where the next write will
86 	* occur.
87 	* @see #seek(long)
88 	*/
89 	virtual int64_t getFilePointer() const = 0;
90 
91 	/** Sets current position in this file, where the next write will occur.
92 	* @see #getFilePointer()
93 	*/
94 	virtual void seek(const int64_t pos) = 0;
95 
96 	/** The number of bytes in the file. */
97 	virtual int64_t length() const = 0;
98 
99 	/** Forces any buffered output to be written. */
100 	virtual void flush() = 0;
101 
102 private:
103 	LUCENE_STATIC_CONSTANT(int32_t, COPY_BUFFER_SIZE = 16384);
104 	uint8_t* copyBuffer;
105 
106 public:
107 	/** Copy numBytes bytes from input to ourself. */
108 	void copyBytes(CL_NS(store)::IndexInput* input, int64_t numBytes);
109 };
110 
111 /** Base implementation class for buffered {@link IndexOutput}. */
112 class CLUCENE_EXPORT BufferedIndexOutput : public IndexOutput{
113 public:
114 	LUCENE_STATIC_CONSTANT(int32_t, BUFFER_SIZE=16384);
115 private:
116 	uint8_t* buffer;
117 	int64_t bufferStart;			  // position in file of buffer
118 	int32_t bufferPosition;		  // position in buffer
119 
120 public:
121 	BufferedIndexOutput();
122 	virtual ~BufferedIndexOutput();
123 
124 	/** Writes a single byte.
125 	* @see IndexInput#readByte()
126 	*/
127 	virtual void writeByte(const uint8_t b);
128 
129 	/** Writes an array of bytes.
130 	* @param b the bytes to write
131 	* @param length the number of bytes to write
132 	* @see IndexInput#readBytes(byte[],int32_t,int32_t)
133 	*/
134 	virtual void writeBytes(const uint8_t* b, const int32_t length);
135 
136 	/** Closes this stream to further operations. */
137 	virtual void close();
138 
139 	/** Returns the current position in this file, where the next write will
140 	* occur.
141 	* @see #seek(long)
142 	*/
143 	int64_t getFilePointer() const;
144 
145 	/** Sets current position in this file, where the next write will occur.
146 	* @see #getFilePointer()
147 	*/
148 	virtual void seek(const int64_t pos);
149 
150 	/** The number of bytes in the file. */
151 	virtual int64_t length() const = 0;
152 
153 	/** Forces any buffered output to be written. */
154 	void flush();
155 
156 protected:
157 	/** Expert: implements buffer write.  Writes bytes at the current position in
158 	* the output.
159 	* @param b the bytes to write
160 	* @param len the number of bytes to write
161 	*/
162 	virtual void flushBuffer(const uint8_t* b, const int32_t len) = 0;
163 };
164 
165 CL_NS_END
166 #endif
167