pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / ChecksumIndexOutput.java
1 package org.apache.lucene.store;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.util.zip.CRC32;
22 import java.util.zip.Checksum;
23
24 /** Writes bytes through to a primary IndexOutput, computing
25  *  checksum.  Note that you cannot use seek().
26  *
27  * @lucene.internal
28  */
29 public class ChecksumIndexOutput extends IndexOutput {
30   IndexOutput main;
31   Checksum digest;
32
33   public ChecksumIndexOutput(IndexOutput main) {
34     this.main = main;
35     digest = new CRC32();
36   }
37
38   @Override
39   public void writeByte(byte b) throws IOException {
40     digest.update(b);
41     main.writeByte(b);
42   }
43
44   @Override
45   public void writeBytes(byte[] b, int offset, int length) throws IOException {
46     digest.update(b, offset, length);
47     main.writeBytes(b, offset, length);
48   }
49
50   public long getChecksum() {
51     return digest.getValue();
52   }
53
54   @Override
55   public void flush() throws IOException {
56     main.flush();
57   }
58
59   @Override
60   public void close() throws IOException {
61     main.close();
62   }
63
64   @Override
65   public long getFilePointer() {
66     return main.getFilePointer();
67   }
68
69   @Override
70   public void seek(long pos) {
71     throw new RuntimeException("not allowed");    
72   }
73
74   /**
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.
79    */
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);
89     main.flush();
90     main.seek(pos);
91   }
92
93   /** See {@link #prepareCommit} */
94   public void finishCommit() throws IOException {
95     main.writeLong(getChecksum());
96   }
97
98   @Override
99   public long length() throws IOException {
100     return main.length();
101   }
102 }