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 | #if defined(_LUCENE_PRAGMA_ONCE) |
10 | # pragma once |
11 | #endif |
12 | |
13 | CL_NS_DEF(store) |
14 | |
15 | |
16 | /** Abstract class for output to a file in a Directory. A random-access output |
17 | * stream. Used for all Lucene index output operations. |
18 | * @see Directory |
19 | * @see IndexInput |
20 | */ |
21 | class IndexOutput:LUCENE_BASE{ |
22 | bool isclosed; |
23 | public: |
24 | IndexOutput(); |
25 | virtual ~IndexOutput(); |
26 | |
27 | /** Writes a single byte. |
28 | * @see IndexInput#readByte() |
29 | */ |
30 | virtual void writeByte(const uint8_t b) = 0; |
31 | |
32 | /** Writes an array of bytes. |
33 | * @param b the bytes to write |
34 | * @param length the number of bytes to write |
35 | * @see IndexInput#readBytes(byte[],int32_t,int32_t) |
36 | */ |
37 | virtual void writeBytes(const uint8_t* b, const int32_t length) = 0; |
38 | |
39 | /** Writes an int as four bytes. |
40 | * @see IndexInput#readInt() |
41 | */ |
42 | void writeInt(const int32_t i); |
43 | |
44 | /** Writes an int in a variable-length format. Writes between one and |
45 | * five bytes. Smaller values take fewer bytes. Negative numbers are not |
46 | * supported. |
47 | * @see IndexInput#readVInt() |
48 | */ |
49 | void writeVInt(const int32_t vi); |
50 | |
51 | /** Writes a long as eight bytes. |
52 | * @see IndexInput#readLong() |
53 | */ |
54 | void writeLong(const int64_t i); |
55 | |
56 | /** Writes an long in a variable-length format. Writes between one and five |
57 | * bytes. Smaller values take fewer bytes. Negative numbers are not |
58 | * supported. |
59 | * @see IndexInput#readVLong() |
60 | */ |
61 | void writeVLong(const int64_t vi); |
62 | |
63 | /** Writes a string. |
64 | * @see IndexInput#readString() |
65 | */ |
66 | void writeString(const TCHAR* s, const int32_t length); |
67 | |
68 | /** Writes a sequence of UTF-8 encoded characters from a string. |
69 | * @param s the source of the characters |
70 | * @param start the first character in the sequence |
71 | * @param length the number of characters in the sequence |
72 | * @see IndexInput#readChars(char[],int32_t,int32_t) |
73 | */ |
74 | void writeChars(const TCHAR* s, const int32_t start, const int32_t length); |
75 | |
76 | /** Closes this stream to further operations. */ |
77 | virtual void close() = 0; |
78 | |
79 | /** Returns the current position in this file, where the next write will |
80 | * occur. |
81 | * @see #seek(long) |
82 | */ |
83 | virtual int64_t getFilePointer() const = 0; |
84 | |
85 | /** Sets current position in this file, where the next write will occur. |
86 | * @see #getFilePointer() |
87 | */ |
88 | virtual void seek(const int64_t pos) = 0; |
89 | |
90 | /** The number of bytes in the file. */ |
91 | virtual int64_t length() = 0; |
92 | |
93 | /** Forces any buffered output to be written. */ |
94 | virtual void flush() = 0; |
95 | }; |
96 | |
97 | /** Base implementation class for buffered {@link IndexOutput}. */ |
98 | class BufferedIndexOutput : public IndexOutput{ |
99 | public: |
100 | LUCENE_STATIC_CONSTANT(int32_t, BUFFER_SIZE=LUCENE_STREAM_BUFFER_SIZE); |
101 | private: |
102 | uint8_t* buffer; |
103 | int64_t bufferStart; // position in file of buffer |
104 | int32_t bufferPosition; // position in buffer |
105 | |
106 | public: |
107 | BufferedIndexOutput(); |
108 | virtual ~BufferedIndexOutput(); |
109 | |
110 | /** Writes a single byte. |
111 | * @see IndexInput#readByte() |
112 | */ |
113 | virtual void writeByte(const uint8_t b); |
114 | |
115 | /** Writes an array of bytes. |
116 | * @param b the bytes to write |
117 | * @param length the number of bytes to write |
118 | * @see IndexInput#readBytes(byte[],int32_t,int32_t) |
119 | */ |
120 | virtual void writeBytes(const uint8_t* b, const int32_t length); |
121 | |
122 | /** Closes this stream to further operations. */ |
123 | virtual void close(); |
124 | |
125 | /** Returns the current position in this file, where the next write will |
126 | * occur. |
127 | * @see #seek(long) |
128 | */ |
129 | int64_t getFilePointer() const; |
130 | |
131 | /** Sets current position in this file, where the next write will occur. |
132 | * @see #getFilePointer() |
133 | */ |
134 | virtual void seek(const int64_t pos); |
135 | |
136 | /** The number of bytes in the file. */ |
137 | virtual int64_t length() = 0; |
138 | |
139 | /** Forces any buffered output to be written. */ |
140 | void flush(); |
141 | |
142 | protected: |
143 | /** Expert: implements buffer write. Writes bytes at the current position in |
144 | * the output. |
145 | * @param b the bytes to write |
146 | * @param len the number of bytes to write |
147 | */ |
148 | virtual void flushBuffer(const uint8_t* b, const int32_t len) = 0; |
149 | }; |
150 | |
151 | CL_NS_END |
152 | #endif |
153 | |