1
package org.apache.lucene.store;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import java.io.IOException;
21
import java.util.zip.CRC32;
22
import java.util.zip.Checksum;
24
/** Writes bytes through to a primary IndexOutput, computing
25
* checksum. Note that you cannot use seek().
29
public class ChecksumIndexOutput extends IndexOutput {
33
public ChecksumIndexOutput(IndexOutput main) {
39
public void writeByte(byte b) throws IOException {
45
public void writeBytes(byte[] b, int offset, int length) throws IOException {
46
digest.update(b, offset, length);
47
main.writeBytes(b, offset, length);
50
public long getChecksum() {
51
return digest.getValue();
55
public void flush() throws IOException {
60
public void close() throws IOException {
65
public long getFilePointer() {
66
return main.getFilePointer();
70
public void seek(long pos) {
71
throw new RuntimeException("not allowed");
75
* Starts but does not complete the commit of this file (=
76
* writing of the final checksum at the end). After this
77
* is called must call {@link #finishCommit} and the
78
* {@link #close} to complete the commit.
80
public void prepareCommit() throws IOException {
81
final long checksum = getChecksum();
82
// Intentionally write a mismatched checksum. This is
83
// because we want to 1) test, as best we can, that we
84
// are able to write a long to the file, but 2) not
85
// actually "commit" the file yet. This (prepare
86
// commit) is phase 1 of a two-phase commit.
87
final long pos = main.getFilePointer();
88
main.writeLong(checksum-1);
93
/** See {@link #prepareCommit} */
94
public void finishCommit() throws IOException {
95
main.writeLong(getChecksum());
99
public long length() throws IOException {
100
return main.length();