17
17
* under the License.
18
18
*/
19
19
20
- package org .elasticsearch .common .lucene .store ;
21
-
22
- import org .apache .lucene .store .BufferedIndexOutput ;
23
- import org .apache .lucene .store .IndexOutput ;
20
+ package org .apache .lucene .store ;
24
21
25
22
import java .io .IOException ;
26
23
import java .util .zip .Checksum ;
29
26
*/
30
27
public class BufferedChecksumIndexOutput extends BufferedIndexOutput {
31
28
32
- private final IndexOutput out ;
33
-
29
+ private final IndexOutput delegate ;
30
+ private final BufferedIndexOutput bufferedDelegate ;
34
31
private final Checksum digest ;
35
32
36
- public BufferedChecksumIndexOutput (IndexOutput out , Checksum digest ) {
37
- // we add 8 to be bigger than the default BufferIndexOutput buffer size so any flush will go directly
38
- // to the output without being copied over to the delegate buffer
39
- super (BufferedIndexOutput .DEFAULT_BUFFER_SIZE + 64 );
40
- this .out = out ;
33
+ public BufferedChecksumIndexOutput (IndexOutput delegate , Checksum digest ) {
34
+ super (delegate instanceof BufferedIndexOutput ? ((BufferedIndexOutput ) delegate ).getBufferSize () : BufferedIndexOutput .DEFAULT_BUFFER_SIZE );
35
+ if (delegate instanceof BufferedIndexOutput ) {
36
+ bufferedDelegate = (BufferedIndexOutput ) delegate ;
37
+ this .delegate = delegate ;
38
+ } else {
39
+ this .delegate = delegate ;
40
+ bufferedDelegate = null ;
41
+ }
41
42
this .digest = digest ;
42
43
}
43
44
@@ -46,7 +47,7 @@ public Checksum digest() {
46
47
}
47
48
48
49
public IndexOutput underlying () {
49
- return this .out ;
50
+ return this .delegate ;
50
51
}
51
52
52
53
// don't override it, base class method simple reads from input and writes to this output
@@ -59,14 +60,18 @@ public void close() throws IOException {
59
60
try {
60
61
super .close ();
61
62
} finally {
62
- out .close ();
63
+ delegate .close ();
63
64
}
64
65
65
66
}
66
67
67
68
@ Override
68
69
protected void flushBuffer (byte [] b , int offset , int len ) throws IOException {
69
- out .writeBytes (b , offset , len );
70
+ if (bufferedDelegate != null ) {
71
+ bufferedDelegate .flushBuffer (b , offset , len );
72
+ } else {
73
+ delegate .writeBytes (b , offset , len );
74
+ }
70
75
digest .update (b , offset , len );
71
76
}
72
77
@@ -77,8 +82,11 @@ protected void flushBuffer(byte[] b, int offset, int len) throws IOException {
77
82
78
83
@ Override
79
84
public void flush () throws IOException {
80
- super .flush ();
81
- out .flush ();
85
+ try {
86
+ super .flush ();
87
+ } finally {
88
+ delegate .flush ();
89
+ }
82
90
}
83
91
84
92
@ Override
@@ -87,21 +95,21 @@ public void seek(long pos) throws IOException {
87
95
// but a checksum of the bytes written to this stream, which is the same for each
88
96
// type of file in lucene
89
97
super .seek (pos );
90
- out .seek (pos );
98
+ delegate .seek (pos );
91
99
}
92
100
93
101
@ Override
94
102
public long length () throws IOException {
95
- return out .length ();
103
+ return delegate .length ();
96
104
}
97
105
98
106
@ Override
99
107
public void setLength (long length ) throws IOException {
100
- out .setLength (length );
108
+ delegate .setLength (length );
101
109
}
102
110
103
111
@ Override
104
112
public String toString () {
105
- return out .toString ();
113
+ return delegate .toString ();
106
114
}
107
115
}
0 commit comments